"""Regulaciones Router"""

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

router = APIRouter()


@router.get("/", response_model=List[RegulacionesResponse])
async def search_regulaciones(
    fraccion: Optional[str] = Query(None, description="Fracción arancelaria"),
    nico: Optional[str] = Query(None, description="NICO"),
    skip: int = Query(0, ge=0),
    limit: int = Query(100, ge=1, le=1000),
    current_user: dict = Depends(get_current_user),
):
    try:
        service = RegulacionesService.get_instance()
        return await service.search(
            fraccion=fraccion, nico=nico, skip=skip, limit=limit
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error: {str(e)}")


@router.get("/{sysid}", response_model=RegulacionesResponse)
async def get_regulacion_by_id(
    sysid: int, current_user: dict = Depends(get_current_user)
):
    try:
        service = RegulacionesService.get_instance()
        return await service.get_by_id(sysid)
    except Exception as e:
        raise HTTPException(status_code=404, detail=f"Not found: {str(e)}")
