#!/bin/bash
# ONE COMMAND to set up everything - run this script

set -e

cd /home/intlc/projects/Sankofa/api

echo "=========================================="
echo "Sovereign Stack - Complete Setup"
echo "=========================================="
echo ""
echo "This will:"
echo "  1. Set up PostgreSQL database"
echo "  2. Configure .env file"
echo "  3. Run migrations"
echo "  4. Seed all services"
echo "  5. Verify setup"
echo ""
echo "You may be prompted for your sudo password."
echo ""
echo "Starting setup in 2 seconds..."
sleep 2

# Step 1: Ensure .env is configured
echo ""
echo "Step 1: Configuring .env..."
if [ ! -f .env ]; then
  cat > .env << 'ENVEOF'
DB_HOST=localhost
DB_PORT=5432
DB_NAME=sankofa
DB_USER=postgres
DB_PASSWORD=dev_sankofa_2024_secure
NODE_ENV=development
PORT=4000
ENVEOF
  echo "✅ Created .env file"
else
  # Update password
  sed -i 's|^DB_PASSWORD=.*|DB_PASSWORD=dev_sankofa_2024_secure|' .env || echo "DB_PASSWORD=dev_sankofa_2024_secure" >> .env
  sed -i 's|^NODE_ENV=.*|NODE_ENV=development|' .env || echo "NODE_ENV=development" >> .env
  echo "✅ Updated .env file"
fi

# Step 2: Set up database
echo ""
echo "Step 2: Setting up database..."
echo "(You may be prompted for sudo password)"

if ! sudo -u postgres psql << 'EOSQL'; then
-- Create database if it doesn't exist
SELECT 'CREATE DATABASE sankofa'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'sankofa')\gexec

-- Set password
ALTER USER postgres WITH PASSWORD 'dev_sankofa_2024_secure';
EOSQL
  echo "❌ Database setup failed"
  echo ""
  echo "Please run manually:"
  echo "  sudo -u postgres psql"
  echo "  CREATE DATABASE sankofa;"
  echo "  ALTER USER postgres PASSWORD 'dev_sankofa_2024_secure';"
  echo "  \\q"
  exit 1
fi

echo "✅ Database configured"

# Step 3: Test connection
echo ""
echo "Step 3: Testing database connection..."
sleep 1

if PGPASSWORD="dev_sankofa_2024_secure" psql -h localhost -U postgres -d sankofa -c "SELECT 1;" >/dev/null 2>&1; then
  echo "✅ Database connection successful"
else
  echo "❌ Database connection failed"
  echo "Please verify PostgreSQL is running and try again"
  exit 1
fi

# Step 4: Run migrations
echo ""
echo "Step 4: Running migrations..."
pnpm db:migrate:up || {
  echo "❌ Migration failed"
  exit 1
}
echo "✅ Migrations completed"

# Step 5: Seed services
echo ""
echo "Step 5: Seeding Sovereign Stack services..."
pnpm db:seed:sovereign-stack || {
  echo "❌ Seeding failed"
  exit 1
}
echo "✅ Services seeded"

# Step 6: Verify
echo ""
echo "Step 6: Verifying setup..."
pnpm verify:sovereign-stack || {
  echo "⚠ Verification found issues"
  exit 1
}

echo ""
echo "=========================================="
echo "✅ SETUP COMPLETE!"
echo "=========================================="
echo ""
echo "All 9 Sovereign Stack services are now registered!"
echo ""
echo "Next steps:"
echo "  1. Access marketplace: https://portal.sankofa.nexus/marketplace"
echo "  2. Query via GraphQL API"
echo "  3. Browse Phoenix Cloud Services offerings"
echo ""
