"""quark init — initialize a cognitive repo in the current directory."""
import typer
from rich.console import Console
from rich.prompt import Prompt, Confirm

from quark import config

console = Console()


def run(
    url: str = typer.Option(
        config.DEFAULT_URL,
        "--url",
        help="Quark-AI API URL",
    ),
    agent: str = typer.Option(
        "",
        "--agent",
        help="Default agent name for snapshots",
    ),
):
    """Initialize a Quark cognitive repo in the current directory."""
    if config.config_exists():
        overwrite = Confirm.ask(
            "[yellow].quark/config.toml already exists. Overwrite?[/yellow]",
            default=False,
        )
        if not overwrite:
            raise typer.Exit()

    if not url or url == config.DEFAULT_URL:
        url = Prompt.ask("API URL", default=config.DEFAULT_URL)

    cfg = {"api_url": url}
    if agent:
        cfg["default_agent"] = agent

    config.save(cfg)

    console.print()
    console.print("[bold cyan]◈  Quark cognitive repo initialized[/bold cyan]")
    console.print(f"   Config  → [dim].quark/config.toml[/dim]")
    console.print(f"   API     → [dim]{url}[/dim]")
    console.print()
    console.print(
        "Next: [bold]quark login[/bold] to authenticate, "
        "then [bold]quark snapshot --agent <name>[/bold]"
    )
