"""PROSEC Service"""

from typing import Optional, List
from ..common import SitarAPIBaseService
from .schemas import ProsecResponse


class ProsecService(SitarAPIBaseService):
    """Service for PROSEC operations"""

    _instance: Optional["ProsecService"] = None

    @classmethod
    def get_instance(cls) -> "ProsecService":
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance

    async def search(
        self,
        fraccion: Optional[str] = None,
        nico: Optional[str] = None,
        sector: Optional[str] = None,
        articulo: Optional[str] = None,
        skip: int = 0,
        limit: int = 100,
    ) -> List[ProsecResponse]:
        params = {"skip": skip, "limit": min(limit, 1000)}
        if fraccion:
            params["fraccion"] = fraccion
        if nico:
            params["nico"] = nico
        if sector:
            params["sector"] = sector
        if articulo:
            params["articulo"] = articulo

        data = await self._make_request("GET", "/api/v1/prosec/", params=params)
        return [ProsecResponse(**item) for item in data]

    async def get_by_id(self, sysid: int) -> ProsecResponse:
        data = await self._make_request("GET", f"/api/v1/prosec/{sysid}")
        return ProsecResponse(**data)
