"""Servers resource."""

from __future__ import annotations

import builtins

from .._client import AplikuClient
from .._models import Server, ServerCreateCustom, ServerCreateDO, ServerCreateEC2

_list = builtins.list


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

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

    def list(self, team: str) -> _list[Server]:
        """List servers for a team."""
        data = self._client.get(self._base(team))
        return [Server.model_validate(item) for item in data]

    def get(self, team: str, server_id: int) -> Server:
        """Get a single server."""
        data = self._client.get(f"{self._base(team)}/{server_id}")
        return Server.model_validate(data)

    def create_digitalocean(
        self, team: str, name: str, region: str, size: str, **kwargs: object
    ) -> Server:
        """Create a DigitalOcean server."""
        body = ServerCreateDO(name=name, region=region, size=size, **kwargs)  # type: ignore[arg-type]
        data = self._client.post(
            f"/api/team/{team}/create_digital_ocean_server/",
            json=body.model_dump(exclude_none=True),
        )
        return Server.model_validate(data)

    def create_ec2(
        self, team: str, name: str, region: str, instance_type: str, **kwargs: object
    ) -> Server:
        """Create an EC2 server."""
        body = ServerCreateEC2(name=name, region=region, instance_type=instance_type, **kwargs)  # type: ignore[arg-type]
        data = self._client.post(
            f"/api/team/{team}/create_new_ec2_instance/",
            json=body.model_dump(exclude_none=True),
        )
        return Server.model_validate(data)

    def create_custom(self, team: str, name: str, host: str, **kwargs: object) -> Server:
        """Create a custom (SSH) server."""
        body = ServerCreateCustom(name=name, host=host, **kwargs)  # type: ignore[arg-type]
        data = self._client.post(
            f"/api/team/{team}/add_custom_server/",
            json=body.model_dump(exclude_none=True),
        )
        return Server.model_validate(data)
