"""Cuotas2 Service"""

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


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

    @classmethod
    def get_instance(cls) -> "Cuotas2Service":
        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[Cuotas2Response]:
        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/cuotas2/", params=params)
        return [Cuotas2Response(**item) for item in data]

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