"""Slices AI Infrastructure Service Job Debug Logs API."""

from uuid import UUID

from httpx import Client, HTTPError
from msgspec.json import Decoder as JsonDecoder
from slices_clientlib_core.base.exceptions import from_httpx_exception

from .schema.job import Job
from .schema.job_debug_logs import JobDebugLog

_job_debug_logs_decoder = JsonDecoder(list[JobDebugLog])


class AIJobDebugLogsClient:
    """AI Infrastructure Service Job API Debug Logs Client."""

    API_BASE_PATH = "/apis/ai.slices-ri.eu/v1/"

    def __init__(self, client: Client) -> None:
        """Create AI Infrastructure Service Jobs Client."""
        self._client = client

        self._base_path = f"{self.API_BASE_PATH}jobs/"

        assert client.base_url is not None, "Expected base_url to be set"

    def get(self, job: str | UUID | Job) -> list[JobDebugLog]:
        """Get job Debug Log."""
        job_id = job.uuid if isinstance(job, Job) else str(job)
        response = self._client.get(f"{self._base_path}{job_id}/debug/")
        try:
            response.raise_for_status()
        except HTTPError as err:
            raise from_httpx_exception(err) from err
        else:
            return _job_debug_logs_decoder.decode(response.content)
