"""REST API helpers for the terminal client."""

from __future__ import annotations

import httpx

from clients.terminal.config import settings


def _client() -> httpx.Client:
    return httpx.Client(base_url=settings.base_url, timeout=30.0)


def get_profiles() -> list[dict]:
    with _client() as client:
        resp = client.get("/agents/profiles")
        resp.raise_for_status()
        return resp.json()


def list_sessions() -> list[dict]:
    with _client() as client:
        resp = client.get("/sessions")
        resp.raise_for_status()
        return resp.json()


def create_session(profile_id: str | None = None) -> dict:
    body: dict = {}
    if profile_id:
        body["profile_id"] = profile_id
    with _client() as client:
        resp = client.post("/sessions", json=body)
        resp.raise_for_status()
        return resp.json()


def get_session(session_id: str) -> dict:
    with _client() as client:
        resp = client.get(f"/sessions/{session_id}")
        resp.raise_for_status()
        return resp.json()


def stop_session(session_id: str) -> dict:
    with _client() as client:
        resp = client.post(f"/sessions/{session_id}/stop")
        resp.raise_for_status()
        return resp.json()
