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

Admin pages for hosted site management:
- Dashboard - Overview with KPIs and charts
- Sites - Hosted site list with filters
- Site Detail - Single site view with tabs (overview, services, store link)
- Site New - Create new hosted site form
- Clients - All services overview with expiry warnings
"""

from fastapi import APIRouter, Depends, Path, 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()


@router.get("/hosting", response_class=HTMLResponse, include_in_schema=False)
async def admin_hosting_dashboard(
    request: Request,
    current_user: User = Depends(require_menu_access("hosting-dashboard", FrontendType.ADMIN)),
    db: Session = Depends(get_db),
):
    """Render hosting dashboard page."""
    return templates.TemplateResponse(
        "hosting/admin/dashboard.html",
        get_admin_context(request, db, current_user),
    )


@router.get("/hosting/sites", response_class=HTMLResponse, include_in_schema=False)
async def admin_hosting_sites(
    request: Request,
    current_user: User = Depends(require_menu_access("hosting-sites", FrontendType.ADMIN)),
    db: Session = Depends(get_db),
):
    """Render hosted sites list page."""
    return templates.TemplateResponse(
        "hosting/admin/sites.html",
        get_admin_context(request, db, current_user),
    )


@router.get("/hosting/sites/new", response_class=HTMLResponse, include_in_schema=False)
async def admin_hosting_site_new(
    request: Request,
    current_user: User = Depends(require_menu_access("hosting-sites", FrontendType.ADMIN)),
    db: Session = Depends(get_db),
):
    """Render new hosted site form."""
    return templates.TemplateResponse(
        "hosting/admin/site-new.html",
        get_admin_context(request, db, current_user),
    )


@router.get(
    "/hosting/sites/{site_id}",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_hosting_site_detail(
    request: Request,
    site_id: int = Path(..., description="Hosted Site ID"),
    current_user: User = Depends(require_menu_access("hosting-sites", FrontendType.ADMIN)),
    db: Session = Depends(get_db),
):
    """Render hosted site detail page."""
    context = get_admin_context(request, db, current_user)
    context["site_id"] = site_id
    return templates.TemplateResponse(
        "hosting/admin/site-detail.html",
        context,
    )


@router.get("/hosting/clients", response_class=HTMLResponse, include_in_schema=False)
async def admin_hosting_clients(
    request: Request,
    current_user: User = Depends(require_menu_access("hosting-clients", FrontendType.ADMIN)),
    db: Session = Depends(get_db),
):
    """Render all client services overview page."""
    return templates.TemplateResponse(
        "hosting/admin/clients.html",
        get_admin_context(request, db, current_user),
    )
