"""PROSEC Router"""

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

router = APIRouter()


@router.get("/", response_model=List[ProsecResponse])
async def search_prosec(
    fraccion: Optional[str] = Query(None),
    nico: Optional[str] = Query(None),
    sector: Optional[str] = Query(None),
    articulo: Optional[str] = Query(None),
    skip: int = Query(0, ge=0),
    limit: int = Query(100, ge=1, le=1000),
    current_user: dict = Depends(get_current_user),
):
    try:
        service = ProsecService.get_instance()
        return await service.search(
            fraccion=fraccion,
            nico=nico,
            sector=sector,
            articulo=articulo,
            skip=skip,
            limit=limit,
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error: {str(e)}")


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