"""
品牌和供应商API路由
"""
from typing import List, Optional
from fastapi import APIRouter, Depends, HTTPException, status, Query
from sqlalchemy.orm import Session
from app.core.deps import get_db, get_current_user
from app.schemas.brand_supplier import (
    BrandCreate,
    BrandUpdate,
    BrandResponse,
    SupplierCreate,
    SupplierUpdate,
    SupplierResponse
)
from app.services.brand_supplier_service import brand_service, supplier_service

router = APIRouter()


# ===== 品牌管理 =====

@router.get("/brands", response_model=List[BrandResponse])
def get_brands(
    skip: int = Query(0, ge=0, description="跳过条数"),
    limit: int = Query(20, ge=1, le=100, description="返回条数"),
    status: Optional[str] = Query(None, description="状态筛选"),
    keyword: Optional[str] = Query(None, description="搜索关键词"),
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """获取品牌列表"""
    items, total = brand_service.get_brands(db, skip, limit, status, keyword)
    return items


@router.get("/brands/{brand_id}", response_model=BrandResponse)
def get_brand(
    brand_id: int,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """获取品牌详情"""
    return brand_service.get_brand(db, brand_id)


@router.post("/brands", response_model=BrandResponse, status_code=status.HTTP_201_CREATED)
def create_brand(
    obj_in: BrandCreate,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """创建品牌"""
    return brand_service.create_brand(db, obj_in, current_user.id)


@router.put("/brands/{brand_id}", response_model=BrandResponse)
def update_brand(
    brand_id: int,
    obj_in: BrandUpdate,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """更新品牌"""
    return brand_service.update_brand(db, brand_id, obj_in, current_user.id)


@router.delete("/brands/{brand_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_brand(
    brand_id: int,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """删除品牌"""
    brand_service.delete_brand(db, brand_id, current_user.id)
    return None


# ===== 供应商管理 =====

@router.get("/suppliers", response_model=List[SupplierResponse])
def get_suppliers(
    skip: int = Query(0, ge=0, description="跳过条数"),
    limit: int = Query(20, ge=1, le=100, description="返回条数"),
    status: Optional[str] = Query(None, description="状态筛选"),
    keyword: Optional[str] = Query(None, description="搜索关键词"),
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """获取供应商列表"""
    items, total = supplier_service.get_suppliers(db, skip, limit, status, keyword)
    return items


@router.get("/suppliers/{supplier_id}", response_model=SupplierResponse)
def get_supplier(
    supplier_id: int,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """获取供应商详情"""
    return supplier_service.get_supplier(db, supplier_id)


@router.post("/suppliers", response_model=SupplierResponse, status_code=status.HTTP_201_CREATED)
def create_supplier(
    obj_in: SupplierCreate,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """创建供应商"""
    return supplier_service.create_supplier(db, obj_in, current_user.id)


@router.put("/suppliers/{supplier_id}", response_model=SupplierResponse)
def update_supplier(
    supplier_id: int,
    obj_in: SupplierUpdate,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """更新供应商"""
    return supplier_service.update_supplier(db, supplier_id, obj_in, current_user.id)


@router.delete("/suppliers/{supplier_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_supplier(
    supplier_id: int,
    db: Session = Depends(get_db),
    current_user = Depends(get_current_user)
):
    """删除供应商"""
    supplier_service.delete_supplier(db, supplier_id, current_user.id)
    return None
