"""Noms Service"""

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


class NomsService(SitarAPIBaseService):
    _instance: Optional["NomsService"] = None

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

    async def search(
        self,
        fraccion: Optional[str] = None,
        pais: Optional[str] = None,
        nico: Optional[str] = None,
        skip: int = 0,
        limit: int = 100,
    ) -> List[NomsResponse]:
        params = {"skip": skip, "limit": min(limit, 1000)}
        if fraccion:
            params["fraccion"] = fraccion
        if pais:
            params["pais"] = pais
        if nico:
            params["nico"] = nico
        data = await self._make_request("GET", "/api/v1/noms/", params=params)
        return [NomsResponse(**item) for item in data]

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