from __future__ import annotations
import path_setup  # noqa: F401  # adds shared/ to sys.path
from pathlib import Path

from quart import g, request
from jinja2 import FileSystemLoader, ChoiceLoader
from sqlalchemy import select

from shared.infrastructure.factory import create_base_app
from shared.config import config
from shared.models import KV

from bp import (
    register_blog_bp,
    register_admin,
    register_menu_items,
    register_snippets,
    register_data,
    register_actions,
)
from sxc.pages import setup_blog_pages


async def blog_context() -> dict:
    """
    Blog app context processor.

    - cart_count/cart_total: via cart service (shared DB)
    - cart_mini / auth_menu / nav_tree: pre-fetched fragments
    """
    from shared.infrastructure.context import base_context
    from shared.services.navigation import get_navigation_tree
    from shared.infrastructure.cart_identity import current_cart_identity
    from shared.infrastructure.fragments import fetch_fragments
    from shared.infrastructure.data_client import fetch_data
    from shared.contracts.dtos import CartSummaryDTO, dto_from_dict

    ctx = await base_context()

    # Fallback for _nav.html when nav-tree fragment fetch fails
    ctx["menu_items"] = await get_navigation_tree(g.s)

    # Cart data via internal data endpoint
    ident = current_cart_identity()
    summary_params = {}
    if ident["user_id"] is not None:
        summary_params["user_id"] = ident["user_id"]
    if ident["session_id"] is not None:
        summary_params["session_id"] = ident["session_id"]
    raw = await fetch_data("cart", "cart-summary", params=summary_params, required=False)
    summary = dto_from_dict(CartSummaryDTO, raw) if raw else CartSummaryDTO()
    ctx["cart_count"] = summary.count + summary.calendar_count + summary.ticket_count
    ctx["cart_total"] = float(summary.total + summary.calendar_total + summary.ticket_total)

    # Pre-fetch cross-app HTML fragments concurrently
    # (fetch_fragment auto-skips when inside a fragment request to prevent circular deps)
    user = getattr(g, "user", None)
    cart_params = {}
    if ident["user_id"] is not None:
        cart_params["user_id"] = ident["user_id"]
    if ident["session_id"] is not None:
        cart_params["session_id"] = ident["session_id"]

    auth_params = {"email": user.email} if user else {}
    nav_params = {"app_name": "blog", "path": request.path}

    cart_mini, auth_menu, nav_tree = await fetch_fragments([
        ("cart", "cart-mini", cart_params or None),
        ("account", "auth-menu", auth_params or None),
        ("blog", "nav-tree", nav_params),
    ])
    ctx["cart_mini"] = cart_mini
    ctx["auth_menu"] = auth_menu
    ctx["nav_tree"] = nav_tree

    return ctx


def create_app() -> "Quart":
    from services import register_domain_services

    setup_blog_pages()

    app = create_base_app(
        "blog",
        context_fn=blog_context,
        domain_services_fn=register_domain_services,
    )

    # App-specific templates override shared templates
    app_templates = str(Path(__file__).resolve().parent / "templates")
    app.jinja_loader = ChoiceLoader([
        FileSystemLoader(app_templates),
        app.jinja_loader,
    ])

    # --- blueprints ---
    app.register_blueprint(
        register_blog_bp(
            url_prefix=config()["blog_root"],
            title=config()["blog_title"],
        ),
        url_prefix=config()["blog_root"],
    )

    app.register_blueprint(register_admin("/settings"))
    app.register_blueprint(register_menu_items())
    app.register_blueprint(register_snippets())
    from shared.sx.handlers import auto_mount_fragment_handlers
    auto_mount_fragment_handlers(app, "blog")

    app.register_blueprint(register_data())
    app.register_blueprint(register_actions())

    # --- KV admin endpoints ---
    @app.get("/settings/kv/<key>")
    async def kv_get(key: str):
        row = (
            await g.s.execute(select(KV).where(KV.key == key))
        ).scalar_one_or_none()
        return {"key": key, "value": (row.value if row else None)}

    @app.post("/settings/kv/<key>")
    async def kv_set(key: str):
        data = await request.get_json() or {}
        val = data.get("value", "")
        obj = await g.s.get(KV, key)
        if obj is None:
            obj = KV(key=key, value=val)
            g.s.add(obj)
        else:
            obj.value = val
        return {"ok": True, "key": key, "value": val}

    # --- oEmbed endpoint ---
    @app.get("/oembed")
    async def oembed():
        from urllib.parse import urlparse
        from quart import jsonify
        from services import blog_service
        from shared.infrastructure.urls import blog_url
        from shared.infrastructure.oembed import build_oembed_response

        url = request.args.get("url", "")
        if not url:
            return jsonify({"error": "url parameter required"}), 400

        parsed = urlparse(url)
        slug = parsed.path.strip("/").split("/")[-1] if parsed.path else ""
        if not slug:
            return jsonify({"error": "could not extract slug"}), 404

        post = await blog_service.get_post_by_slug(g.s, slug)
        if not post:
            return jsonify({"error": "not found"}), 404

        resp = build_oembed_response(
            title=post.title,
            oembed_type="link",
            thumbnail_url=post.feature_image,
            url=blog_url(f"/{post.slug}"),
        )
        return jsonify(resp)

    # Auto-mount all defpages with absolute paths
    from shared.sx.pages import auto_mount_pages
    auto_mount_pages(app, "blog")

    # --- Pass defpage helper data to template context for layouts ---
    @app.context_processor
    async def inject_blog_data():
        import os
        from shared.config import config as get_config
        ctx = {
            "blog_title": get_config()["blog_title"],
            "base_title": get_config()["title"],
            "unsplash_api_key": os.environ.get("UNSPLASH_ACCESS_KEY", ""),
        }
        ctx.update(getattr(g, '_defpage_ctx', {}))
        return ctx

    # --- debug: url rules ---
    @app.get("/__rules")
    async def dump_rules():
        rules = []
        for r in app.url_map.iter_rules():
            rules.append({
                "endpoint": r.endpoint,
                "rule": repr(r.rule),
                "methods": sorted(r.methods - {"HEAD", "OPTIONS"}),
                "strict_slashes": r.strict_slashes,
            })
        return {"rules": rules}

    return app


app = create_app()
