# Ralph Wiggum: Windows PowerShell Runner
# 
# Main entry point for running Ralph on Windows
# This script handles the Windows-specific workaround

param(
    [int]$MaxIterations = 200,
    [string]$Model = "gpt-5.2-high",
    [string]$Branch = "feature/ralph-backend-cleanup"
)

$ErrorActionPreference = "Stop"

Write-Host "═══════════════════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host "🚀 Ralph Backend Hardening Loop (Windows)" -ForegroundColor Cyan
Write-Host "═══════════════════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""

$Workspace = (Get-Location).Path
$TaskFile = Join-Path $Workspace "RALPH_TASK.md"

# Check prerequisites
if (-not (Test-Path $TaskFile)) {
    Write-Host "❌ RALPH_TASK.md not found. Please create it first." -ForegroundColor Red
    exit 1
}

# Check if git repo
try {
    git rev-parse --git-dir 2>$null | Out-Null
    if ($LASTEXITCODE -ne 0) {
        Write-Host "⚠️  Not in a git repo. Initializing..." -ForegroundColor Yellow
        git init
    }
} catch {
    Write-Host "⚠️  Git not found. Please install Git for Windows." -ForegroundColor Yellow
}

Write-Host "Workspace: $Workspace" -ForegroundColor Green
Write-Host "Max Iterations: $MaxIterations" -ForegroundColor Green
Write-Host "Model: $Model" -ForegroundColor Green
Write-Host "Branch: $Branch" -ForegroundColor Green
Write-Host ""

# Check if WSL is available
$WSLAvailable = $false
try {
    wsl --list --quiet 2>$null | Out-Null
    if ($LASTEXITCODE -eq 0) {
        $WSLAvailable = $true
        Write-Host "✅ WSL detected! You can run bash scripts via WSL." -ForegroundColor Green
        Write-Host ""
        Write-Host "To run Ralph via WSL:" -ForegroundColor Cyan
        Write-Host "  wsl bash -c `"cd /mnt/e/opennotion && ./.cursor/ralph-scripts/ralph-once.sh`"" -ForegroundColor White
        Write-Host ""
    }
} catch {
    Write-Host "⚠️  WSL not detected. Using Cursor chat method instead." -ForegroundColor Yellow
}

Write-Host "═══════════════════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host "📝 Recommended Approach for Windows" -ForegroundColor Cyan
Write-Host "═══════════════════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""
Write-Host "Since cursor-agent CLI is not available on Windows, use one of:" -ForegroundColor Yellow
Write-Host ""
Write-Host "Option 1: Use Cursor Chat (Recommended)" -ForegroundColor Cyan
Write-Host "─────────────────────────────────────────────────────────────────" -ForegroundColor Gray
Write-Host "1. In Cursor IDE, open this workspace" -ForegroundColor White
Write-Host "2. Use command: /ralph-loop" -ForegroundColor White
Write-Host "3. The agent will work on RALPH_TASK.md iteratively" -ForegroundColor White
Write-Host ""
Write-Host "Option 2: Use WSL (if available)" -ForegroundColor Cyan
Write-Host "─────────────────────────────────────────────────────────────────" -ForegroundColor Gray
if ($WSLAvailable) {
    Write-Host "Run in WSL:" -ForegroundColor White
    Write-Host "  wsl bash -c `"cd /mnt/e/opennotion && MAX_ITERATIONS=$MaxIterations RALPH_MODEL=$Model bash run_ralph_backend.sh`"" -ForegroundColor Cyan
} else {
    Write-Host "Install WSL first: wsl --install" -ForegroundColor White
    Write-Host "Then run bash scripts in WSL environment" -ForegroundColor White
}
Write-Host ""
Write-Host "Option 3: Manual Iteration" -ForegroundColor Cyan
Write-Host "─────────────────────────────────────────────────────────────────" -ForegroundColor Gray
Write-Host "1. Review RALPH_TASK.md" -ForegroundColor White
Write-Host "2. Use Cursor's built-in agent to work on unchecked criteria" -ForegroundColor White
Write-Host "3. Test: cd backend && pytest" -ForegroundColor White
Write-Host "4. Commit changes" -ForegroundColor White
Write-Host "5. Repeat until all criteria are checked" -ForegroundColor White
Write-Host ""
Write-Host "═══════════════════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""

# Create/update branch if needed
try {
    $CurrentBranch = git rev-parse --abbrev-ref HEAD 2>$null
    if ($CurrentBranch -ne $Branch) {
        Write-Host "📦 Creating/switching to branch: $Branch" -ForegroundColor Cyan
        git checkout -b $Branch 2>$null
        if ($LASTEXITCODE -ne 0) {
            git checkout $Branch 2>$null
        }
    }
} catch {
    Write-Host "⚠️  Could not create branch. Continuing on current branch." -ForegroundColor Yellow
}

Write-Host ""
Write-Host "✅ Setup complete!" -ForegroundColor Green
Write-Host "   Choose one of the options above to proceed." -ForegroundColor Green
Write-Host ""

