#
#   Imandra Inc.
#
#   speclogician/presentation/preview/cli.py
#

from __future__ import annotations

import json
import typer

from speclogician.presentation.ctx import RenderCtx
from speclogician.presentation.preview.registry import get, list_previews
from speclogician.presentation.preview.fixtures import register_fixtures
from speclogician.utils import console  # your rich console


app = typer.Typer(help="Preview PM renderers using synthetic fixtures.")


@app.command("list")
def list_cmd() -> None:
    register_fixtures()
    for spec in sorted(list_previews(), key=lambda s: s.name):
        console.print(f"[bold]{spec.name}[/bold]  variants={', '.join(spec.variants)}")


@app.command("show")
def show_cmd(
    name: str = typer.Argument(..., help="Preview name (see: preview list)"),
    variant: str = typer.Argument("typical", help="Variant: typical|minimal|edge"),
    json_only: bool = typer.Option(False, "--json", help="Dump PM as JSON instead of rendering."),
    compact: bool = typer.Option(False, "--compact", help="Use compact rendering."),
) -> None:
    register_fixtures()

    ctx = RenderCtx(compact=compact)
    spec = get(name)

    if variant not in spec.variants:
        raise typer.BadParameter(f"Unknown variant '{variant}'. Choose one of: {', '.join(spec.variants)}")

    pm = spec.make(ctx, variant)

    if json_only:
        # Pydantic v2
        typer.echo(json.dumps(pm.model_dump(), indent=2, default=str))
        raise typer.Exit(0)

    console.print(spec.render(pm, ctx))
