#!/bin/bash
# 00_setup.sh — Initialize Nexus server, seed demo data, start TUI in tmux
#
# Run this ONCE before executing any scenario script.
#
# What it does:
#   1. nexus init --preset demo  (creates nexus.yaml)
#   2. nexus up                  (starts Docker Compose stack)
#   3. eval $(nexus env)         (loads NEXUS_URL, NEXUS_API_KEY, etc.)
#   4. nexus demo init           (seeds users, agents, files, permissions)
#   5. Starts TUI in a tmux session for visual verification
#
# Prerequisites:
#   - Docker & Docker Compose installed
#   - `nexus` CLI installed (pip install nexus-ai-fs)
#   - tmux installed
#
# Usage:
#   cd <project-root>
#   bash scripts/cli-scenarios/00_setup.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/lib.sh"

SCENARIO_NAME="Setup"
header "Nexus CLI Scenario Test — Initial Setup"

# ── Step 1: Check prerequisites ──────────────────────────────────────────
info "Checking prerequisites..."

for cmd in nexus docker tmux; do
    if ! command -v "$cmd" &>/dev/null; then
        fail "Required command not found: $cmd"
        echo "  Install $cmd before running this script."
        exit 1
    fi
done
ok "All prerequisites found (nexus, docker, tmux)"

# ── Step 2: Init project ────────────────────────────────────────────────
header "Step 1: nexus init --preset demo"

if [ -f nexus.yaml ]; then
    warn "nexus.yaml already exists — skipping init (delete it to reinit)"
else
    run_cli INIT_OUT nexus init --preset demo
    assert_exit_code "nexus init" 0 "$INIT_OUT_RC"
fi
ok "Project initialised (nexus.yaml present)"

# ── Step 3: Start stack ─────────────────────────────────────────────────
header "Step 2: nexus up"

run_cli UP_OUT nexus up --timeout 120
assert_exit_code "nexus up" 0 "$UP_OUT_RC"
info "Stack output: $(echo "$UP_OUT" | tail -3)"

# ── Step 4: Load environment ────────────────────────────────────────────
header "Step 3: Load environment variables"

eval "$(nexus env 2>/dev/null)"
info "NEXUS_URL=${NEXUS_URL:-<not set>}"
info "NEXUS_API_KEY=${NEXUS_API_KEY:+sk-****}"

if [ -z "${NEXUS_URL:-}" ]; then
    fail "NEXUS_URL not set after 'nexus env'"
    exit 1
fi
ok "Environment loaded"

# ── Step 5: Health check ────────────────────────────────────────────────
header "Step 4: Health check"

run_cli STATUS_OUT nexus status
assert_exit_code "nexus status" 0 "$STATUS_OUT_RC"
info "$STATUS_OUT"
ok "Server is healthy"

# ── Step 6: Seed demo data ──────────────────────────────────────────────
header "Step 5: nexus demo init"

run_cli DEMO_OUT nexus demo init
assert_exit_code "nexus demo init" 0 "$DEMO_OUT_RC"
info "Demo seed output:"
echo "$DEMO_OUT" | tail -10 | sed 's/^/    /'
ok "Demo data seeded"

# ── Step 7: Verify demo data ────────────────────────────────────────────
header "Step 6: Verify demo data"

run_cli LS_OUT nexus ls /workspace/demo/
assert_exit_code "nexus ls /workspace/demo/" 0 "$LS_OUT_RC"
assert_contains "Demo files present" "$LS_OUT" "README.md"
assert_contains "Demo files present" "$LS_OUT" "plan.md"

# ── Step 8: Start TUI in tmux ───────────────────────────────────────────
header "Step 7: Start TUI in tmux"

tui_start
sleep 2
tui_assert_contains "TUI connected" "Files"
ok "TUI running in tmux session '$TUI_SESSION'"

# ── Step 9: Write env file for scenario scripts ─────────────────────────
header "Step 8: Write env file for scenarios"

cat > "$SCRIPT_DIR/.env.scenarios" <<EOF
# Auto-generated by 00_setup.sh — source this in scenario scripts
export NEXUS_URL="${NEXUS_URL}"
export NEXUS_API_KEY="${NEXUS_API_KEY}"
export TUI_SESSION="${TUI_SESSION}"
EOF

ok "Env file written to $SCRIPT_DIR/.env.scenarios"

# ── Done ─────────────────────────────────────────────────────────────────
print_summary

echo ""
info "Setup complete. Run scenario scripts:"
echo "    bash scripts/cli-scenarios/01_file_operations.sh"
echo "    bash scripts/cli-scenarios/02_version_management.sh"
echo "    ..."
echo ""
info "To attach to TUI:  tmux attach -t $TUI_SESSION"
info "To stop TUI:       tmux kill-session -t $TUI_SESSION"
info "To stop server:    nexus down"
