from __future__ import annotations

from typing import TYPE_CHECKING

from ..objects.session import Session
from ..objects.list_response import ListResponse

if TYPE_CHECKING:
    from ..client import WhatsrbCloud
    from ..connection import Connection


class Sessions:
    def __init__(self, *, client: WhatsrbCloud, connection: Connection):
        self._client = client
        self._connection = connection

    def list(self) -> ListResponse[Session]:
        response = self._connection.get("/sessions")
        data = [Session(s, client=self._client) for s in (response.get("data") or [])]
        return ListResponse(data=data, meta=response.get("meta", {}))

    def create(self, **params) -> Session:
        response = self._connection.post("/sessions", {"session": params})
        return Session(response["data"], client=self._client)

    def retrieve(self, id: str) -> Session:
        response = self._connection.get(f"/sessions/{id}")
        return Session(response["data"], client=self._client)

    def delete(self, id: str) -> bool:
        self._connection.delete(f"/sessions/{id}")
        return True
