# app/modules/billing/routes/pages/merchant.py """ Merchant Billing Page Routes (HTML rendering). Page routes for the merchant billing portal: - Dashboard (overview of stores, subscriptions) - Subscriptions list - Subscription detail per platform - Billing history / invoices - Login page Authentication: merchant_token cookie or Authorization header. Login page uses optional auth to check if already logged in. Auto-discovered by the route system (merchant.py in routes/pages/ triggers registration under /merchants/billing/*). """ from fastapi import APIRouter, Depends, Path, Request from fastapi.responses import HTMLResponse, RedirectResponse from sqlalchemy.orm import Session from app.api.deps import ( get_current_merchant_from_cookie_or_header, get_current_merchant_optional, ) from app.core.database import get_db from app.modules.core.utils.page_context import get_context_for_frontend from app.modules.enums import FrontendType from app.templates_config import templates from models.schema.auth import UserContext ROUTE_CONFIG = { "prefix": "/billing", } router = APIRouter() # ============================================================================ # Helper # ============================================================================ def _get_merchant_context( request: Request, db: Session, current_user: UserContext, **extra_context, ) -> dict: """ Build template context for merchant portal pages. Uses the module-driven context builder with FrontendType.MERCHANT, and adds the authenticated user to the context. Args: request: FastAPI request db: Database session current_user: Authenticated merchant user context **extra_context: Additional template variables Returns: Dict of context variables for template rendering """ return get_context_for_frontend( FrontendType.MERCHANT, request, db, user=current_user, **extra_context, ) # ============================================================================ # DASHBOARD # ============================================================================ @router.get("/", response_class=HTMLResponse, include_in_schema=False) async def merchant_dashboard_page( request: Request, current_user: UserContext = Depends(get_current_merchant_from_cookie_or_header), db: Session = Depends(get_db), ): """ Render merchant dashboard page. Shows an overview of the merchant's stores and subscriptions. """ context = _get_merchant_context(request, db, current_user) return templates.TemplateResponse( "billing/merchant/dashboard.html", context, ) # ============================================================================ # SUBSCRIPTIONS # ============================================================================ @router.get("/subscriptions", response_class=HTMLResponse, include_in_schema=False) async def merchant_subscriptions_page( request: Request, current_user: UserContext = Depends(get_current_merchant_from_cookie_or_header), db: Session = Depends(get_db), ): """ Render merchant subscriptions list page. Shows all subscriptions across platforms with status and tier info. """ context = _get_merchant_context(request, db, current_user) return templates.TemplateResponse( "billing/merchant/subscriptions.html", context, ) @router.get( "/subscriptions/{platform_id}", response_class=HTMLResponse, include_in_schema=False, ) async def merchant_subscription_detail_page( request: Request, platform_id: int = Path(..., description="Platform ID"), current_user: UserContext = Depends(get_current_merchant_from_cookie_or_header), db: Session = Depends(get_db), ): """ Render subscription detail page for a specific platform. Shows subscription status, tier details, usage, and upgrade options. """ context = _get_merchant_context( request, db, current_user, platform_id=platform_id ) return templates.TemplateResponse( "billing/merchant/subscription-detail.html", context, ) # ============================================================================ # BILLING HISTORY # ============================================================================ @router.get("/billing", response_class=HTMLResponse, include_in_schema=False) async def merchant_billing_history_page( request: Request, current_user: UserContext = Depends(get_current_merchant_from_cookie_or_header), db: Session = Depends(get_db), ): """ Render billing history page. Shows invoice history and payment records for the merchant. """ context = _get_merchant_context(request, db, current_user) return templates.TemplateResponse( "billing/merchant/billing-history.html", context, ) # ============================================================================ # LOGIN # ============================================================================ @router.get("/login", response_class=HTMLResponse, include_in_schema=False) async def merchant_login_page( request: Request, current_user: UserContext | None = Depends(get_current_merchant_optional), db: Session = Depends(get_db), ): """ Render merchant login page. If the user is already authenticated as a merchant owner, redirects to the merchant dashboard. """ # Redirect to dashboard if already logged in if current_user is not None: return RedirectResponse(url="/merchants/billing/", status_code=302) context = get_context_for_frontend( FrontendType.MERCHANT, request, db, ) return templates.TemplateResponse( "billing/merchant/login.html", context, )