"""
ERP 产品 & 库存模块路由 —— /api/products
薄路由层：参数解析 + 调用 Service + 包装响应
"""
from __future__ import annotations
import uuid
from fastapi import APIRouter, Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession
from app.api.deps import get_current_user
from app.db.database import get_db
from app.schemas.auth import CurrentUserPayload
from app.schemas.erp import CategoryCreate, CategoryUpdate, InventoryFlowCreate, SkuCreate, SkuUpdate
from app.schemas.response import ok
from app.services import product_service as svc

router = APIRouter(prefix="/products", tags=["产品与库存"])


@router.get("/categories/tree", summary="获取产品分类树（嵌套结构）")
async def get_category_tree(
    db: AsyncSession = Depends(get_db),
    _: CurrentUserPayload = Depends(get_current_user),
) -> dict:
    tree = await svc.get_category_tree(db)
    return ok(data=tree)


@router.post("/categories", summary="新增产品分类")
async def create_category(
    body: CategoryCreate,
    db: AsyncSession = Depends(get_db),
    _: CurrentUserPayload = Depends(get_current_user),
) -> dict:
    result = await svc.create_category(db, body)
    return ok(data=result, message="分类创建成功")


@router.put("/categories/{cat_id}", summary="修改产品分类")
async def update_category(
    cat_id: uuid.UUID,
    body: CategoryUpdate,
    db: AsyncSession = Depends(get_db),
    _: CurrentUserPayload = Depends(get_current_user),
) -> dict:
    await svc.update_category(db, cat_id, body)
    return ok(message="分类信息已更新")


@router.delete("/categories/{cat_id}", summary="软删除产品分类")
async def delete_category(
    cat_id: uuid.UUID,
    db: AsyncSession = Depends(get_db),
    _: CurrentUserPayload = Depends(get_current_user),
) -> dict:
    await svc.delete_category(db, cat_id)
    return ok(message="分类已删除")


@router.get("/skus", summary="分页获取产品 SKU 列表")
async def list_skus(
    page: int = Query(1, ge=1),
    size: int = Query(20, ge=1, le=100),
    category_id: uuid.UUID | None = Query(None, description="按分类过滤"),
    keyword: str | None = Query(None, description="模糊搜索 SKU 编码或名称"),
    db: AsyncSession = Depends(get_db),
    _: CurrentUserPayload = Depends(get_current_user),
) -> dict:
    result = await svc.list_skus(db, page, size, category_id, keyword)
    return ok(data=result.model_dump(mode="json"))


@router.post("/skus", summary="新增产品 SKU")
async def create_sku(
    body: SkuCreate,
    db: AsyncSession = Depends(get_db),
    _: CurrentUserPayload = Depends(get_current_user),
) -> dict:
    result = await svc.create_sku(db, body)
    return ok(data=result.model_dump(mode="json"), message="产品创建成功")


@router.put("/skus/{sku_id}", summary="修改产品基础信息（不含库存）")
async def update_sku(
    sku_id: uuid.UUID,
    body: SkuUpdate,
    db: AsyncSession = Depends(get_db),
    _: CurrentUserPayload = Depends(get_current_user),
) -> dict:
    result = await svc.update_sku(db, sku_id, body)
    return ok(data=result.model_dump(mode="json"), message="产品信息已更新")


@router.post("/inventory/flow", summary="库存变更（事务级原子操作）")
async def create_inventory_flow(
    body: InventoryFlowCreate,
    db: AsyncSession = Depends(get_db),
    current_user: CurrentUserPayload = Depends(get_current_user),
) -> dict:
    result = await svc.create_inventory_flow(db, current_user, body)
    return ok(data=result.model_dump(mode="json"), message="库存变更成功")


@router.get("/inventory/flows/{sku_id}", summary="获取单个 SKU 的库存流水（倒序）")
async def get_inventory_flows(
    sku_id: uuid.UUID,
    page: int = Query(1, ge=1),
    size: int = Query(50, ge=1, le=200),
    db: AsyncSession = Depends(get_db),
    _: CurrentUserPayload = Depends(get_current_user),
) -> dict:
    result = await svc.get_inventory_flows(db, sku_id, page, size)
    return ok(data=result)
