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

Store pages for customer management:
- Customers list
"""

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


# ============================================================================
# CUSTOMER MANAGEMENT
# ============================================================================


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


@router.get(
    "/customers/{customer_id}", response_class=HTMLResponse, include_in_schema=False
)
async def store_customer_detail_page(
    request: Request,
    customer_id: int,
    store_code: str = Depends(get_resolved_store_code),
    current_user: User = Depends(require_store_page_permission("customers.view")),
    db: Session = Depends(get_db),
):
    """
    Render customer detail page.
    JavaScript loads customer profile and order stats via API.
    """
    context = get_store_context(request, db, current_user, store_code)
    context["customer_id"] = customer_id
    return templates.TemplateResponse(
        "customers/store/customer-detail.html",
        context,
    )
