"""Fracciones USA Service"""

from typing import Optional, List, Dict, Any
from ..common import SitarAPIBaseService
from .schemas import FraccionesUSAResponse



class FraccionesUSAService(SitarAPIBaseService):
    """Service for USA Fracciones operations"""

    _instance: Optional["FraccionesUSAService"] = None

    @classmethod
    def get_instance(cls) -> "FraccionesUSAService":
        """Get singleton instance"""
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance

    async def search(
        self,
        fraccion: Optional[str] = None,
        descripcion: Optional[str] = None,
        skip: int = 0,
        limit: int = 100,
    ) -> List[FraccionesUSAResponse]:
        """Search USA tariff fractions"""
        params = {"skip": skip, "limit": min(limit, 1000)}
        if fraccion:
            params["fraccion"] = fraccion
        if descripcion:
            params["descripcion"] = descripcion

        data = await self._make_request("GET", "api/v1/fracciones-usa/", params=params)
        return [FraccionesUSAResponse(**item) for item in data]

    async def get_by_id(self, consecutivo: int) -> FraccionesUSAResponse:
        """Get single USA Fraccion record by CONSECUTIVO"""
        data = await self._make_request("GET", f"api/v1/fracciones-usa/{consecutivo}")
        return FraccionesUSAResponse(**data)
