"""Fundamentos TLC Service"""

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


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

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

    async def search(
        self,
        fraccion: Optional[str] = None,
        nico: Optional[str] = None,
        tipat_only: bool = False,
        skip: int = 0,
        limit: int = 100,
    ) -> List[FundamentosTLCResponse]:
        params = {"skip": skip, "limit": min(limit, 1000)}
        if fraccion:
            params["fraccion"] = fraccion
        if nico:
            params["nico"] = nico
        endpoint = (
            "/api/v1/fundamentos-tlc/tipat"
            if tipat_only
            else "/api/v1/fundamentos-tlc/"
        )
        data = await self._make_request("GET", endpoint, params=params)
        return [FundamentosTLCResponse(**item) for item in data]

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