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

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

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

from app.api.deps import (
    get_current_store_from_cookie_or_header,
    get_db,
    get_resolved_store_code,
)
from app.modules.core.utils.page_context import get_store_context
from app.modules.marketplace.services.onboarding_service import OnboardingService
from app.modules.service import module_service
from app.modules.tenancy.models import User
from app.modules.tenancy.models.store_platform import StorePlatform
from app.templates_config import templates

router = APIRouter()


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


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

    4-step wizard for marketplace setup:
    1. Merchant Profile Setup
    2. Letzshop API Configuration
    3. Product & Order Import Configuration
    4. Order Sync (historical import)

    If marketplace module is not enabled for this store's platform,
    redirects to dashboard. If onboarding is already completed,
    redirects to dashboard.
    """
    sp = (
        db.query(StorePlatform)
        .filter(StorePlatform.store_id == current_user.token_store_id)
        .first()
    )
    if not sp or not module_service.is_module_enabled(db, sp.platform_id, "marketplace"):
        return RedirectResponse(
            url=f"/store/{store_code}/dashboard",
            status_code=302,
        )

    onboarding_service = OnboardingService(db)
    if onboarding_service.is_completed(current_user.token_store_id):
        return RedirectResponse(
            url=f"/store/{store_code}/dashboard",
            status_code=302,
        )

    return templates.TemplateResponse(
        "marketplace/store/onboarding.html",
        get_store_context(request, db, current_user, store_code),
    )


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


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


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


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