#!/bin/bash
set -e

echo "=========================================="
echo "FASE 1: Setup WACRM + Hermes"
echo "SKEEN Derma Experts"
echo "=========================================="

# Variables
PROJECT_ROOT="/root/skeen-roadmap-hermes"
WACRM_DIR="/root/skeen-wacrm"
HERMES_DIR="$PROJECT_ROOT/hermes"

# 1. Verificar dependencias
echo "[1/8] Verificando dependencias..."
python3 --version
node -v
docker --version
nginx -v

# 2. Configurar PostgreSQL
echo "[2/8] Configurando PostgreSQL..."
sudo -u postgres psql -c "CREATE DATABASE skeen_wacrm;" 2>/dev/null || true
sudo -u postgres psql -c "CREATE USER skeen WITH PASSWORD 'skeen_dev_2026';" 2>/dev/null || true
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE skeen_wacrm TO skeen;" 2>/dev/null || true

# 3. Crear schemas de Supabase
echo "[3/8] Creando schemas de Supabase..."
sudo -u postgres psql -d skeen_wacrm -c "CREATE SCHEMA IF NOT EXISTS auth;" 2>/dev/null || true
sudo -u postgres psql -d skeen_wacrm -c "CREATE SCHEMA IF NOT EXISTS storage;" 2>/dev/null || true
sudo -u postgres psql -d skeen_wacrm -c "CREATE TABLE IF NOT EXISTS auth.users (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), email TEXT, encrypted_password TEXT, email_confirmed_at TIMESTAMPTZ, created_at TIMESTAMPTZ DEFAULT NOW());" 2>/dev/null || true

# 4. Crear .env.local para WACRM
echo "[4/8] Configurando WACRM..."
cat > "$WACRM_DIR/.env.local" << 'EOF'
# WACRM Environment - Development
NEXT_PUBLIC_APP_URL=http://localhost:3000
NEXT_PUBLIC_SUPABASE_URL=http://localhost:54321
NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InNrZWVuIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTk3NTc0MDAsImV4cCI6MTkxNzUxMzgwMH0.skeen_dev_key
SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InNrZWVuIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc1OTc1NzQwMCwiZXhwIjoxOTE3NTEzODAwfQ.skeen_dev_service
ENCRYPTION_KEY=0000000000000000000000000000000000000000000000000000000000000000
WHATSAPP_VERIFY_TOKEN=skeen_webhook_verify_2026
EOF

# 5. Crear directorios de Hermes
echo "[5/8] Configurando Hermes..."
mkdir -p "$HERMES_DIR/.hermes/workspace"
mkdir -p "$HERMES_DIR/.hermes/plugins"
mkdir -p "$HERMES_DIR/skills"

# 6. Copiar archivos de configuración
echo "[6/8] Copiando archivos de configuración..."
cp "$PROJECT_ROOT/hermes/config.yaml" "$HERMES_DIR/.hermes/config.yaml" 2>/dev/null || true
cp "$PROJECT_ROOT/hermes/workspace/SOUL.md" "$HERMES_DIR/.hermes/workspace/SOUL.md" 2>/dev/null || true
cp "$PROJECT_ROOT/hermes/workspace/AGENTS.md" "$HERMES_DIR/.hermes/workspace/AGENTS.md" 2>/dev/null || true

# 7. Copiar skills
echo "[7/8] Copiando skills..."
cp -r "$PROJECT_ROOT/skills/hermes-skeen/"* "$HERMES_DIR/skills/" 2>/dev/null || true

# 8. Copiar plugins
echo "[8/8] Copiando plugins..."
cp "$PROJECT_ROOT/hermes/plugins/"*.py "$HERMES_DIR/.hermes/plugins/" 2>/dev/null || true

echo "=========================================="
echo "Setup completado!"
echo "=========================================="
echo ""
echo "Próximos pasos:"
echo "1. Configurar variables de Meta (PHONE_ID, ACCESS_TOKEN)"
echo "2. Instalar Hermes: pip install 'hermes-agent[messaging,cron,web]'"
echo "3. Iniciar WACRM: cd $WACRM_DIR && npm run dev"
echo "4. Iniciar Hermes: hermes gateway start"
echo ""
