"""TLCS Router"""

from typing import Optional, List
from fastapi import APIRouter, HTTPException, Query, Depends
from core.security import get_current_user
from .service import TLCSService
from .schemas import TLCSResponse

router = APIRouter()


@router.get("/", response_model=List[TLCSResponse])
async def search_tlcs(
    fraccion: Optional[str] = Query(None, description="Fracción arancelaria"),
    pais: Optional[str] = Query(None, description="Código de país"),
    nico: Optional[str] = Query(None, description="NICO"),
    skip: int = Query(0, ge=0, description="Registros a saltar"),
    limit: int = Query(100, ge=1, le=1000, description="Máximo de registros"),
    current_user: dict = Depends(get_current_user),
):
    """Search TLCS records"""
    try:
        service = TLCSService.get_instance()
        return await service.search(
            fraccion=fraccion, pais=pais, nico=nico, skip=skip, limit=limit
        )
    except Exception as e:
        raise HTTPException(
            status_code=500, detail=f"Error fetching TLCS data: {str(e)}"
        )


@router.get("/{sysid}/{fraccion}", response_model=TLCSResponse)
async def get_tlcs_by_id(
    sysid: int,
    fraccion: str,
    current_user: dict = Depends(get_current_user),
):
    """Get single TLCS record by SYSID and FRACCION"""
    try:
        service = TLCSService.get_instance()
        return await service.get_by_id(sysid, fraccion)
    except Exception as e:
        raise HTTPException(status_code=404, detail=f"TLCS record not found: {str(e)}")
