"""AI CLI."""

from dataclasses import dataclass
from functools import cached_property

import typer

from . import err_console
from .context import ContextObj


@dataclass
class AIContextObj(ContextObj):
    """AI Context object."""

    site_id: str

    def _require_project_id(self):
        project_id = self.get_current_project_id()
        if not project_id:
            err_console.print("[bold red]No project set. Please set a project with `slices project use <name>`. :x:")
            raise typer.Exit(1)
        return project_id

    @cached_property
    def ai_api(self):
        """AI API."""
        return self.api.for_project(self._require_project_id()).ai(self.site_id)


class AIContext(typer.Context):
    """Custom context for slices-cli."""

    obj: AIContextObj


class WrapContextAsAIContext:
    """Helper to wrap a regular Context so it behaves as a AIContext."""

    def __init__(self, orig_context: typer.Context, ctx_obj: AIContextObj):
        self.orig_context = orig_context
        self.ctx_obj = ctx_obj

    @property
    def obj(self):
        """Return the modified context object."""
        return self.ctx_obj

    def __getattr__(self, attr):
        """Proxy all calls to the real Context."""
        return getattr(self.orig_context, attr)
