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

Admin pages for vendor product catalog management:
- Vendor products list
- Vendor product create
- Vendor product detail/edit
"""

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.templates_config import templates
from app.modules.enums import FrontendType
from app.modules.tenancy.models import User

router = APIRouter()


# ============================================================================
# VENDOR PRODUCT CATALOG ROUTES
# ============================================================================


@router.get("/vendor-products", response_class=HTMLResponse, include_in_schema=False)
async def admin_vendor_products_page(
    request: Request,
    current_user: User = Depends(
        require_menu_access("vendor-products", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render vendor products catalog page.
    Browse vendor-specific product catalogs with override capability.
    """
    return templates.TemplateResponse(
        "catalog/admin/vendor-products.html",
        get_admin_context(request, db, current_user),
    )


@router.get(
    "/vendor-products/create", response_class=HTMLResponse, include_in_schema=False
)
async def admin_vendor_product_create_page(
    request: Request,
    current_user: User = Depends(
        require_menu_access("vendor-products", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render vendor product create page.
    Create a new vendor product entry.
    """
    return templates.TemplateResponse(
        "catalog/admin/vendor-product-create.html",
        get_admin_context(request, db, current_user),
    )


@router.get(
    "/vendor-products/{product_id}",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_vendor_product_detail_page(
    request: Request,
    product_id: int = Path(..., description="Vendor Product ID"),
    current_user: User = Depends(
        require_menu_access("vendor-products", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render vendor product detail page.
    Shows full product information with vendor-specific overrides.
    """
    return templates.TemplateResponse(
        "catalog/admin/vendor-product-detail.html",
        get_admin_context(request, db, current_user, product_id=product_id),
    )


@router.get(
    "/vendor-products/{product_id}/edit",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_vendor_product_edit_page(
    request: Request,
    product_id: int = Path(..., description="Vendor Product ID"),
    current_user: User = Depends(
        require_menu_access("vendor-products", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render vendor product edit page.
    Edit vendor product information and overrides.
    """
    return templates.TemplateResponse(
        "catalog/admin/vendor-product-edit.html",
        get_admin_context(request, db, current_user, product_id=product_id),
    )
