# app/modules/billing/routes/pages/store.py
"""
Billing Store Page Routes (HTML rendering).

Store pages for billing management:
- Billing dashboard
- Invoices
"""

from fastapi import APIRouter, Depends, Request
from fastapi.responses import HTMLResponse
from sqlalchemy.orm import Session

from app.api.deps import (
    get_current_store_from_cookie_or_header,
    get_db,
    get_resolved_store_code,
)
from app.modules.core.utils.page_context import get_store_context
from app.modules.tenancy.models import User
from app.templates_config import templates

router = APIRouter()


# ============================================================================
# BILLING ROUTES
# ============================================================================


@router.get(
    "/billing", response_class=HTMLResponse, include_in_schema=False
)
async def store_billing_page(
    request: Request,
    store_code: str = Depends(get_resolved_store_code),
    current_user: User = Depends(get_current_store_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render billing and subscription management page.
    JavaScript loads subscription status, tiers, and invoices via API.
    """
    return templates.TemplateResponse(
        "billing/store/billing.html",
        get_store_context(request, db, current_user, store_code),
    )


@router.get(
    "/invoices", response_class=HTMLResponse, include_in_schema=False
)
async def store_invoices_page(
    request: Request,
    store_code: str = Depends(get_resolved_store_code),
    current_user: User = Depends(get_current_store_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render invoices management page.
    JavaScript loads invoices via API.
    """
    return templates.TemplateResponse(
        "orders/store/invoices.html",
        get_store_context(request, db, current_user, store_code),
    )
