# Geutebruck API - Development Environment Setup Script
# This script sets up the complete development environment

param(
    [switch]$SkipPython,
    [switch]$SkipDotnet,
    [switch]$SkipDatabase,
    [switch]$SkipRedis
)

$ErrorActionPreference = "Stop"

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Geutebruck API - Development Setup" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

$RepoRoot = Split-Path -Parent $PSScriptRoot

# Function to check if command exists
function Test-Command {
    param($Command)
    $null = Get-Command $Command -ErrorAction SilentlyContinue
    return $?
}

# Check Prerequisites
Write-Host "[1/8] Checking prerequisites..." -ForegroundColor Yellow

if (-not $SkipPython) {
    if (-not (Test-Command python)) {
        Write-Host "ERROR: Python 3.11+ is required but not found" -ForegroundColor Red
        Write-Host "Please install Python from https://www.python.org/downloads/" -ForegroundColor Red
        exit 1
    }
    $pythonVersion = python --version
    Write-Host "  ✓ Python found: $pythonVersion" -ForegroundColor Green
}

if (-not $SkipDotnet) {
    if (-not (Test-Command dotnet)) {
        Write-Host "ERROR: .NET 8.0 SDK is required but not found" -ForegroundColor Red
        Write-Host "Please install from https://dotnet.microsoft.com/download" -ForegroundColor Red
        exit 1
    }
    $dotnetVersion = dotnet --version
    Write-Host "  ✓ .NET SDK found: $dotnetVersion" -ForegroundColor Green
}

# Create .env file if it doesn't exist
Write-Host "[2/8] Setting up environment configuration..." -ForegroundColor Yellow
if (-not (Test-Path "$RepoRoot\.env")) {
    Copy-Item "$RepoRoot\.env.example" "$RepoRoot\.env"
    Write-Host "  ✓ Created .env file from .env.example" -ForegroundColor Green
    Write-Host "  ⚠ IMPORTANT: Edit .env to configure your settings!" -ForegroundColor Yellow
} else {
    Write-Host "  ✓ .env file already exists" -ForegroundColor Green
}

# Setup Python virtual environment
if (-not $SkipPython) {
    Write-Host "[3/8] Setting up Python virtual environment..." -ForegroundColor Yellow

    if (-not (Test-Path "$RepoRoot\.venv")) {
        python -m venv "$RepoRoot\.venv"
        Write-Host "  ✓ Created Python virtual environment" -ForegroundColor Green
    } else {
        Write-Host "  ✓ Virtual environment already exists" -ForegroundColor Green
    }

    # Activate virtual environment
    & "$RepoRoot\.venv\Scripts\Activate.ps1"

    # Upgrade pip
    python -m pip install --upgrade pip | Out-Null

    # Install Python dependencies
    Write-Host "[4/8] Installing Python dependencies..." -ForegroundColor Yellow
    pip install -r "$RepoRoot\requirements.txt"
    Write-Host "  ✓ Python dependencies installed" -ForegroundColor Green
} else {
    Write-Host "[3/8] Skipping Python setup" -ForegroundColor Gray
    Write-Host "[4/8] Skipping Python dependencies" -ForegroundColor Gray
}

# Build SDK Bridge
if (-not $SkipDotnet) {
    Write-Host "[5/8] Building SDK Bridge (.NET gRPC service)..." -ForegroundColor Yellow

    $sdkBridgePath = "$RepoRoot\src\sdk-bridge\GeViScopeBridge"
    if (Test-Path "$sdkBridgePath\GeViScopeBridge.csproj") {
        Push-Location $sdkBridgePath
        dotnet restore
        dotnet build --configuration Debug
        Pop-Location
        Write-Host "  ✓ SDK Bridge built successfully" -ForegroundColor Green
    } else {
        Write-Host "  ⚠ SDK Bridge project not found, skipping" -ForegroundColor Yellow
    }
} else {
    Write-Host "[5/8] Skipping .NET build" -ForegroundColor Gray
}

# Setup PostgreSQL Database
if (-not $SkipDatabase) {
    Write-Host "[6/8] Setting up PostgreSQL database..." -ForegroundColor Yellow

    if (Test-Command psql) {
        # Create database
        Write-Host "  Creating database 'geutebruck_api'..." -ForegroundColor Cyan
        $createDbCommand = @"
CREATE DATABASE geutebruck_api;
CREATE USER geutebruck WITH PASSWORD 'geutebruck';
GRANT ALL PRIVILEGES ON DATABASE geutebruck_api TO geutebruck;
"@

        Write-Host "  Run these commands manually in psql:" -ForegroundColor Yellow
        Write-Host $createDbCommand -ForegroundColor White
        Write-Host ""
        Write-Host "  Then run: alembic upgrade head" -ForegroundColor Yellow
    } else {
        Write-Host "  ⚠ PostgreSQL not found. Install PostgreSQL 14+ manually" -ForegroundColor Yellow
        Write-Host "  Download from: https://www.postgresql.org/download/windows/" -ForegroundColor Yellow
    }
} else {
    Write-Host "[6/8] Skipping database setup" -ForegroundColor Gray
}

# Check Redis
if (-not $SkipRedis) {
    Write-Host "[7/8] Checking Redis..." -ForegroundColor Yellow

    if (Test-Command redis-server) {
        Write-Host "  ✓ Redis found" -ForegroundColor Green
    } else {
        Write-Host "  ⚠ Redis not found. Install Redis for Windows:" -ForegroundColor Yellow
        Write-Host "  Option 1: choco install redis-64" -ForegroundColor Yellow
        Write-Host "  Option 2: Download from https://redis.io/download" -ForegroundColor Yellow
    }
} else {
    Write-Host "[7/8] Skipping Redis check" -ForegroundColor Gray
}

# Summary
Write-Host "[8/8] Setup complete!" -ForegroundColor Yellow
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Next Steps:" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "1. Edit .env file with your GeViServer credentials" -ForegroundColor White
Write-Host "2. Ensure PostgreSQL is running and database is created" -ForegroundColor White
Write-Host "3. Run database migrations: alembic upgrade head" -ForegroundColor White
Write-Host "4. Ensure Redis is running: redis-server" -ForegroundColor White
Write-Host "5. Start services: .\scripts\start_services.ps1" -ForegroundColor White
Write-Host ""
Write-Host "Development Environment Ready! 🚀" -ForegroundColor Green
