"""Click wrapper for asya init command."""

from __future__ import annotations

import sys
from pathlib import Path

import click

from asya_lab.init import init_project


def _prompt_registry() -> str:
    try:
        return click.prompt("Container image registry (e.g. ghcr.io/my-org)")
    except (EOFError, KeyboardInterrupt):
        click.echo("\n[-] Registry is required", err=True)
        sys.exit(1)


@click.command()
@click.option("--registry", default=None, help="Container image registry (e.g. ghcr.io/my-org)")
@click.option("--dir", "target_dir", default=".", help="Target directory (default: current directory)")
def init(registry, target_dir):
    """Scaffold .asya/ project directory."""
    target = Path(target_dir).resolve()
    if not target.is_dir():
        click.echo(f"Error: {target} is not a directory", err=True)
        sys.exit(1)

    if registry is None:
        registry = _prompt_registry()

    asya_dir = init_project(target, registry=registry)
    click.echo(f"[+] Initialized project at {asya_dir}")
