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

Store pages for product management:
- Products list
- Product create
"""

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

from app.api.deps import (
    get_db,
    get_resolved_store_code,
    require_store_page_permission,
)
from app.modules.core.utils.page_context import get_store_context
from app.modules.tenancy.models import User
from app.templates_config import templates

router = APIRouter()


# ============================================================================
# PRODUCT MANAGEMENT
# ============================================================================


@router.get(
    "/products", response_class=HTMLResponse, include_in_schema=False
)
async def store_products_page(
    request: Request,
    store_code: str = Depends(get_resolved_store_code),
    current_user: User = Depends(require_store_page_permission("products.view")),
    db: Session = Depends(get_db),
):
    """
    Render products management page.
    JavaScript loads product list via API.
    """
    return templates.TemplateResponse(
        "catalog/store/products.html",
        get_store_context(request, db, current_user, store_code),
    )


@router.get(
    "/products/create",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def store_product_create_page(
    request: Request,
    store_code: str = Depends(get_resolved_store_code),
    current_user: User = Depends(require_store_page_permission("products.create")),
    db: Session = Depends(get_db),
):
    """
    Render product creation page.
    JavaScript handles form submission via API.
    """
    return templates.TemplateResponse(
        "catalog/store/product-create.html",
        get_store_context(request, db, current_user, store_code),
    )
