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

Store pages for core functionality:
- Dashboard
- Media library
- Notifications
"""

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

from app.api.deps import (
    UserContext,
    get_current_store_from_cookie_or_header,
    get_current_store_optional,
    get_db,
    get_resolved_store_code,
)
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()


# ============================================================================
# STORE ROOT REDIRECT
# ============================================================================


@router.get("/", response_class=RedirectResponse, include_in_schema=False)
async def store_root(
    current_user: UserContext | None = Depends(get_current_store_optional),
):
    """Redirect /store/ based on authentication status."""
    if current_user:
        return RedirectResponse(url="/store/dashboard", status_code=302)
    return RedirectResponse(url="/store/login", status_code=302)


# ============================================================================
# STORE DASHBOARD
# ============================================================================


@router.get(
    "/dashboard", response_class=HTMLResponse, include_in_schema=False
)
async def store_dashboard_page(
    request: Request,
    store_code: str = Depends(get_resolved_store_code),
    current_user: User = Depends(get_current_store_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render store dashboard.

    JavaScript will:
    - Load store info via API
    - Load dashboard stats via API
    - Load recent orders via API
    - Handle all interactivity
    """
    return templates.TemplateResponse(
        "core/store/dashboard.html",
        get_store_context(request, db, current_user, store_code),
    )


# ============================================================================
# MEDIA LIBRARY
# ============================================================================


@router.get(
    "/media", response_class=HTMLResponse, include_in_schema=False
)
async def store_media_page(
    request: Request,
    store_code: str = Depends(get_resolved_store_code),
    current_user: User = Depends(get_current_store_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render media library page.
    JavaScript loads media files via API.
    """
    return templates.TemplateResponse(
        "cms/store/media.html",
        get_store_context(request, db, current_user, store_code),
    )


# ============================================================================
# NOTIFICATIONS
# ============================================================================


@router.get(
    "/notifications",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def store_notifications_page(
    request: Request,
    store_code: str = Depends(get_resolved_store_code),
    current_user: User = Depends(get_current_store_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render notifications center page.
    JavaScript loads notifications via API.
    """
    return templates.TemplateResponse(
        "messaging/store/notifications.html",
        get_store_context(request, db, current_user, store_code),
    )
