"""TLCS Service"""

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


class TLCSService(SitarAPIBaseService):
    """Service for TLCS operations"""

    _instance: Optional["TLCSService"] = None

    @classmethod
    def get_instance(cls) -> "TLCSService":
        """Get singleton instance"""
        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[TLCSResponse]:
        """
        Search TLCS records

        Args:
            fraccion: Tariff fraction code
            pais: Country code
            nico: NICO code
            skip: Records to skip
            limit: Maximum records to return

        Returns:
            List of TLCS records
        """
        params = {"skip": skip, "limit": min(limit, 1000)}
        if fraccion:
            params["fraccion"] = fraccion[:8]
        if pais:
            params["pais"] = pais
        if nico:
            params["nico"] = nico

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

    async def get_by_id(self, sysid: int, fraccion: str) -> TLCSResponse:
        """Get single TLCS record by SYSID and FRACCION"""
        data = await self._make_request("GET", f"/api/v1/tlcs/{sysid}/{fraccion}")
        return TLCSResponse(**data)
