#!/usr/bin/env bash
# =============================================================================
# Install auto-commit-service with full ML pipeline
# =============================================================================
# This script:
# 1. Installs auto-commit-service Python package
# 2. Sets up model-boss-coordinator + multi-model llama-http services
# 3. Starts RAG retrieval service
# 4. Creates config files and systemd services
# 5. Enables lingering for persistent services
#
# Dependencies:
#   - Redis (for model-boss coordination)
#   - Redis Stack (for RAG vector search) - port 6384
#   - GPU with CUDA (for local inference)
#
# Services started:
#   - model-boss-coordinator (port 8210) - GPU/VRAM lease management
#   - llama-http-3b (port 10010) - ministral-3b-instruct (formatting)
#   - llama-http-14b (port 10020) - ministral-14b-reasoning (analysis)
#   - rag-retrieval (port 8111) - context retrieval
#   - commits-packages (port 8200) - auto-commit daemon for @packages
#   - commits-applications (port 8201) - auto-commit daemon for @applications
#   - commits-projects (port 8202) - auto-commit daemon for @projects
# =============================================================================
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

# Paths
MODEL_BOSS_ROOT="/var/home/lilith/Code/@applications/@model-boss"
LLAMA_HTTP_PATH="$MODEL_BOSS_ROOT/services/llama-http/service"
COORDINATOR_PATH="$MODEL_BOSS_ROOT/services/coordinator/service"
RAG_PATH="/var/home/lilith/Code/@applications/@ml/rag-retrieval"

echo "==> Installing auto-commit-service..."

# =============================================================================
# Step 1: Check prerequisites
# =============================================================================
echo ""
echo "==> Checking prerequisites..."

# Check Redis
if ! redis-cli ping >/dev/null 2>&1; then
    echo "    ERROR: Redis not running on localhost:6379"
    echo "    Run: brew services start redis"
    exit 1
fi
echo "    ✓ Redis available"

# Check Redis Stack (for RAG)
if ! redis-cli -p 6384 MODULE LIST 2>/dev/null | grep -q "search"; then
    echo "    WARNING: Redis Stack not found on port 6384"
    echo "    RAG will not work without Redis Stack"
    echo "    Looking for existing Redis Stack containers..."
    if podman ps 2>/dev/null | grep -q "redis-stack"; then
        echo "    ✓ Redis Stack container found"
    else
        echo "    Consider starting: podman run -d --name rag-redis -p 6384:6379 redis/redis-stack:latest"
    fi
else
    echo "    ✓ Redis Stack available on port 6384"
fi

# Check GPU
if ! command -v nvidia-smi >/dev/null 2>&1; then
    echo "    WARNING: nvidia-smi not found - GPU inference may not work"
else
    GPU_COUNT=$(nvidia-smi -L 2>/dev/null | wc -l)
    echo "    ✓ $GPU_COUNT GPU(s) detected"
fi

# =============================================================================
# Step 2: Install Python packages
# =============================================================================
echo ""
echo "==> Installing Python packages..."

# Auto-commit service
if [[ ! -d "$SCRIPT_DIR/.venv" ]]; then
    echo "    Creating auto-commit-service venv..."
    python -m venv "$SCRIPT_DIR/.venv"
fi
echo "    Installing auto-commit-service..."
"$SCRIPT_DIR/.venv/bin/pip" install -e "$SCRIPT_DIR" --quiet

# Model-boss coordinator
if [[ -d "$COORDINATOR_PATH" ]]; then
    if [[ ! -d "$COORDINATOR_PATH/.venv" ]]; then
        echo "    Creating model-boss-coordinator venv..."
        python -m venv "$COORDINATOR_PATH/.venv"
    fi
    echo "    Installing model-boss-coordinator..."
    "$COORDINATOR_PATH/.venv/bin/pip" install -e "$COORDINATOR_PATH" --quiet
fi

# Llama HTTP service
if [[ -d "$LLAMA_HTTP_PATH" ]]; then
    if [[ ! -d "$LLAMA_HTTP_PATH/.venv" ]]; then
        echo "    Creating llama-http venv..."
        python -m venv "$LLAMA_HTTP_PATH/.venv"
    fi
    echo "    Installing llama-http..."
    "$LLAMA_HTTP_PATH/.venv/bin/pip" install -e "$LLAMA_HTTP_PATH" --quiet
fi

# RAG retrieval service
if [[ -d "$RAG_PATH" ]]; then
    if [[ ! -d "$RAG_PATH/.venv" ]]; then
        echo "    Creating rag-retrieval venv..."
        python -m venv "$RAG_PATH/.venv"
    fi
    echo "    Installing rag-retrieval..."
    "$RAG_PATH/.venv/bin/pip" install -e "$RAG_PATH" --quiet
fi

echo "    ✓ Python packages installed"

# =============================================================================
# Step 3: Create config files
# =============================================================================
echo ""
echo "==> Creating config files..."
mkdir -p ~/.config/commits

cat > ~/.config/commits/startup-config.json << 'EOF'
{
  "daemons": [
    {
      "id": "packages",
      "directory": "/var/home/lilith/Code/@packages",
      "port": 8200,
      "interval_seconds": 300,
      "recursive": true,
      "recursive_depth": 4,
      "cache_update_minutes": 60,
      "ignore_repos": [
        ".archive",
        "_archive",
        ".deprecated"
      ],
      "exclude_patterns": [
        "node_modules",
        "pyvenv",
        ".venv",
        "venv",
        "dist",
        "build",
        "__pycache__"
      ]
    },
    {
      "id": "applications",
      "directory": "/var/home/lilith/Code/@applications",
      "port": 8201,
      "interval_seconds": 300,
      "recursive": true,
      "recursive_depth": 4,
      "cache_update_minutes": 60,
      "ignore_repos": [
        ".archive",
        "_archive",
        "egirl-platform",
        ".deprecated"
      ],
      "exclude_patterns": [
        "node_modules",
        "pyvenv",
        ".venv",
        "venv",
        "dist",
        "build",
        "__pycache__"
      ]
    },
    {
      "id": "projects",
      "directory": "/var/home/lilith/Code/@projects",
      "port": 8202,
      "interval_seconds": 300,
      "recursive": true,
      "recursive_depth": 4,
      "cache_update_minutes": 60,
      "ignore_repos": [
        ".archive",
        "_archive",
        ".deprecated"
      ],
      "exclude_patterns": [
        "node_modules",
        "pyvenv",
        ".venv",
        "venv",
        "dist",
        "build",
        "__pycache__"
      ]
    }
  ]
}
EOF
echo "    ✓ startup-config.json created (3 daemons: packages, applications, projects)"

# =============================================================================
# Step 4: Create systemd user services
# =============================================================================
echo ""
echo "==> Setting up systemd user services..."
mkdir -p ~/.config/systemd/user

# Clean up old services
echo "    Cleaning up old services..."
for svc in llama-http.service commits-*.service auto-commit-*.service; do
    systemctl --user stop "$svc" 2>/dev/null || true
    systemctl --user disable "$svc" 2>/dev/null || true
done

# Model Boss Coordinator
cat > ~/.config/systemd/user/model-boss-coordinator.service << EOF
[Unit]
Description=Model Boss Coordinator (GPU/VRAM lease management)
After=network.target
Wants=homebrew.redis.service

[Service]
Type=simple
WorkingDirectory=$COORDINATOR_PATH
ExecStart=$COORDINATOR_PATH/.venv/bin/python -m model_boss_coordinator
Restart=on-failure
RestartSec=10
StandardOutput=journal
StandardError=journal

Environment="PATH=/home/linuxbrew/.linuxbrew/bin:/var/home/lilith/.local/bin:/usr/local/bin:/usr/bin:/bin"
Environment="HOME=/var/home/lilith"
Environment="MODEL_BOSS_PORT=8210"
Environment="MODEL_BOSS_HOST=0.0.0.0"

[Install]
WantedBy=default.target
EOF

# Llama HTTP 3B (instruct/formatting)
cat > ~/.config/systemd/user/llama-http-3b.service << EOF
[Unit]
Description=Llama HTTP Service - 3B (ministral-3b-instruct)
After=network.target model-boss-coordinator.service
Wants=model-boss-coordinator.service

[Service]
Type=simple
WorkingDirectory=$LLAMA_HTTP_PATH
ExecStart=$LLAMA_HTTP_PATH/.venv/bin/python -m llama_http
Restart=on-failure
RestartSec=30
StandardOutput=journal
StandardError=journal

Environment="PATH=/home/linuxbrew/.linuxbrew/bin:/var/home/lilith/.local/bin:/usr/local/bin:/usr/bin:/bin"
Environment="HOME=/var/home/lilith"
Environment="LLAMA_HTTP_SERVICE_NAME=llama-http-3b"
Environment="LLAMA_HTTP_PORT=10010"
Environment="LLAMA_HTTP_MODEL_ID=ministral-3b-instruct"
Environment="LLAMA_HTTP_CONTEXT_SIZE=4096"
Environment="LLAMA_HTTP_N_GPU_LAYERS=-1"
Environment="LLAMA_HTTP_LLAMA_SERVER_PORT=10009"
Environment="LLAMA_HTTP_IDLE_TIMEOUT_SECONDS=0"

[Install]
WantedBy=default.target
EOF

# Llama HTTP 14B (reasoning/analysis)
cat > ~/.config/systemd/user/llama-http-14b.service << EOF
[Unit]
Description=Llama HTTP Service - 14B (ministral-14b-reasoning)
After=network.target model-boss-coordinator.service
Wants=model-boss-coordinator.service

[Service]
Type=simple
WorkingDirectory=$LLAMA_HTTP_PATH
ExecStart=$LLAMA_HTTP_PATH/.venv/bin/python -m llama_http
Restart=on-failure
RestartSec=30
StandardOutput=journal
StandardError=journal

Environment="PATH=/home/linuxbrew/.linuxbrew/bin:/var/home/lilith/.local/bin:/usr/local/bin:/usr/bin:/bin"
Environment="HOME=/var/home/lilith"
Environment="LLAMA_HTTP_SERVICE_NAME=llama-http-14b"
Environment="LLAMA_HTTP_PORT=10020"
Environment="LLAMA_HTTP_MODEL_ID=ministral-14b-reasoning"
Environment="LLAMA_HTTP_CONTEXT_SIZE=8192"
Environment="LLAMA_HTTP_N_GPU_LAYERS=-1"
Environment="LLAMA_HTTP_LLAMA_SERVER_PORT=10019"
Environment="LLAMA_HTTP_IDLE_TIMEOUT_SECONDS=0"

[Install]
WantedBy=default.target
EOF

# RAG Retrieval Service
cat > ~/.config/systemd/user/rag-retrieval.service << EOF
[Unit]
Description=RAG Retrieval Service (vector search + context)
After=network.target

[Service]
Type=simple
WorkingDirectory=$RAG_PATH
ExecStart=$RAG_PATH/.venv/bin/python -m service.src.api.main
Restart=on-failure
RestartSec=10
StandardOutput=journal
StandardError=journal

Environment="PATH=/home/linuxbrew/.linuxbrew/bin:/var/home/lilith/.local/bin:/usr/local/bin:/usr/bin:/bin"
Environment="HOME=/var/home/lilith"

[Install]
WantedBy=default.target
EOF

# Auto-commit services (3 daemons for packages, applications, projects)
for DAEMON_NAME in packages applications projects; do
    case $DAEMON_NAME in
        packages) WORK_DIR="/var/home/lilith/Code/@packages" ;;
        applications) WORK_DIR="/var/home/lilith/Code/@applications" ;;
        projects) WORK_DIR="/var/home/lilith/Code/@projects" ;;
    esac

    cat > ~/.config/systemd/user/commits-${DAEMON_NAME}.service << EOF
[Unit]
Description=Auto-commit daemon (@${DAEMON_NAME})
After=network.target llama-http-3b.service llama-http-14b.service rag-retrieval.service
Wants=llama-http-3b.service llama-http-14b.service rag-retrieval.service

[Service]
Type=simple
WorkingDirectory=$WORK_DIR
ExecStart=$SCRIPT_DIR/.venv/bin/python -m auto_commit_service
Restart=on-failure
RestartSec=30
StandardOutput=journal
StandardError=journal

Environment="PATH=/home/linuxbrew/.linuxbrew/bin:/var/home/lilith/.local/bin:/usr/local/bin:/usr/bin:/bin"
Environment="HOME=/var/home/lilith"

[Install]
WantedBy=default.target
EOF
done

echo "    ✓ systemd services created"

# =============================================================================
# Step 5: Enable and start services
# =============================================================================
echo ""
echo "==> Reloading systemd..."
systemctl --user daemon-reload

echo "==> Enabling services..."
systemctl --user enable model-boss-coordinator.service
systemctl --user enable llama-http-3b.service
systemctl --user enable llama-http-14b.service
systemctl --user enable rag-retrieval.service
systemctl --user enable commits-packages.service
systemctl --user enable commits-applications.service
systemctl --user enable commits-projects.service

echo "==> Enabling lingering..."
loginctl enable-linger "$(whoami)" 2>/dev/null || true

echo ""
echo "==> Starting services in dependency order..."

echo "    [1/5] Starting model-boss-coordinator..."
systemctl --user start model-boss-coordinator.service || true
sleep 3

# Initialize GPUs
if [[ -x "$MODEL_BOSS_ROOT/scripts/init-gpus.sh" ]]; then
    echo "    [1.5/5] Initializing GPUs..."
    "$MODEL_BOSS_ROOT/scripts/init-gpus.sh" || true
fi

echo "    [2/5] Starting llama-http-3b..."
systemctl --user start llama-http-3b.service || true

echo "    [3/5] Starting llama-http-14b..."
systemctl --user start llama-http-14b.service || true

echo "    [4/5] Starting rag-retrieval..."
systemctl --user start rag-retrieval.service || true

# Wait for LLM services
echo "    Waiting for LLM services to initialize..."
MAX_WAIT=120
WAITED=0
while ! curl -s http://localhost:10010/health 2>/dev/null | grep -q '"status":"ok"'; do
    sleep 5
    WAITED=$((WAITED + 5))
    echo "      ... waiting ($WAITED/${MAX_WAIT}s)"
    if [[ $WAITED -ge $MAX_WAIT ]]; then
        echo "    WARNING: llama-http-3b not ready after ${MAX_WAIT}s"
        break
    fi
done

echo "    [5/7] Starting commits-packages daemon..."
systemctl --user start commits-packages.service || true

echo "    [6/7] Starting commits-applications daemon..."
systemctl --user start commits-applications.service || true

echo "    [7/7] Starting commits-projects daemon..."
systemctl --user start commits-projects.service || true

# =============================================================================
# Summary
# =============================================================================
echo ""
echo "=========================================="
echo "Installation complete!"
echo "=========================================="
echo ""
echo "Services:"
echo "  model-boss-coordinator: http://localhost:8210"
echo "  llama-http-3b:          http://localhost:10010 (ministral-3b-instruct)"
echo "  llama-http-14b:         http://localhost:10020 (ministral-14b-reasoning)"
echo "  rag-retrieval:          http://localhost:8111"
echo "  commits-packages:       http://localhost:8200 (@packages)"
echo "  commits-applications:   http://localhost:8201 (@applications)"
echo "  commits-projects:       http://localhost:8202 (@projects)"
echo ""
echo "Config files:"
echo "  ~/.config/commits/startup-config.json"
echo "  ~/.config/commits/daemons.json (runtime)"
echo ""
echo "Commands:"
echo "  commits status          - Show daemon status"
echo "  commits list            - List all daemons"
echo "  commits commit --dry-run - Preview commit"
echo "  journalctl --user -u commits -f - View logs"
echo ""

# Show service status
echo "==> Service status:"
for svc in model-boss-coordinator llama-http-3b llama-http-14b rag-retrieval commits-packages commits-applications commits-projects; do
    STATUS=$(systemctl --user is-active ${svc}.service 2>/dev/null || echo "unknown")
    printf "  %-25s %s\n" "$svc:" "$STATUS"
done
