# app/routes/admin_pages.py
"""
Admin HTML page routes using Jinja2 templates.

These routes return rendered HTML pages (response_class=HTMLResponse).
Separate from admin API routes which return JSON data.

All routes require admin authentication except /login.
Authentication failures redirect to /admin/login.

Routes:
- GET /              → Redirect to /admin/login
- GET /login         → Admin login page (no auth)
- GET /dashboard     → Admin dashboard (auth required)
- GET /companies     → Company list page (auth required)
- GET /companies/create → Create company form (auth required)
- GET /companies/{company_id}/edit → Edit company form (auth required)
- GET /vendors       → Vendor list page (auth required)
- GET /vendors/create → Create vendor form (auth required)
- GET /vendors/{vendor_code} → Vendor details (auth required)
- GET /vendors/{vendor_code}/edit → Edit vendor form (auth required)
- GET /vendors/{vendor_code}/domains → Vendor domains management (auth required)
- GET /vendor-themes → Vendor themes selection page (auth required)
- GET /vendors/{vendor_code}/theme → Vendor theme editor (auth required)
- GET /users         → User management page (auth required)
- GET /customers     → Customer management page (auth required)
- GET /inventory     → Inventory management page (auth required)
- GET /orders        → Orders management page (auth required)
- GET /imports       → Import history page (auth required)
- GET /marketplace-products → Marketplace products catalog (auth required)
- GET /vendor-products → Vendor products catalog (auth required)
- GET /settings      → Settings page (auth required)
- GET /platform-homepage → Platform homepage manager (auth required)
- GET /content-pages → Content pages list (auth required)
- GET /content-pages/create → Create content page (auth required)
- GET /content-pages/{page_id}/edit → Edit content page (auth required)
- GET /code-quality → Code quality dashboard (auth required)
- GET /code-quality/violations → Violations list (auth required)
- GET /code-quality/violations/{violation_id} → Violation details (auth required)
"""

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

from app.api.deps import (
    get_current_admin_from_cookie_or_header,
    get_current_admin_optional,
    get_db,
)
from models.database.user import User

router = APIRouter()
templates = Jinja2Templates(directory="app/templates")


# ============================================================================
# PUBLIC ROUTES (No Authentication Required)
# ============================================================================


@router.get("/", response_class=RedirectResponse, include_in_schema=False)
async def admin_root(
    current_user: User | None = Depends(get_current_admin_optional),
):
    """
    Redirect /admin/ based on authentication status.

    - Authenticated admin users → /admin/dashboard
    - Unauthenticated users → /admin/login
    """
    if current_user:
        # User is already logged in as admin, redirect to dashboard
        return RedirectResponse(url="/admin/dashboard", status_code=302)

    return RedirectResponse(url="/admin/login", status_code=302)


@router.get("/login", response_class=HTMLResponse, include_in_schema=False)
async def admin_login_page(
    request: Request, current_user: User | None = Depends(get_current_admin_optional)
):
    """
    Render admin login page.

    If user is already authenticated as admin, redirect to dashboard.
    Otherwise, show login form.
    """
    if current_user:
        # User is already logged in as admin, redirect to dashboard
        return RedirectResponse(url="/admin/dashboard", status_code=302)

    return templates.TemplateResponse("admin/login.html", {"request": request})


# ============================================================================
# AUTHENTICATED ROUTES (Admin Only)
# ============================================================================


@router.get("/dashboard", response_class=HTMLResponse, include_in_schema=False)
async def admin_dashboard_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render admin dashboard page.
    Shows platform statistics and recent activity.
    """
    return templates.TemplateResponse(
        "admin/dashboard.html",
        {
            "request": request,
            "user": current_user,
        },
    )


# ============================================================================
# COMPANY MANAGEMENT ROUTES
# ============================================================================


@router.get("/companies", response_class=HTMLResponse, include_in_schema=False)
async def admin_companies_list_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render companies management page.
    Shows list of all companies with stats.
    """
    return templates.TemplateResponse(
        "admin/companies.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get("/companies/create", response_class=HTMLResponse, include_in_schema=False)
async def admin_company_create_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render company creation form.
    """
    return templates.TemplateResponse(
        "admin/company-create.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get(
    "/companies/{company_id}", response_class=HTMLResponse, include_in_schema=False
)
async def admin_company_detail_page(
    request: Request,
    company_id: int = Path(..., description="Company ID"),
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render company detail view.
    """
    return templates.TemplateResponse(
        "admin/company-detail.html",
        {
            "request": request,
            "user": current_user,
            "company_id": company_id,
        },
    )


@router.get(
    "/companies/{company_id}/edit", response_class=HTMLResponse, include_in_schema=False
)
async def admin_company_edit_page(
    request: Request,
    company_id: int = Path(..., description="Company ID"),
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render company edit form.
    """
    return templates.TemplateResponse(
        "admin/company-edit.html",
        {
            "request": request,
            "user": current_user,
            "company_id": company_id,
        },
    )


# ============================================================================
# VENDOR MANAGEMENT ROUTES
# ============================================================================


@router.get("/vendors", response_class=HTMLResponse, include_in_schema=False)
async def admin_vendors_list_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render vendors management page.
    Shows list of all vendors with stats.
    """
    return templates.TemplateResponse(
        "admin/vendors.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get("/vendors/create", response_class=HTMLResponse, include_in_schema=False)
async def admin_vendor_create_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render vendor creation form.
    """
    return templates.TemplateResponse(
        "admin/vendor-create.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get(
    "/vendors/{vendor_code}", response_class=HTMLResponse, include_in_schema=False
)
async def admin_vendor_detail_page(
    request: Request,
    vendor_code: str = Path(..., description="Vendor code"),
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render vendor detail page.
    Shows full vendor information.
    """
    return templates.TemplateResponse(
        "admin/vendor-detail.html",
        {
            "request": request,
            "user": current_user,
            "vendor_code": vendor_code,
        },
    )


@router.get(
    "/vendors/{vendor_code}/edit", response_class=HTMLResponse, include_in_schema=False
)
async def admin_vendor_edit_page(
    request: Request,
    vendor_code: str = Path(..., description="Vendor code"),
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render vendor edit form.
    """
    return templates.TemplateResponse(
        "admin/vendor-edit.html",
        {
            "request": request,
            "user": current_user,
            "vendor_code": vendor_code,
        },
    )


# ============================================================================
# VENDOR DOMAINS ROUTES
# ============================================================================


@router.get(
    "/vendors/{vendor_code}/domains",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_vendor_domains_page(
    request: Request,
    vendor_code: str = Path(..., description="Vendor code"),
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render vendor domains management page.
    Shows custom domains, verification status, and DNS configuration.
    """
    return templates.TemplateResponse(
        "admin/vendor-domains.html",
        {
            "request": request,
            "user": current_user,
            "vendor_code": vendor_code,
        },
    )


# ============================================================================
# VENDOR THEMES ROUTES
# ============================================================================


@router.get("/vendor-themes", response_class=HTMLResponse, include_in_schema=False)
async def admin_vendor_themes_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render vendor themes selection page.
    Allows admins to select a vendor to customize their theme.
    """
    return templates.TemplateResponse(
        "admin/vendor-themes.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get(
    "/vendors/{vendor_code}/theme", response_class=HTMLResponse, include_in_schema=False
)
async def admin_vendor_theme_page(
    request: Request,
    vendor_code: str = Path(..., description="Vendor code"),
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render vendor theme customization page.
    Allows admins to customize colors, fonts, layout, and branding.
    """
    return templates.TemplateResponse(
        "admin/vendor-theme.html",
        {
            "request": request,
            "user": current_user,
            "vendor_code": vendor_code,
        },
    )


# ============================================================================
# USER MANAGEMENT ROUTES
# ============================================================================


@router.get("/users", response_class=HTMLResponse, include_in_schema=False)
async def admin_users_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render users management page.
    Shows list of all platform users.
    """
    return templates.TemplateResponse(
        "admin/users.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get("/users/create", response_class=HTMLResponse, include_in_schema=False)
async def admin_user_create_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render user creation form.
    """
    return templates.TemplateResponse(
        "admin/user-create.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get("/users/{user_id}", response_class=HTMLResponse, include_in_schema=False)
async def admin_user_detail_page(
    request: Request,
    user_id: int = Path(..., description="User ID"),
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render user detail view.
    """
    return templates.TemplateResponse(
        "admin/user-detail.html",
        {
            "request": request,
            "user": current_user,
            "user_id": user_id,
        },
    )


@router.get(
    "/users/{user_id}/edit", response_class=HTMLResponse, include_in_schema=False
)
async def admin_user_edit_page(
    request: Request,
    user_id: int = Path(..., description="User ID"),
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render user edit form.
    """
    return templates.TemplateResponse(
        "admin/user-edit.html",
        {
            "request": request,
            "user": current_user,
            "user_id": user_id,
        },
    )


# ============================================================================
# CUSTOMER MANAGEMENT ROUTES
# ============================================================================


@router.get("/customers", response_class=HTMLResponse, include_in_schema=False)
async def admin_customers_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render customers management page.
    Shows list of all platform customers.
    """
    return templates.TemplateResponse(
        "admin/customers.html",
        {
            "request": request,
            "user": current_user,
        },
    )


# ============================================================================
# INVENTORY MANAGEMENT ROUTES
# ============================================================================


@router.get("/inventory", response_class=HTMLResponse, include_in_schema=False)
async def admin_inventory_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render inventory management page.
    Shows stock levels across all vendors with filtering and adjustment capabilities.
    """
    return templates.TemplateResponse(
        "admin/inventory.html",
        {
            "request": request,
            "user": current_user,
        },
    )


# ============================================================================
# ORDER MANAGEMENT ROUTES
# ============================================================================


@router.get("/orders", response_class=HTMLResponse, include_in_schema=False)
async def admin_orders_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render orders management page.
    Shows orders across all vendors with filtering and status management.
    """
    return templates.TemplateResponse(
        "admin/orders.html",
        {
            "request": request,
            "user": current_user,
        },
    )


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


@router.get("/imports", response_class=HTMLResponse, include_in_schema=False)
async def admin_imports_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render imports management page.
    Shows import history and status.
    """
    return templates.TemplateResponse(
        "admin/imports.html",
        {
            "request": request,
            "user": 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(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render background tasks monitoring page.
    Shows running and completed background tasks across the system.
    """
    return templates.TemplateResponse(
        "admin/background-tasks.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get("/marketplace", response_class=HTMLResponse, include_in_schema=False)
async def admin_marketplace_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render marketplace import management page.
    Allows admins to import products for any vendor and monitor all imports.
    """
    return templates.TemplateResponse(
        "admin/marketplace.html",
        {
            "request": request,
            "user": 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(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render unified Letzshop management page.
    Combines products (import/export), orders, and settings management.
    Admin can select a vendor and manage their Letzshop integration.
    """
    return templates.TemplateResponse(
        "admin/marketplace-letzshop.html",
        {
            "request": request,
            "user": 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(get_current_admin_from_cookie_or_header),
    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(
        "admin/letzshop-order-detail.html",
        {
            "request": request,
            "user": current_user,
            "order_id": order_id,
        },
    )


# ============================================================================
# PRODUCT CATALOG ROUTES
# ============================================================================


@router.get(
    "/marketplace-products", response_class=HTMLResponse, include_in_schema=False
)
async def admin_marketplace_products_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render marketplace products page.
    Browse the master product repository imported from external sources.
    """
    return templates.TemplateResponse(
        "admin/marketplace-products.html",
        {
            "request": request,
            "user": 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(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render marketplace product detail page.
    Shows full product information from the master repository.
    """
    return templates.TemplateResponse(
        "admin/marketplace-product-detail.html",
        {
            "request": request,
            "user": current_user,
            "product_id": product_id,
        },
    )


@router.get("/vendor-products", response_class=HTMLResponse, include_in_schema=False)
async def admin_vendor_products_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render vendor products catalog page.
    Browse vendor-specific product catalogs with override capability.
    """
    return templates.TemplateResponse(
        "admin/vendor-products.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get(
    "/vendor-products/{product_id}",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_vendor_product_detail_page(
    request: Request,
    product_id: int = Path(..., description="Vendor Product ID"),
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render vendor product detail page.
    Shows full product information with vendor-specific overrides.
    """
    return templates.TemplateResponse(
        "admin/vendor-product-detail.html",
        {
            "request": request,
            "user": current_user,
            "product_id": product_id,
        },
    )


# ============================================================================
# SETTINGS ROUTES
# ============================================================================


@router.get("/settings", response_class=HTMLResponse, include_in_schema=False)
async def admin_settings_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render admin settings page.
    Platform configuration and preferences.
    """
    return templates.TemplateResponse(
        "admin/settings.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get("/logs", response_class=HTMLResponse, include_in_schema=False)
async def admin_logs_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render admin logs viewer page.
    View database and file logs with filtering and search.
    """
    return templates.TemplateResponse(
        "admin/logs.html",
        {
            "request": request,
            "user": current_user,
        },
    )


# ============================================================================
# CONTENT MANAGEMENT SYSTEM (CMS) ROUTES
# ============================================================================


@router.get("/platform-homepage", response_class=HTMLResponse, include_in_schema=False)
async def admin_platform_homepage_manager(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render platform homepage manager.
    Allows editing the main platform homepage with template selection.
    """
    return templates.TemplateResponse(
        "admin/platform-homepage.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get("/content-pages", response_class=HTMLResponse, include_in_schema=False)
async def admin_content_pages_list(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render content pages list.
    Shows all platform defaults and vendor overrides with filtering.
    """
    return templates.TemplateResponse(
        "admin/content-pages.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get(
    "/content-pages/create", response_class=HTMLResponse, include_in_schema=False
)
async def admin_content_page_create(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render create content page form.
    Allows creating new platform defaults or vendor-specific pages.
    """
    return templates.TemplateResponse(
        "admin/content-page-edit.html",
        {
            "request": request,
            "user": current_user,
            "page_id": None,  # Indicates this is a create operation
        },
    )


@router.get(
    "/content-pages/{page_id}/edit",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_content_page_edit(
    request: Request,
    page_id: int = Path(..., description="Content page ID"),
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render edit content page form.
    Allows editing existing platform or vendor content pages.
    """
    return templates.TemplateResponse(
        "admin/content-page-edit.html",
        {
            "request": request,
            "user": current_user,
            "page_id": page_id,
        },
    )


# ============================================================================
# DEVELOPER TOOLS - COMPONENTS & TESTING
# ============================================================================


@router.get("/components", response_class=HTMLResponse, include_in_schema=False)
async def admin_components_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render UI components library page.
    Reference for all available UI components.
    """
    return templates.TemplateResponse(
        "admin/components.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get("/icons", response_class=HTMLResponse, include_in_schema=False)
async def admin_icons_page(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render icons browser page.
    Browse and search all available icons.
    """
    return templates.TemplateResponse(
        "admin/icons.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get("/testing", response_class=HTMLResponse, include_in_schema=False)
async def admin_testing_dashboard(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render testing dashboard page.
    pytest results and test coverage overview.
    """
    return templates.TemplateResponse(
        "admin/testing-dashboard.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get("/testing-hub", response_class=HTMLResponse, include_in_schema=False)
async def admin_testing_hub(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render manual testing hub page.
    Central hub for all manual test suites and QA tools.
    """
    return templates.TemplateResponse(
        "admin/testing-hub.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get("/test/auth-flow", response_class=HTMLResponse, include_in_schema=False)
async def admin_test_auth_flow(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render authentication flow testing page.
    Tests login, logout, token expiration, and protected routes.
    """
    return templates.TemplateResponse(
        "admin/test-auth-flow.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get(
    "/test/vendors-users-migration",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_test_vendors_users_migration(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render vendors and users migration testing page.
    Tests CRUD operations, data migration, and form validation.
    """
    return templates.TemplateResponse(
        "admin/test-vendors-users-migration.html",
        {
            "request": request,
            "user": current_user,
        },
    )


# ============================================================================
# CODE QUALITY & ARCHITECTURE ROUTES
# ============================================================================


@router.get("/code-quality", response_class=HTMLResponse, include_in_schema=False)
async def admin_code_quality_dashboard(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render code quality dashboard.
    Shows architecture violations, trends, and technical debt score.
    """
    return templates.TemplateResponse(
        "admin/code-quality-dashboard.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get(
    "/code-quality/violations", response_class=HTMLResponse, include_in_schema=False
)
async def admin_code_quality_violations(
    request: Request,
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render violations list page.
    Shows all violations with filtering and sorting options.
    """
    return templates.TemplateResponse(
        "admin/code-quality-violations.html",
        {
            "request": request,
            "user": current_user,
        },
    )


@router.get(
    "/code-quality/violations/{violation_id}",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_code_quality_violation_detail(
    request: Request,
    violation_id: int = Path(..., description="Violation ID"),
    current_user: User = Depends(get_current_admin_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render violation detail page.
    Shows violation details, code context, assignments, and comments.
    """
    return templates.TemplateResponse(
        "admin/code-quality-violation-detail.html",
        {
            "request": request,
            "user": current_user,
            "violation_id": violation_id,
        },
    )
