#!/bin/bash

set -e

echo "🚀 Neon Database Setup Script"
echo ""

# Check if Neon CLI is installed
if ! command -v neonctl &> /dev/null; then
    echo "⚠️  Neon CLI not found. Installing..."
    echo "Visit: https://neon.tech/docs/cli/install"
    echo ""
    read -p "Do you want to continue with manual setup? (y/n): " continue
    if [ "$continue" != "y" ]; then
        exit 1
    fi
fi

echo "This script will help you set up Neon database branches."
echo ""
echo "Prerequisites:"
echo "1. Neon account created at https://neon.tech"
echo "2. Neon project created"
echo "3. Neon CLI installed (optional)"
echo ""

read -p "Enter your Neon project name (default: goodgo-platform): " project_name
project_name=${project_name:-goodgo-platform}

echo ""
echo "📋 Setup Steps:"
echo ""
echo "1. Go to https://console.neon.tech"
echo "2. Select your project: $project_name"
echo "3. Create branches:"
echo "   - staging (from main)"
echo "   - production (from main)"
echo ""
echo "4. Get connection strings for each branch:"
echo "   - Development (main branch)"
echo "   - Staging branch"
echo "   - Production branch"
echo ""
echo "5. Update environment files:"
echo "   - deployments/local/.env.local"
echo "   - GitHub Secrets (for CI/CD)"
echo "   - Kubernetes Secrets (for deployments)"
echo ""

read -p "Enter development DATABASE_URL (or press Enter to skip): " dev_url
if [ -n "$dev_url" ]; then
    echo "DATABASE_URL=$dev_url" >> deployments/local/.env.local
    echo "✅ Added to deployments/local/.env.local"
fi

echo ""
echo "📝 Next steps:"
echo ""
echo "1. Add staging URL to GitHub Secrets: NEON_DATABASE_URL_STAGING"
echo "2. Add production URL to GitHub Secrets: NEON_DATABASE_URL_PRODUCTION"
echo "3. Create Kubernetes secrets for staging/production"
echo "4. Run migrations: ./scripts/db/migrate.sh iam-service dev"
echo ""
echo "✅ Setup complete! See infra/databases/neon/README.md for details."
