#!/bin/bash
# Interactive setup script that prompts for database password

set -e

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

cd "$API_DIR"

echo "=========================================="
echo "Sovereign Stack Marketplace Setup"
echo "=========================================="
echo ""

# Check if .env exists, if not create from template
if [ ! -f .env ]; then
  echo "Creating .env file..."
  if [ -f .env.example ]; then
    cp .env.example .env
  else
    cat > .env << 'ENVEOF'
DB_HOST=localhost
DB_PORT=5432
DB_NAME=sankofa
DB_USER=postgres
DB_PASSWORD=
NODE_ENV=development
PORT=4000
ENVEOF
  fi
  echo "✅ .env file created"
  echo ""
fi

# Check if DB_PASSWORD is set and not placeholder
CURRENT_PASSWORD=$(grep "^DB_PASSWORD=" .env | cut -d'=' -f2- | tr -d '"' | tr -d "'")

if [ -z "$CURRENT_PASSWORD" ] || [ "$CURRENT_PASSWORD" = "your_secure_password_here" ] || [ "$CURRENT_PASSWORD" = "YOUR_ACTUAL_DATABASE_PASSWORD_HERE" ]; then
  echo "⚠ Database password not set or using placeholder."
  echo ""
  echo "Please enter your PostgreSQL database password:"
  echo "(For development: minimum 8 characters)"
  read -s DB_PASS
  echo ""
  
  if [ -z "$DB_PASS" ]; then
    echo "❌ Password cannot be empty"
    exit 1
  fi
  
  # Update .env with actual password
  if grep -q "^DB_PASSWORD=" .env; then
    sed -i "s|^DB_PASSWORD=.*|DB_PASSWORD=$DB_PASS|" .env
  else
    echo "DB_PASSWORD=$DB_PASS" >> .env
  fi
  
  echo "✅ Password updated in .env"
  echo ""
fi

# Ensure NODE_ENV is set to development
if ! grep -q "^NODE_ENV=" .env; then
  echo "NODE_ENV=development" >> .env
elif ! grep -q "^NODE_ENV=development" .env; then
  sed -i 's/^NODE_ENV=.*/NODE_ENV=development/' .env
fi

# Step 1: Run migrations
echo "Step 1: Running database migrations..."
echo "----------------------------------------"
if pnpm db:migrate:up; then
  echo "✅ Migrations completed"
else
  echo "❌ Migration failed."
  echo ""
  echo "Common issues:"
  echo "  1. Database password is incorrect"
  echo "  2. PostgreSQL is not running"
  echo "  3. Database 'sankofa' does not exist"
  echo ""
  echo "To fix:"
  echo "  1. Verify PostgreSQL is running: sudo systemctl status postgresql"
  echo "  2. Create database if needed: createdb sankofa"
  echo "  3. Update .env with correct password"
  exit 1
fi
echo ""

# Step 2: Seed Sovereign Stack services
echo "Step 2: Seeding Sovereign Stack services..."
echo "----------------------------------------"
if pnpm db:seed:sovereign-stack; then
  echo "✅ Services seeded"
else
  echo "❌ Seeding failed. Please check the error above."
  exit 1
fi
echo ""

# Step 3: Verify setup
echo "Step 3: Verifying setup..."
echo "----------------------------------------"
if pnpm verify:sovereign-stack; then
  echo ""
  echo "=========================================="
  echo "✅ Sovereign Stack setup complete!"
  echo "=========================================="
  echo ""
  echo "Next steps:"
  echo "1. Access the marketplace at: https://portal.sankofa.nexus/marketplace"
  echo "2. Browse Phoenix Cloud Services offerings"
  echo "3. Subscribe to services as needed"
  echo ""
else
  echo "⚠ Verification found issues. Please review the output above."
  exit 1
fi
