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

Admin pages for marketplace management:
- Import history
- Background tasks
- Marketplace integration
- Letzshop management
- Marketplace products
"""

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

from app.api.deps import get_db, require_menu_access
from app.core.config import settings
from app.modules.core.utils.page_context import get_admin_context
from app.modules.enums import FrontendType
from app.modules.tenancy.models import User
from app.templates_config import templates

router = APIRouter()


# ============================================================================
# IMPORT MANAGEMENT ROUTES
# ============================================================================


@router.get("/imports", response_class=HTMLResponse, include_in_schema=False)
async def admin_imports_page(
    request: Request,
    current_user: User = Depends(require_menu_access("imports", FrontendType.ADMIN)),
    db: Session = Depends(get_db),
):
    """
    Render imports management page.
    Shows import history and status.
    """
    return templates.TemplateResponse(
        "marketplace/admin/imports.html",
        get_admin_context(request, db, current_user),
    )


@router.get("/background-tasks", response_class=HTMLResponse, include_in_schema=False)
async def admin_background_tasks_page(
    request: Request,
    current_user: User = Depends(
        require_menu_access("background-tasks", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render background tasks monitoring page.
    Shows running and completed background tasks across the system.
    """
    return templates.TemplateResponse(
        "marketplace/admin/background-tasks.html",
        get_admin_context(request, db, current_user, flower_url=settings.flower_url),
    )


@router.get("/marketplace", response_class=HTMLResponse, include_in_schema=False)
async def admin_marketplace_page(
    request: Request,
    current_user: User = Depends(
        require_menu_access("marketplace-letzshop", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render marketplace import management page.
    Allows admins to import products for any store and monitor all imports.
    """
    return templates.TemplateResponse(
        "marketplace/admin/marketplace.html",
        get_admin_context(request, db, current_user),
    )


# ============================================================================
# MARKETPLACE INTEGRATION ROUTES
# ============================================================================


@router.get(
    "/marketplace/letzshop", response_class=HTMLResponse, include_in_schema=False
)
async def admin_marketplace_letzshop_page(
    request: Request,
    current_user: User = Depends(
        require_menu_access("marketplace-letzshop", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render unified Letzshop management page.
    Combines products (import/export), orders, and settings management.
    Admin can select a store and manage their Letzshop integration.
    """
    return templates.TemplateResponse(
        "marketplace/admin/marketplace-letzshop.html",
        get_admin_context(request, db, current_user),
    )


@router.get(
    "/letzshop/orders/{order_id}",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_letzshop_order_detail_page(
    request: Request,
    order_id: int = Path(..., description="Letzshop order ID"),
    current_user: User = Depends(
        require_menu_access("marketplace-letzshop", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render detailed Letzshop order page.
    Shows full order information with shipping address, billing address,
    product details, and order history.
    """
    return templates.TemplateResponse(
        "marketplace/admin/letzshop-order-detail.html",
        get_admin_context(request, db, current_user, order_id=order_id),
    )


@router.get(
    "/letzshop/products/{product_id}",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_letzshop_product_detail_page(
    request: Request,
    product_id: int = Path(..., description="Marketplace Product ID"),
    current_user: User = Depends(
        require_menu_access("marketplace-letzshop", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render Letzshop product detail page.
    Shows full product information from the marketplace.
    """
    return templates.TemplateResponse(
        "marketplace/admin/marketplace-product-detail.html",
        get_admin_context(
            request,
            current_user,
            product_id=product_id,
            back_url="/admin/marketplace/letzshop",
        ),
    )


# ============================================================================
# LETZSHOP STORE DIRECTORY
# ============================================================================


@router.get(
    "/letzshop/store-directory",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_letzshop_store_directory_page(
    request: Request,
    current_user: User = Depends(
        require_menu_access("marketplace-letzshop", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render Letzshop store directory management page.

    Allows admins to:
    - View cached Letzshop stores
    - Trigger manual sync from Letzshop API
    - Create platform stores from cached Letzshop stores
    """
    return templates.TemplateResponse(
        "marketplace/admin/letzshop-store-directory.html",
        get_admin_context(request, db, current_user),
    )


# ============================================================================
# MARKETPLACE PRODUCTS ROUTES
# ============================================================================


@router.get(
    "/marketplace-products", response_class=HTMLResponse, include_in_schema=False
)
async def admin_marketplace_products_page(
    request: Request,
    current_user: User = Depends(
        require_menu_access("marketplace-letzshop", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render marketplace products page.
    Browse the master product repository imported from external sources.
    """
    return templates.TemplateResponse(
        "marketplace/admin/marketplace-products.html",
        get_admin_context(request, db, current_user),
    )


@router.get(
    "/marketplace-products/{product_id}",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_marketplace_product_detail_page(
    request: Request,
    product_id: int = Path(..., description="Marketplace Product ID"),
    current_user: User = Depends(
        require_menu_access("marketplace-letzshop", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render marketplace product detail page.
    Shows full product information from the master repository.
    """
    return templates.TemplateResponse(
        "marketplace/admin/marketplace-product-detail.html",
        get_admin_context(
            request,
            current_user,
            product_id=product_id,
            back_url="/admin/marketplace-products",
        ),
    )
