"""Routes PWA : manifeste Web App dynamique (préfixe proxy inclus)."""

from flask import Blueprint, jsonify, request, url_for

bp = Blueprint("pwa", __name__)


@bp.route("/manifest.webmanifest")
def manifest():
    """Manifeste installable ; start_url/scope suivent SCRIPT_NAME (ex. /copro_gestion)."""
    root = (request.script_root or "").rstrip("/") + "/"
    icons_base = url_for("static", filename="icons/")

    data = {
        "id": root,
        "name": "Gestion copropriété",
        "short_name": "Copro",
        "lang": "fr",
        "start_url": root,
        "scope": root,
        "display": "standalone",
        "background_color": "#1e3a5f",
        "theme_color": "#1e3a5f",
        "icons": [
            {
                "src": icons_base + "icon-192.png",
                "sizes": "192x192",
                "type": "image/png",
                "purpose": "any",
            },
            {
                "src": icons_base + "icon-512.png",
                "sizes": "512x512",
                "type": "image/png",
                "purpose": "any",
            },
            {
                "src": icons_base + "icon-512-maskable.png",
                "sizes": "512x512",
                "type": "image/png",
                "purpose": "maskable",
            },
        ],
    }
    response = jsonify(data)
    response.headers["Content-Type"] = "application/manifest+json"
    return response
