# app/modules/cms/routes/pages/vendor.py
"""
CMS Vendor Page Routes (HTML rendering).

Vendor pages for managing content pages and rendering CMS content.
"""

import logging

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

from app.api.deps import get_current_vendor_from_cookie_or_header, get_db
from app.modules.cms.services import content_page_service
from app.modules.core.services.platform_settings_service import platform_settings_service  # noqa: MOD-004 - shared platform service
from app.templates_config import templates
from app.modules.tenancy.models import User
from app.modules.tenancy.models import Vendor

logger = logging.getLogger(__name__)

router = APIRouter()


# ============================================================================
# HELPER: Build Vendor Dashboard Context
# ============================================================================


def get_vendor_context(
    request: Request,
    db: Session,
    current_user: User,
    vendor_code: str,
    **extra_context,
) -> dict:
    """
    Build template context for vendor dashboard pages.

    Resolves locale/currency using the platform settings service with
    vendor override support.
    """
    # Load vendor from database
    vendor = db.query(Vendor).filter(Vendor.subdomain == vendor_code).first()

    # Get platform defaults
    platform_config = platform_settings_service.get_storefront_config(db)

    # Resolve with vendor override
    storefront_locale = platform_config["locale"]
    storefront_currency = platform_config["currency"]

    if vendor and vendor.storefront_locale:
        storefront_locale = vendor.storefront_locale

    context = {
        "request": request,
        "user": current_user,
        "vendor": vendor,
        "vendor_code": vendor_code,
        "storefront_locale": storefront_locale,
        "storefront_currency": storefront_currency,
        "dashboard_language": vendor.dashboard_language if vendor else "en",
    }

    # Add any extra context
    if extra_context:
        context.update(extra_context)

    return context


# ============================================================================
# CONTENT PAGES MANAGEMENT
# ============================================================================


@router.get(
    "/{vendor_code}/content-pages", response_class=HTMLResponse, include_in_schema=False
)
async def vendor_content_pages_list(
    request: Request,
    vendor_code: str = Path(..., description="Vendor code"),
    current_user: User = Depends(get_current_vendor_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render content pages management page.
    Shows platform defaults (can be overridden) and vendor custom pages.
    """
    return templates.TemplateResponse(
        "cms/vendor/content-pages.html",
        get_vendor_context(request, db, current_user, vendor_code),
    )


@router.get(
    "/{vendor_code}/content-pages/create",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def vendor_content_page_create(
    request: Request,
    vendor_code: str = Path(..., description="Vendor code"),
    current_user: User = Depends(get_current_vendor_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render content page creation form.
    """
    return templates.TemplateResponse(
        "cms/vendor/content-page-edit.html",
        get_vendor_context(request, db, current_user, vendor_code, page_id=None),
    )


@router.get(
    "/{vendor_code}/content-pages/{page_id}/edit",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def vendor_content_page_edit(
    request: Request,
    vendor_code: str = Path(..., description="Vendor code"),
    page_id: int = Path(..., description="Content page ID"),
    current_user: User = Depends(get_current_vendor_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render content page edit form.
    """
    return templates.TemplateResponse(
        "cms/vendor/content-page-edit.html",
        get_vendor_context(request, db, current_user, vendor_code, page_id=page_id),
    )


# ============================================================================
# DYNAMIC CONTENT PAGES (CMS) - Public Shop Display
# ============================================================================


@router.get(
    "/{vendor_code}/{slug}", response_class=HTMLResponse, include_in_schema=False
)
async def vendor_content_page(
    request: Request,
    vendor_code: str = Path(..., description="Vendor code"),
    slug: str = Path(..., description="Content page slug"),
    db: Session = Depends(get_db),
):
    """
    Generic content page handler for vendor shop (CMS).

    Handles dynamic content pages like:
    - /vendors/wizamart/about, /vendors/wizamart/faq, /vendors/wizamart/contact, etc.

    Features:
    - Two-tier system: Vendor overrides take priority, fallback to platform defaults
    - Only shows published pages
    - Returns 404 if page not found or unpublished

    NOTE: This is a catch-all route and must be registered LAST to avoid
    shadowing other specific routes.
    """
    logger.debug(
        "[CMS] vendor_content_page REACHED",
        extra={
            "path": request.url.path,
            "vendor_code": vendor_code,
            "slug": slug,
            "vendor": getattr(request.state, "vendor", "NOT SET"),
            "context": getattr(request.state, "context_type", "NOT SET"),
        },
    )

    vendor = getattr(request.state, "vendor", None)
    vendor_id = vendor.id if vendor else None

    # Load content page from database (vendor override → platform default)
    page = content_page_service.get_page_for_vendor(
        db, slug=slug, vendor_id=vendor_id, include_unpublished=False
    )

    if not page:
        logger.info(
            f"[CMS] Content page not found: {slug}",
            extra={
                "slug": slug,
                "vendor_code": vendor_code,
                "vendor_id": vendor_id,
            },
        )
        raise HTTPException(status_code=404, detail="Page not found")

    logger.info(
        f"[CMS] Rendering page: {page.title}",
        extra={
            "slug": slug,
            "page_id": page.id,
            "is_vendor_override": page.vendor_id is not None,
            "vendor_id": vendor_id,
        },
    )

    # Resolve locale for shop template
    platform_config = platform_settings_service.get_storefront_config(db)
    storefront_locale = platform_config["locale"]
    storefront_currency = platform_config["currency"]

    if vendor and vendor.storefront_locale:
        storefront_locale = vendor.storefront_locale

    return templates.TemplateResponse(
        "storefront/content-page.html",
        {
            "request": request,
            "page": page,
            "vendor": vendor,
            "vendor_code": vendor_code,
            "storefront_locale": storefront_locale,
            "storefront_currency": storefront_currency,
        },
    )
