"""
quark — CLI for Quark-AI cognitive fingerprinting.

  quark init                           # initialize repo
  quark login                          # authenticate
  quark snapshot --agent my_agent      # capture a fossil
  quark list                           # list fossils
  quark diff <id_a> <id_b>             # compare two fingerprints
  quark restore <id> [--to file.md]    # export behavior card
"""
import typer
from rich.console import Console

from quark.commands import init_, login, snapshot, diff, restore, list_, log, push, pull

app     = typer.Typer(
    name="quark",
    help="Cognitive fingerprinting for AI agents — quark-ai.cordee.ovh",
    add_completion=True,
    rich_markup_mode="rich",
    no_args_is_help=True,
)
console = Console()


@app.command("init")
def cmd_init(
    url: str = typer.Option("", "--url"),
    agent: str = typer.Option("", "--agent"),
):
    """Initialize a cognitive repo in the current directory."""
    init_.run(url=url, agent=agent)


@app.command("login")
def cmd_login(
    email: str = typer.Option("", "--email", "-e"),
    token: str = typer.Option("", "--token", "-t"),
):
    """Authenticate with Quark-AI."""
    login.run(email=email, token=token)


@app.command("snapshot")
def cmd_snapshot(
    agent: str       = typer.Option("", "--agent", "-a"),
    prompt: str      = typer.Option("", "--prompt", "-p"),
    prompt_file: str = typer.Option("", "--prompt-file", "-f"),
    fossil_type: str = typer.Option("cognitive_imprint", "--type"),
    description: str = typer.Option("", "--description", "-d"),
    tags: str        = typer.Option("", "--tag"),
    mode: str        = typer.Option("pur", "--mode"),
    output_json: bool = typer.Option(False, "--json"),
):
    """Capture a cognitive fossil from an agent."""
    snapshot.run(
        agent=agent, prompt=prompt, prompt_file=prompt_file,
        fossil_type=fossil_type, description=description,
        tags=tags, mode=mode, output_json=output_json,
    )


@app.command("list")
def cmd_list(
    limit: int        = typer.Option(20, "--limit", "-n"),
    output_json: bool = typer.Option(False, "--json"),
):
    """List fossils in the repository."""
    list_.run(limit=limit, output_json=output_json)


@app.command("diff")
def cmd_diff(
    id_a: str         = typer.Argument(..., metavar="FOSSIL_A"),
    id_b: str         = typer.Argument(..., metavar="FOSSIL_B"),
    output_json: bool = typer.Option(False, "--json"),
):
    """Compare two cognitive fossils — behavioral drift and stability delta."""
    diff.run(id_a=id_a, id_b=id_b, output_json=output_json)


@app.command("restore")
def cmd_restore(
    fossil_id: str    = typer.Argument(..., metavar="FOSSIL_ID"),
    to: str           = typer.Option("", "--to"),
    format: str       = typer.Option("markdown", "--format"),
    output_json: bool = typer.Option(False, "--json"),
):
    """Export the behavioral specification of a past fossil."""
    restore.run(fossil_id=fossil_id, to=to, format=format, output_json=output_json)


@app.command("log")
def cmd_log(
    agent:       str  = typer.Option("", "--agent", "-a"),
    tag:         str  = typer.Option("", "--tag"),
    limit:       int  = typer.Option(30, "--limit", "-n"),
    oneline:     bool = typer.Option(False, "--oneline"),
    output_json: bool = typer.Option(False, "--json"),
):
    """Show the ordered history of cognitive fossils."""
    log.run(agent=agent, tag=tag, limit=limit, oneline=oneline, output_json=output_json)


@app.command("push")
def cmd_push(
    fossil_ids:  list[str] = typer.Argument(default=None),
    agent:       str  = typer.Option("", "--agent", "-a"),
    output_dir:  str  = typer.Option("", "--dir"),
    output_json: bool = typer.Option(False, "--json"),
):
    """Export fossils to .quark/fossils/ for team sharing via git."""
    push.run(fossil_ids=fossil_ids or [], agent=agent, output_dir=output_dir, output_json=output_json)


@app.command("pull")
def cmd_pull(
    sources:     list[str] = typer.Argument(...),
    output_json: bool = typer.Option(False, "--json"),
):
    """Import fossil(s) from JSON files exported by `quark push`."""
    pull.run(sources=sources, output_json=output_json)


@app.command("version")
def cmd_version():
    """Show version."""
    from quark import __version__
    console.print(f"quark-agent [bold]{__version__}[/bold]")
