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

Admin pages for billing and subscription management:
- Subscription tiers
- Subscriptions list
- Billing history
"""

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

from app.api.deps import get_db, require_menu_access
from app.modules.core.utils.page_context import get_admin_context
from app.modules.enums import FrontendType
from app.modules.tenancy.models import User
from app.templates_config import templates

router = APIRouter()


# ============================================================================
# BILLING & SUBSCRIPTIONS ROUTES
# ============================================================================


@router.get("/subscription-tiers", response_class=HTMLResponse, include_in_schema=False)
async def admin_subscription_tiers_page(
    request: Request,
    current_user: User = Depends(
        require_menu_access("subscription-tiers", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render subscription tiers management page.
    Shows all subscription tiers with their limits and pricing.
    """
    return templates.TemplateResponse(
        "billing/admin/subscription-tiers.html",
        get_admin_context(request, db, current_user),
    )


@router.get("/subscriptions", response_class=HTMLResponse, include_in_schema=False)
async def admin_subscriptions_page(
    request: Request,
    current_user: User = Depends(
        require_menu_access("subscriptions", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render store subscriptions management page.
    Shows all store subscriptions with status and usage.
    """
    return templates.TemplateResponse(
        "billing/admin/subscriptions.html",
        get_admin_context(request, db, current_user),
    )


@router.get("/billing-history", response_class=HTMLResponse, include_in_schema=False)
async def admin_billing_history_page(
    request: Request,
    current_user: User = Depends(
        require_menu_access("billing-history", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render billing history page.
    Shows invoices and payments across all stores.
    """
    return templates.TemplateResponse(
        "billing/admin/billing-history.html",
        get_admin_context(request, db, current_user),
    )
