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

Vendor pages for marketplace management:
- Onboarding wizard
- Dashboard
- Marketplace imports
- Letzshop integration
"""

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

from app.api.deps import get_current_vendor_from_cookie_or_header, get_db
from app.modules.core.utils.page_context import get_vendor_context
from app.modules.marketplace.services.onboarding_service import OnboardingService
from app.templates_config import templates
from app.modules.tenancy.models import User

router = APIRouter()


# ============================================================================
# ONBOARDING WIZARD
# ============================================================================


@router.get(
    "/{vendor_code}/onboarding", response_class=HTMLResponse, include_in_schema=False
)
async def vendor_onboarding_page(
    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 vendor onboarding wizard.

    Mandatory 4-step wizard that must be completed before accessing dashboard:
    1. Company Profile Setup
    2. Letzshop API Configuration
    3. Product & Order Import Configuration
    4. Order Sync (historical import)

    If onboarding is already completed, redirects to dashboard.
    """
    onboarding_service = OnboardingService(db)
    if onboarding_service.is_completed(current_user.token_vendor_id):
        return RedirectResponse(
            url=f"/vendor/{vendor_code}/dashboard",
            status_code=302,
        )

    return templates.TemplateResponse(
        "marketplace/vendor/onboarding.html",
        get_vendor_context(request, db, current_user, vendor_code),
    )


# ============================================================================
# VENDOR DASHBOARD
# ============================================================================


@router.get(
    "/{vendor_code}/dashboard", response_class=HTMLResponse, include_in_schema=False
)
async def vendor_dashboard_page(
    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 vendor dashboard.

    Redirects to onboarding if not completed.

    JavaScript will:
    - Load vendor info via API
    - Load dashboard stats via API
    - Load recent orders via API
    - Handle all interactivity
    """
    onboarding_service = OnboardingService(db)
    if not onboarding_service.is_completed(current_user.token_vendor_id):
        return RedirectResponse(
            url=f"/vendor/{vendor_code}/onboarding",
            status_code=302,
        )

    return templates.TemplateResponse(
        "core/vendor/dashboard.html",
        get_vendor_context(request, db, current_user, vendor_code),
    )


# ============================================================================
# MARKETPLACE IMPORTS
# ============================================================================


@router.get(
    "/{vendor_code}/marketplace", response_class=HTMLResponse, include_in_schema=False
)
async def vendor_marketplace_page(
    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 marketplace import page.
    JavaScript loads import jobs and products via API.
    """
    return templates.TemplateResponse(
        "marketplace/vendor/marketplace.html",
        get_vendor_context(request, db, current_user, vendor_code),
    )


# ============================================================================
# LETZSHOP INTEGRATION
# ============================================================================


@router.get(
    "/{vendor_code}/letzshop", response_class=HTMLResponse, include_in_schema=False
)
async def vendor_letzshop_page(
    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 Letzshop integration page.
    JavaScript loads orders, credentials status, and handles fulfillment operations.
    """
    return templates.TemplateResponse(
        "marketplace/vendor/letzshop.html",
        get_vendor_context(request, db, current_user, vendor_code),
    )
