"""Volumes resource."""

from __future__ import annotations

from .._client import AplikuClient
from .._models import PaginatedResponse, Volume, VolumeCreate, VolumeUpdate


class VolumesResource:
    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}/volumes"

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

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

    def create(
        self, team: str, app_id: int, name: str, container_path: str, **kwargs: object
    ) -> Volume:
        """Create a volume."""
        body = VolumeCreate(name=name, container_path=container_path, **kwargs)  # type: ignore[arg-type]
        data = self._client.post(
            f"{self._base(team, app_id)}/",
            json=body.model_dump(exclude_none=True),
        )
        return Volume.model_validate(data)

    def update(self, team: str, app_id: int, vol_id: int, **kwargs: object) -> Volume:
        """Partially update a volume."""
        body = VolumeUpdate(**kwargs)  # type: ignore[arg-type]
        data = self._client.patch(
            f"{self._base(team, app_id)}/{vol_id}/",
            json=body.model_dump(exclude_none=True),
        )
        return Volume.model_validate(data)

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