"""Ürün + kategori router — panel (JWT). Thin handler'lar.

Route sırası önemli: /categories ve /import, /{id} catch-all'dan ÖNCE gelir.
"""
from fastapi import APIRouter, Depends, Query

from app.auth.dependencies import get_current_tenant
from app.models.tenant import Tenant
from app.schemas.product import (
    CreateCategoryRequest,
    CreateProductRequest,
    UpdateCategoryRequest,
    UpdateProductRequest,
)
from app.services.category_service import CategoryService
from app.services.product_service import ProductService
from app.utils.responses import created, no_content, ok

router = APIRouter(prefix="/api/products", tags=["products"])

# ============================================================
# CATEGORIES  (/{id} catch-all'dan önce tanımlanmalı)
# ============================================================


@router.get("/categories")
async def list_categories(
    tenant: Tenant = Depends(get_current_tenant),
    service: CategoryService = Depends(),
):
    return ok(await service.list_categories(tenant.id))


@router.post("/categories")
async def create_category(
    body: CreateCategoryRequest,
    tenant: Tenant = Depends(get_current_tenant),
    service: CategoryService = Depends(),
):
    result = await service.create_category(tenant.id, body)
    return created(result.model_dump(mode="json"))


@router.put("/categories/{category_id}")
async def update_category(
    category_id: str,
    body: UpdateCategoryRequest,
    tenant: Tenant = Depends(get_current_tenant),
    service: CategoryService = Depends(),
):
    result = await service.update_category(category_id, tenant.id, body)
    return ok(result.model_dump(mode="json"))


@router.delete("/categories/{category_id}")
async def delete_category(
    category_id: str,
    tenant: Tenant = Depends(get_current_tenant),
    service: CategoryService = Depends(),
):
    await service.delete_category(category_id, tenant.id)
    return no_content()


# ============================================================
# PRODUCTS
# ============================================================


@router.get("")
async def list_products(
    limit: int = Query(default=20, ge=1, le=100),
    cursor: str | None = Query(default=None),
    tenant: Tenant = Depends(get_current_tenant),
    service: ProductService = Depends(),
):
    page = await service.list_products(tenant.id, limit=limit, cursor=cursor)
    return ok(page.items, meta=page.meta())


@router.post("")
async def create_product(
    body: CreateProductRequest,
    tenant: Tenant = Depends(get_current_tenant),
    service: ProductService = Depends(),
):
    result = await service.create_product(tenant.id, body)
    return created(result.model_dump(mode="json"))


@router.get("/{product_id}")
async def get_product(
    product_id: str,
    tenant: Tenant = Depends(get_current_tenant),
    service: ProductService = Depends(),
):
    result = await service.get_product(product_id, tenant.id)
    return ok(result.model_dump(mode="json"))


@router.put("/{product_id}")
async def update_product(
    product_id: str,
    body: UpdateProductRequest,
    tenant: Tenant = Depends(get_current_tenant),
    service: ProductService = Depends(),
):
    result = await service.update_product(product_id, tenant.id, body)
    return ok(result.model_dump(mode="json"))


@router.delete("/{product_id}")
async def delete_product(
    product_id: str,
    tenant: Tenant = Depends(get_current_tenant),
    service: ProductService = Depends(),
):
    await service.delete_product(product_id, tenant.id)
    return no_content()


@router.post("/{product_id}/reindex")
async def reindex_product(
    product_id: str,
    tenant: Tenant = Depends(get_current_tenant),
    service: ProductService = Depends(),
):
    result = await service.reindex_product(product_id, tenant.id)
    return ok(result.model_dump(mode="json"))
