"""Invites resource."""

from __future__ import annotations

from .._client import AplikuClient
from .._models import Invite, InviteCreate, PaginatedResponse


class InvitesResource:
    def __init__(self, client: AplikuClient) -> None:
        self._client = client

    def _base(self, team: str) -> str:
        return f"/api/team/{team}/invites"

    def list(self, team: str, *, page: int = 1, page_size: int = 20) -> PaginatedResponse[Invite]:
        """List invites for a team."""
        return self._client.paginate(
            f"{self._base(team)}/",
            params={"page": page, "page_size": page_size},
            response_type=Invite,
        )

    def create(self, team: str, email: str, role: str = "member") -> Invite:
        """Create an invite."""
        body = InviteCreate(email=email, role=role)
        data = self._client.post(f"{self._base(team)}/", json=body.model_dump())
        return Invite.model_validate(data)

    def delete(self, team: str, invite_id: int) -> None:
        """Delete/revoke an invite."""
        self._client.delete(f"{self._base(team)}/{invite_id}/delete")

    def accept(self, team: str, invite_id: int, token: str) -> dict[str, object]:
        """Accept an invite using a token."""
        data = self._client.patch(
            f"{self._base(team)}/{invite_id}/guest/{token}/accept",
        )
        return data

    def decline(self, team: str, invite_id: int, token: str) -> dict[str, object]:
        """Decline an invite using a token."""
        data = self._client.patch(
            f"{self._base(team)}/{invite_id}/guest/{token}/decline",
        )
        return data
