"""bandito signup — account creation flow."""

from __future__ import annotations


def run_signup() -> None:
    """Interactive signup: create account -> get API key -> save to config."""
    import getpass

    from .api import BanditoClient

    print("\n  bandito signup\n")
    print("  Create a free Bandito account to enable grading,")
    print("  LLM-as-judge, and analysis features.\n")

    try:
        email = input("  Email: ").strip()
        if not email:
            print("  Email is required.\n")
            return
        password = getpass.getpass("  Password (min 8 chars): ")
        if len(password) < 8:
            print("  Password must be at least 8 characters.\n")
            return
    except (EOFError, KeyboardInterrupt):
        print("\n  Cancelled.\n")
        return

    # Step 1: Sign up (auto-falls back to login if account exists)
    print("\n  Creating account...")
    try:
        auth = BanditoClient.signup(email, password)
    except RuntimeError as e:
        print(f"  {e}\n")
        return

    # Step 2: Create API key
    print("  Creating API key...")
    try:
        raw_key = BanditoClient.create_api_key(auth["access_token"])
    except RuntimeError as e:
        print(f"  {e}\n")
        return

    # Step 3: Save to config
    from ..config import load_config, save_config

    config = load_config()
    config.api_key = raw_key
    saved_path = save_config(config)

    print(f"\n  Account created for {email}")
    print(f"  API key saved to {saved_path}")
    print(f"\n  Next steps:")
    print(f"    bandito setup config      Configure local settings and storage")
    print(f"    bandito tui               Grade traces in the terminal\n")
