"""Domains resource."""

from __future__ import annotations

from .._client import AplikuClient
from .._models import Domain, DomainCreate, PaginatedResponse


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

    def _base(self, team: str, app_id: int) -> str:
        return f"/api/team/{team}/applications/{app_id}/domains"

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

    def get(self, team: str, app_id: int, domain_id: int) -> Domain:
        """Get a single domain."""
        data = self._client.get(f"{self._base(team, app_id)}/{domain_id}/")
        return Domain.model_validate(data)

    def create(self, team: str, app_id: int, domain: str) -> Domain:
        """Create a domain."""
        body = DomainCreate(domain=domain)
        data = self._client.post(
            f"{self._base(team, app_id)}/",
            json=body.model_dump(),
        )
        return Domain.model_validate(data)

    def delete(self, team: str, app_id: int, domain_id: int) -> None:
        """Delete a domain."""
        self._client.delete(f"{self._base(team, app_id)}/{domain_id}/")
