"""First-run detection and setup helpers for the TUI."""

from __future__ import annotations

from cleveragents.tui.persona.registry import PersonaRegistry
from cleveragents.tui.persona.schema import Persona


def is_first_run(registry: PersonaRegistry) -> bool:
    """Return ``True`` when no personas are configured.

    A first-run state is defined as the persona registry containing zero
    personas.  This is checked by listing all personas from the registry
    without creating any side-effects.

    Parameters
    ----------
    registry:
        The :class:`~cleveragents.tui.persona.registry.PersonaRegistry`
        instance to inspect.

    Returns
    -------
    bool
        ``True`` if the registry has no personas, ``False`` otherwise.
    """
    return len(registry.list_personas()) == 0


def create_default_persona_for_actor(
    registry: PersonaRegistry,
    actor: str,
) -> Persona:
    """Create and persist a ``"default"`` persona for the given actor.

    This is called after the user selects an actor in the first-run
    overlay.  The persona is saved to the registry and set as the last
    active persona so that subsequent launches restore it.

    Parameters
    ----------
    registry:
        The :class:`~cleveragents.tui.persona.registry.PersonaRegistry`
        instance to write to.
    actor:
        Fully-qualified actor reference (e.g. ``"anthropic/claude-4-sonnet"``).

    Returns
    -------
    Persona
        The newly created and persisted default persona.
    """
    persona = Persona(
        name="default",
        actor=actor,
        description="Default persona",
    )
    registry.save(persona)
    registry.set_last_persona(persona.name)
    return persona
