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

Admin pages for managing platform and store content pages,
and store theme customization.
"""

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()


# ============================================================================
# CONTENT PAGES MANAGEMENT
# ============================================================================


@router.get("/content-pages", response_class=HTMLResponse, include_in_schema=False)
async def admin_content_pages_list(
    request: Request,
    current_user: User = Depends(require_menu_access("content-pages", FrontendType.ADMIN)),
    db: Session = Depends(get_db),
):
    """
    Render content pages list.
    Shows all platform defaults and store overrides with filtering.
    """
    return templates.TemplateResponse(
        "cms/admin/content-pages.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get(
    "/content-pages/create", response_class=HTMLResponse, include_in_schema=False
)
async def admin_content_page_create(
    request: Request,
    current_user: User = Depends(require_menu_access("content-pages", FrontendType.ADMIN)),
    db: Session = Depends(get_db),
):
    """
    Render create content page form.
    Allows creating new platform defaults or store-specific pages.
    """
    return templates.TemplateResponse(
        "cms/admin/content-page-edit.html",
        {
            "request": request,
            "user": current_user,
            "page_id": None,  # Indicates this is a create operation
        },
    )


@router.get(
    "/content-pages/{page_id}/edit",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_content_page_edit(
    request: Request,
    page_id: int = Path(..., description="Content page ID"),
    current_user: User = Depends(require_menu_access("content-pages", FrontendType.ADMIN)),
    db: Session = Depends(get_db),
):
    """
    Render edit content page form.
    Allows editing existing platform or store content pages.
    """
    return templates.TemplateResponse(
        "cms/admin/content-page-edit.html",
        {
            "request": request,
            "user": current_user,
            "page_id": page_id,
        },
    )


# ============================================================================
# STORE THEMES
# ============================================================================


@router.get("/store-themes", response_class=HTMLResponse, include_in_schema=False)
async def admin_store_themes_page(
    request: Request,
    current_user: User = Depends(
        require_menu_access("store-themes", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render store themes selection page.
    Allows admins to select a store to customize their theme.
    """
    return templates.TemplateResponse(
        "cms/admin/store-themes.html",
        get_admin_context(request, db, current_user),
    )


@router.get(
    "/stores/{store_code}/theme",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_store_theme_page(
    request: Request,
    store_code: str = Path(..., description="Store code"),
    current_user: User = Depends(
        require_menu_access("store-themes", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render store theme customization page.
    Allows admins to customize colors, fonts, layout, and branding.
    """
    return templates.TemplateResponse(
        "cms/admin/store-theme.html",
        get_admin_context(request, db, current_user, store_code=store_code),
    )
