"""Unauthenticated marketing + legal pages.

This router carries no auth dependency — every route is reachable to
anonymous visitors and is also reachable to logged-in users (the
templates branch off `cu` to flip the top-right CTA between
"Sign in / sign up" and "Dashboard").

The dual-purpose root (`/`) lives in `app/routers/pages.py` because it
also has to render the dashboard when authenticated. Pure public-only
pages live here.
"""
from __future__ import annotations

from fastapi import APIRouter, Depends, Request
from fastapi.responses import HTMLResponse

from app.auth import CurrentUser, maybe_current_user
from app.services.access import is_paid_active
from app.services.feature_flags import require_flag
from app.templates_env import templates


router = APIRouter(tags=["public"])


def _ctx(request: Request, cu: CurrentUser | None) -> dict:
    """Minimal context every public template expects. `cu` is injected
    into the template so the header CTA can flip between
    'Sign in / sign up' and 'Dashboard'."""
    return {"cu": cu}


@router.get(
    "/pricing",
    response_class=HTMLResponse,
    dependencies=[Depends(require_flag("SUBSCRIPTIONS_ENABLED"))],
)
async def pricing_page(
    request: Request,
    cu: CurrentUser | None = Depends(maybe_current_user),
):
    ctx = _ctx(request, cu)
    ctx["paid"] = is_paid_active(cu)
    return templates.TemplateResponse(request, "pricing.html", ctx)


@router.get("/about", response_class=HTMLResponse)
async def about_page(
    request: Request,
    cu: CurrentUser | None = Depends(maybe_current_user),
):
    return templates.TemplateResponse(request, "about.html", _ctx(request, cu))


@router.get("/terms", response_class=HTMLResponse)
async def terms_page(
    request: Request,
    cu: CurrentUser | None = Depends(maybe_current_user),
):
    return templates.TemplateResponse(request, "terms.html", _ctx(request, cu))


@router.get("/privacy", response_class=HTMLResponse)
async def privacy_page(
    request: Request,
    cu: CurrentUser | None = Depends(maybe_current_user),
):
    return templates.TemplateResponse(request, "privacy.html", _ctx(request, cu))


@router.get("/disclaimer", response_class=HTMLResponse)
async def disclaimer_page(
    request: Request,
    cu: CurrentUser | None = Depends(maybe_current_user),
):
    return templates.TemplateResponse(request, "disclaimer.html", _ctx(request, cu))
