"""Receipt service for YooKassa API."""

from __future__ import annotations

import uuid
from typing import Any

from async_yookassa.models.receipt_request import ReceiptRequest
from async_yookassa.models.receipt_response import ReceiptListResponse, ReceiptResponse
from async_yookassa.services.base import BaseService


class ReceiptService(BaseService):
    """Сервис для работы с чеками."""

    BASE_PATH = "/receipts"

    async def find_one(self, receipt_id: str) -> ReceiptResponse:
        """
        Возвращает информацию о чеке.

        :param receipt_id: Уникальный идентификатор чека
        :return: ReceiptResponse
        """
        if not isinstance(receipt_id, str):
            raise ValueError("Invalid receipt_id value")

        response = await self._get(f"{self.BASE_PATH}/{receipt_id}")
        return ReceiptResponse(**response)

    async def create(
        self,
        params: dict[str, Any] | ReceiptRequest,
        idempotency_key: uuid.UUID | None = None,
    ) -> ReceiptResponse:
        """
        Создание чека.

        :param params: Данные чека
        :param idempotency_key: Ключ идемпотентности
        :return: ReceiptResponse
        """
        if isinstance(params, dict):
            request = ReceiptRequest(**params)
        elif isinstance(params, ReceiptRequest):
            request = params
        else:
            raise TypeError("Invalid params value type")

        body = self._serialize_request(request)

        response = await self._post(
            self.BASE_PATH,
            body=body,
            idempotency_key=idempotency_key,
        )
        return ReceiptResponse(**response)

    async def list(
        self,
        params: dict[str, str] | None = None,
    ) -> ReceiptListResponse:
        """
        Возвращает список чеков.

        :param params: Параметры фильтрации
        :return: ReceiptListResponse
        """
        response = await self._get(self.BASE_PATH, query_params=params)
        return ReceiptListResponse(**response)
