"""Setup and lifecycle commands: init, install, uninstall, connect, onboard."""

from __future__ import annotations

import json
import os
import shutil
import sys
from datetime import datetime, timezone
from pathlib import Path
from typing import Optional

import click
import yaml

from ._common import AGENT_HOME, __version__, console, status_icon, consciousness_banner
from ._validators import validate_agent_name
from ..models import AgentConfig, PillarStatus, SyncConfig
from ..pillars.identity import generate_identity
from ..pillars.memory import initialize_memory
from ..pillars.security import audit_event, initialize_security
from ..pillars.sync import initialize_sync
from ..pillars.trust import initialize_trust
from ..runtime import get_runtime

from rich.panel import Panel


def _write_global_claude_md(home_path: Path, agent_name: str) -> Optional[Path]:
    """Write ~/.claude/CLAUDE.md so Claude Code auto-rehydrates on every session."""
    import platform

    if platform.system() == "Windows":
        appdata = os.environ.get("APPDATA", "")
        if appdata:
            claude_dir = Path(appdata) / ".claude"
        else:
            claude_dir = Path.home() / "AppData" / "Roaming" / ".claude"
    else:
        claude_dir = Path.home() / ".claude"

    try:
        claude_dir.mkdir(parents=True, exist_ok=True)
        claude_md = claude_dir / "CLAUDE.md"

        content = (
            f"# Claude Code — Global Agent Instructions ({agent_name})\n\n"
            "## SKCapstone Auto-Rehydration\n\n"
            "At the start of every new session, run these commands in order:\n\n"
            "```bash\n"
            "skcapstone trust rehydrate\n"
            "skcapstone sync pull\n"
            "skcapstone status\n"
            "```\n\n"
            "## Agent Details\n\n"
            f"- **Name**: {agent_name}\n"
            f"- **Home**: `{home_path}`\n\n"
            "## Quick Reference\n\n"
            "```bash\n"
            "skcapstone status                          # full pillar status\n"
            "skcapstone memory list                     # recent memories\n"
            "skcapstone sync push                       # push state to peers\n"
            "skcapstone context show --format claude-md # regenerate this file\n"
            "skcapstone sync pair --export-pubkey       # export your GPG pubkey\n"
            "skcapstone sync pair --import-pubkey <f>   # import a peer's pubkey\n"
            "```\n\n"
            "> Auto-generated by `skcapstone init`. "
            "Regenerate with: `skcapstone context generate --target claude-md`\n"
        )

        claude_md.write_text(content, encoding="utf-8")
        return claude_md
    except OSError:
        return None


def register_setup_commands(main: click.Group) -> None:
    """Register all setup/lifecycle commands on the main CLI group."""

    @main.command()
    @click.option(
        "--home",
        default=AGENT_HOME,
        help="Agent home directory.",
        type=click.Path(),
    )
    def init(home: str):
        """Initialize a sovereign agent (interactive wizard).

        Alias for 'skcapstone onboard' — runs the full 13-step setup wizard.
        Creates ~/.skcapstone/ with identity, memory, trust, security, soul,
        and connects to the mesh. Zero to sovereign in under 5 minutes.
        """
        from ..onboard import run_onboard

        run_onboard(home)

    @main.command("install")
    @click.option("--name", default=None, help="Name for your sovereign agent.")
    @click.option("--email", default=None, help="Email for the agent identity.")
    @click.option("--home", default=AGENT_HOME, help="Agent home directory.", type=click.Path())
    @click.option("--skip-deps", is_flag=True, help="Skip installing ecosystem packages.")
    @click.option("--skip-seeds", is_flag=True, help="Skip importing Cloud 9 seeds.")
    @click.option("--skip-ritual", is_flag=True, help="Skip the rehydration ritual.")
    @click.option("--skip-preflight", is_flag=True, help="Skip Git preflight check.")
    @click.option("--path", "install_path", default=None, type=click.IntRange(1, 3),
                  help="Pre-select install path: 1=fresh, 2=join, 3=update.")
    def install_cmd(name, email, home, skip_deps, skip_seeds, skip_ritual, skip_preflight, install_path):
        """Guided setup wizard — set up, join, or update your sovereign node."""
        from ..install_wizard import run_install_wizard

        run_install_wizard(
            name=name, email=email, home=home,
            skip_deps=skip_deps, skip_seeds=skip_seeds,
            skip_ritual=skip_ritual, skip_preflight=skip_preflight,
            path=install_path,
        )

    @main.command("uninstall")
    @click.option("--home", default=AGENT_HOME, help="Agent home directory.", type=click.Path())
    @click.option("--force", is_flag=True, help="Skip confirmations (for scripting).")
    @click.option("--keep-data", is_flag=True, help="Deregister only — keep local files.")
    @click.option(
        "--export-first",
        is_flag=True,
        help="Create a full backup archive before removing data.",
    )
    def uninstall_cmd(home, force, keep_data, export_first):
        """Remove this sovereign node completely."""
        from ..uninstall_wizard import run_uninstall_wizard

        run_uninstall_wizard(home=home, force=force, keep_data=keep_data, export_first=export_first)

    @main.command("install-gui")
    def install_gui_cmd():
        """Launch the graphical setup wizard (Windows-friendly)."""
        from ..gui_installer import main as gui_main

        gui_main()

    @main.command()
    @click.argument("platform")
    @click.option("--home", default=AGENT_HOME, help="Agent home directory.", type=click.Path())
    def connect(platform: str, home: str):
        """Connect a platform to the sovereign agent.

        Supported platforms: cursor, terminal, vscode, neovim, web
        """
        home_path = Path(home).expanduser()

        if not home_path.exists():
            console.print("[bold red]No agent found.[/] Run [bold]skcapstone init[/] first.")
            sys.exit(1)

        runtime = get_runtime(home_path)
        connector = runtime.register_connector(name=f"{platform} connector", platform=platform)
        audit_event(home_path, "CONNECT", f"Platform '{platform}' connected")

        console.print()
        console.print(
            f"[bold green]Connected:[/] {platform} "
            f"[dim]({connector.connected_at.isoformat() if connector.connected_at else 'now'})[/]"
        )
        console.print(
            f"[dim]Your agent '{runtime.manifest.name}' is now accessible from {platform}.[/]"
        )
        console.print()

    @main.command("onboard")
    @click.option("--home", default=AGENT_HOME, type=click.Path())
    def onboard_cmd(home: str):
        """Interactive onboarding wizard for new humans and AI agents.

        \b
        Eight guided steps — zero to sovereign in under 5 minutes:
          1. Identity   — generate PGP keypair via CapAuth
          2. Soul       — create name, values, and personality blueprint
          3. Memory     — initialize SKMemory and import Cloud 9 seeds
          4. Ritual     — run the full rehydration ritual
          5. Trust      — verify trust chain from FEB files
          6. Mesh       — check Syncthing peering
          7. Heartbeat  — publish your first alive beacon
          8. Board      — register on the coordination board
        """
        from ..onboard import run_onboard

        run_onboard(home)

    @main.command("reset")
    @click.option("--home", default=AGENT_HOME, type=click.Path(), help="Agent home directory.")
    @click.option("--force", is_flag=True, help="Skip confirmation prompt (for scripting/testing).")
    def reset_cmd(home: str, force: bool):
        """Factory reset — wipe all agent data.

        Backs up the identity/ directory to ~/.skcapstone-backup-{timestamp}/
        before deleting. All other data is permanently removed.
        """
        home_path = Path(home).expanduser()

        if not home_path.exists():
            console.print(f"[yellow]No agent home found at {home_path}. Nothing to reset.[/]")
            return

        if not force:
            console.print(
                f"\n[bold red]WARNING:[/] This will permanently delete all agent data at:\n"
                f"  [dim]{home_path}[/]\n"
            )
            answer = click.prompt(
                "Are you sure? This will delete all agent data. Type YES to confirm",
                default="",
                show_default=False,
            )
            if answer.strip() != "YES":
                console.print("[yellow]Reset aborted.[/]")
                return

        # Backup identity/ first
        identity_dir = home_path / "identity"
        backup_path: Path | None = None
        if identity_dir.exists():
            ts = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S")
            backup_path = home_path.parent / f".skcapstone-backup-{ts}"
            backup_path.mkdir(parents=True, exist_ok=True)
            shutil.copytree(str(identity_dir), str(backup_path / "identity"))
            console.print(f"  [dim]Identity backed up → {backup_path}[/]")

        # Wipe the home directory
        shutil.rmtree(str(home_path))
        console.print(f"[bold green]Reset complete.[/] All agent data deleted from {home_path}.")
        if backup_path:
            console.print(f"  [dim]Identity backup: {backup_path}[/]")
        console.print("[dim]Run 'skcapstone init' to start fresh.[/]")

    @main.command("shell")
    def shell_cmd():
        """Interactive REPL for sovereign agent operations."""
        from ..shell import run_shell

        run_shell()
