#!/bin/bash

# Setup Environment Variables for Development
# Thiết lập Environment Variables cho Development

set -e

echo "🔧 Setting up environment variables..."

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# EN: Check if we're in the project root
# VI: Kiểm tra xem có đang ở root project không
if [ ! -f "package.json" ]; then
    echo -e "${RED}❌ Error: Must run from project root / Lỗi: Phải chạy từ root project${NC}"
    exit 1
fi

# EN: Setup shared environment
# VI: Thiết lập môi trường chia sẻ
echo ""
echo "📦 Step 1: Setup shared environment (deployments/local/.env.local)"
if [ -f "deployments/local/.env.local" ]; then
    echo -e "${YELLOW}⚠️  deployments/local/.env.local already exists / đã tồn tại${NC}"
    read -p "Overwrite? (y/n): / Ghi đè? (y/n): " overwrite
    if [ "$overwrite" != "y" ]; then
        echo "Skipping shared environment setup / Bỏ qua thiết lập môi trường chia sẻ"
    else
        cp deployments/local/env.local.example deployments/local/.env.local
        echo -e "${GREEN}✅ Created deployments/local/.env.local${NC}"
    fi
else
    cp deployments/local/env.local.example deployments/local/.env.local
    echo -e "${GREEN}✅ Created deployments/local/.env.local${NC}"
fi

# EN: Setup service-specific environments
# VI: Thiết lập môi trường cụ thể cho từng service
echo ""
echo "📦 Step 2: Setup service-specific environments"

# EN: Function to setup service env
# VI: Hàm để setup env cho service
setup_service_env() {
    local service=$1
    local service_path="services/$service"
    
    if [ ! -d "$service_path" ]; then
        echo -e "${YELLOW}⚠️  Service $service not found, skipping${NC}"
        return
    fi
    
    if [ -f "$service_path/.env.local" ]; then
        echo -e "${YELLOW}⚠️  $service_path/.env.local already exists, skipping${NC}"
    else
        if [ -f "$service_path/env.local.example" ]; then
            cp "$service_path/env.local.example" "$service_path/.env.local"
            echo -e "${GREEN}✅ Created $service_path/.env.local${NC}"
        else
            echo -e "${YELLOW}⚠️  $service_path/env.local.example not found, skipping${NC}"
        fi
    fi
}

# Setup for all services
for service_dir in services/*/; do
    if [ -d "$service_dir" ]; then
        service_name=$(basename "$service_dir")
        if [ "$service_name" != "_template" ]; then
            setup_service_env "$service_name"
        fi
    fi
done

# Summary
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo -e "${GREEN}✅ Environment setup complete!${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "📝 Next steps:"
echo ""
echo "1. Edit deployments/local/.env.local:"
echo "   - Set JWT_SECRET and JWT_REFRESH_SECRET"
echo ""
echo "2. Edit services/<service>/.env.local for each service:"
echo "   - Set DATABASE_URL from Neon Console"
echo "   - Each service needs its own database"
echo ""
echo "3. Create databases in Neon:"
echo "   - goodgo_iam_dev (for iam-service)"
echo "   - goodgo_user_dev (for user-service)"
echo "   - etc."
echo ""
echo "4. Run migrations:"
echo "   ./scripts/db/migrate.sh iam-service dev"
echo ""
echo "5. Start development:"
echo "   ./scripts/dev/start-all.sh"
echo ""
echo "📚 See docs/vi/guides/local-development.md for details"
echo ""
