# QR Master - Quick Setup Script (PowerShell for Windows)
# This script automates the initial setup process

Write-Host "🚀 QR Master - Quick Setup" -ForegroundColor Cyan
Write-Host "================================" -ForegroundColor Cyan
Write-Host ""

# Check if Docker is installed
try {
    docker --version | Out-Null
    Write-Host "✓ Docker is installed" -ForegroundColor Green
} catch {
    Write-Host "❌ Docker is not installed. Please install Docker Desktop first." -ForegroundColor Red
    exit 1
}

# Check if Docker Compose is installed
try {
    docker-compose --version | Out-Null
    Write-Host "✓ Docker Compose is installed" -ForegroundColor Green
} catch {
    Write-Host "❌ Docker Compose is not installed. Please install Docker Desktop first." -ForegroundColor Red
    exit 1
}

Write-Host ""

# Check if .env exists
if (-Not (Test-Path .env)) {
    Write-Host "📝 Creating .env file from template..." -ForegroundColor Yellow
    Copy-Item env.example .env
    
    # Generate secrets
    $NEXTAUTH_SECRET = [Convert]::ToBase64String((1..32 | ForEach-Object { Get-Random -Maximum 256 }))
    $IP_SALT = [Convert]::ToBase64String((1..32 | ForEach-Object { Get-Random -Maximum 256 }))
    
    # Update .env with generated secrets
    (Get-Content .env) -replace 'NEXTAUTH_SECRET=.*', "NEXTAUTH_SECRET=$NEXTAUTH_SECRET" | Set-Content .env
    (Get-Content .env) -replace 'IP_SALT=.*', "IP_SALT=$IP_SALT" | Set-Content .env
    
    Write-Host "✓ Generated secure secrets" -ForegroundColor Green
} else {
    Write-Host "✓ .env file already exists" -ForegroundColor Green
}

Write-Host ""

# Ask user what mode they want
Write-Host "Choose setup mode:"
Write-Host "1) Development (database only in Docker, app on host)"
Write-Host "2) Production (full stack in Docker)"
$choice = Read-Host "Enter choice [1-2]"

Write-Host ""

switch ($choice) {
    "1" {
        Write-Host "🔧 Setting up development environment..." -ForegroundColor Cyan
        Write-Host ""
        
        # Start database services
        Write-Host "Starting PostgreSQL and Redis..."
        docker-compose -f docker-compose.dev.yml up -d
        
        # Wait for database to be ready
        Write-Host "Waiting for database to be ready..."
        Start-Sleep -Seconds 5
        
        # Install dependencies
        Write-Host "Installing dependencies..."
        npm install
        
        # Run migrations
        Write-Host "Running database migrations..."
        npm run db:migrate
        
        # Seed database
        Write-Host "Seeding database with demo data..."
        npm run db:seed
        
        Write-Host ""
        Write-Host "✅ Development environment ready!" -ForegroundColor Green
        Write-Host ""
        Write-Host "To start the application:"
        Write-Host "  npm run dev"
        Write-Host ""
        Write-Host "Access points:"
        Write-Host "  - App: http://localhost:3050"
        Write-Host "  - Database UI: http://localhost:8080"
        Write-Host "  - Database: localhost:5435"
        Write-Host "  - Redis: localhost:6379"
    }
    "2" {
        Write-Host "🚀 Setting up production environment..." -ForegroundColor Cyan
        Write-Host ""
        
        # Build and start all services
        Write-Host "Building and starting all services..."
        docker-compose up -d --build
        
        # Wait for services to be ready
        Write-Host "Waiting for services to be ready..."
        Start-Sleep -Seconds 10
        
        # Run migrations
        Write-Host "Running database migrations..."
        docker-compose exec web npx prisma migrate deploy
        
        # Seed database
        Write-Host "Seeding database with demo data..."
        docker-compose exec web npm run db:seed
        
        Write-Host ""
        Write-Host "✅ Production environment ready!" -ForegroundColor Green
        Write-Host ""
        Write-Host "Access points:"
        Write-Host "  - App: http://localhost:3050"
        Write-Host "  - Database: localhost:5435"
        Write-Host "  - Redis: localhost:6379"
        Write-Host ""
        Write-Host "To view logs:"
        Write-Host "  docker-compose logs -f"
    }
    default {
        Write-Host "❌ Invalid choice. Exiting." -ForegroundColor Red
        exit 1
    }
}

Write-Host ""
Write-Host "📚 Documentation:"
Write-Host "  - Quick start: README.md"
Write-Host "  - Docker guide: DOCKER_SETUP.md"
Write-Host "  - Migration guide: MIGRATION_FROM_SUPABASE.md"
Write-Host ""
Write-Host "🎉 Setup complete! Happy coding!" -ForegroundColor Green

