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

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

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

from app.api.deps import get_current_vendor_from_cookie_or_header, get_db
from app.modules.core.utils.page_context import get_vendor_context
from app.templates_config import templates
from app.modules.tenancy.models import User

router = APIRouter()


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


@router.get(
    "/{vendor_code}/billing", response_class=HTMLResponse, include_in_schema=False
)
async def vendor_billing_page(
    request: Request,
    vendor_code: str = Path(..., description="Vendor code"),
    current_user: User = Depends(get_current_vendor_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/vendor/billing.html",
        get_vendor_context(request, db, current_user, vendor_code),
    )


@router.get(
    "/{vendor_code}/invoices", response_class=HTMLResponse, include_in_schema=False
)
async def vendor_invoices_page(
    request: Request,
    vendor_code: str = Path(..., description="Vendor code"),
    current_user: User = Depends(get_current_vendor_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render invoices management page.
    JavaScript loads invoices via API.
    """
    return templates.TemplateResponse(
        "orders/vendor/invoices.html",
        get_vendor_context(request, db, current_user, vendor_code),
    )
