"""FraccionesAnteriores Router"""

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

router = APIRouter()


@router.get("/", response_model=List[FraccionesAnterioresResponse])
async def search(
    fraccion_actual: Optional[str] = Query(None),
    fraccion_anterior: 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 = FraccionesAnterioresService.get_instance()
        return await service.search(
            fraccion_actual=fraccion_actual,
            fraccion_anterior=fraccion_anterior,
            skip=skip,
            limit=limit,
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


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