from pyuspostalservice.endpoints.base.base_endpoint import USPSEndpoint
from pyuspostalservice.interfaces import (
    IPostable,
    IPaginateable,
)
from pyuspostalservice.models.usps import Answer
from pyuspostalservice.responses.paginated_response import PaginatedResponse
from pyuspostalservice.types import (
    JSON,
    USPSRequestParams,
)


class AnswersSearchEndpoint(
    USPSEndpoint,
    IPostable[Answer, USPSRequestParams],
    IPaginateable[Answer, USPSRequestParams],

):
    def __init__(self, client, parent_endpoint=None) -> None:
        USPSEndpoint.__init__(self, client, "search", parent_endpoint=parent_endpoint)
        IPostable.__init__(self, Answer)
        IPaginateable.__init__(self, Answer)

    def paginated(
        self,
        page: int,
        params: USPSRequestParams | None = None,
        data: JSON | None = None,
    ) -> PaginatedResponse[Answer]:
        """
        Performs a POST request against the /answers/search endpoint and returns an initialized PaginatedResponse object.

        Parameters:
            page (int): The page number to request.
            params (dict[str, int | str]): The parameters to send in the request query string.
        Returns:
            PaginatedResponse[Answer]: The initialized PaginatedResponse object.
        """
        if params:
            params["page"] = page
        else:
            params = {"page": page}
        return PaginatedResponse(
            super()._make_request("POST", data=data, params=params),
            Answer,
            self,
            "answers",
            page,
            params,
            data,
        )

    def post(self, data: JSON | None = None, params: USPSRequestParams | None = None) -> Answer:
        """
        Performs a POST request against the /answers/search endpoint.

        Parameters:
            data (dict[str, Any]): The data to send in the request body.
            params (dict[str, int | str]): The parameters to send in the request query string.
        Returns:
            Survey: The parsed response data.
        """
        return self._parse_many(Answer, super()._make_request("POST", data=data, params=params).json().get('answers', {}))
