"""
navig.commands.whoami — Display the user's NAVIG entity sigil card.

Usage:
    navig whoami [--debug]

Loads the persisted identity, re-derives the full NaviEntity from the seed,
and renders the Rich sigil card in the terminal. If no identity exists yet,
directs the user to run `navig init`.
"""
from __future__ import annotations


def run_whoami(debug: bool = False) -> None:
    from navig.identity.sigil_store import load_entity

    if debug:
        try:
            from navig.platform.paths import entity_json_path  # type: ignore[import]
            import typer  # type: ignore[import]
            typer.echo(f"[debug] loading from {entity_json_path()}")
        except Exception:  # noqa: BLE001
            try:
                from navig.identity.sigil_store import _identity_path  # type: ignore[attr-defined]
                import typer  # type: ignore[import]
                typer.echo(f"[debug] loading from {_identity_path()}")
            except Exception:  # noqa: BLE001
                pass

    data = load_entity()
    if not data:
        try:
            from rich.console import Console
            Console().print(
                "\n[bold yellow]Agent identity not found.[/bold yellow]  "
                "Run [bold cyan]navig init[/bold cyan] to configure.\n"
            )
        except ImportError:
            print("Agent identity not found. Run navig init to configure.")
        raise SystemExit(1)

    from navig.identity.entity import derive_entity
    from navig.identity.renderer import render_sigil_card

    entity = derive_entity(data["seed"])
    render_sigil_card(entity)

