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

Admin pages for store product catalog management:
- Store products list
- Store product create
- Store 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.modules.enums import FrontendType
from app.modules.tenancy.models import User
from app.templates_config import templates

router = APIRouter()


# ============================================================================
# STORE PRODUCT CATALOG ROUTES
# ============================================================================


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


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


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


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