"""Behave steps for TUI persona cycling.

Shared persona steps (registry setup, set-active-persona, active-persona
assertion) live in ``tui_persona_system_steps.py``; behave loads step
files into a single global registry so we only need to define each step
in one place. Only cycle-specific steps live here.
"""

from __future__ import annotations

from behave import given, then, when
from behave.runner import Context

from cleveragents.tui.persona.schema import Persona
from cleveragents.tui.persona.state import PersonaState


@given('I save TUI persona "{name}" with actor "{actor}" and cycle order {cycle:d}')
def step_save_persona_cycle(
    context: Context, name: str, actor: str, cycle: int
) -> None:
    persona = Persona(name=name, actor=actor, cycle_order=cycle)
    context.tui_registry.save(persona)


@when('I cycle persona for session "{session_id}"')
def step_cycle_persona(context: Context, session_id: str) -> None:
    if not hasattr(context, "tui_state"):
        context.tui_state = PersonaState(registry=context.tui_registry)
    context.tui_state.cycle_persona(session_id)


@then('the registry last persona should be set to "{persona_name}"')
def step_registry_last_persona(context: Context, persona_name: str) -> None:
    last = context.tui_registry.get_last_persona()
    assert last == persona_name, f"expected {persona_name!r}, got {last!r}"
