# 🏎️ RR3 Local Testing Environment - Quick Start Script
# This script starts your RR3 Community Server for local APK testing

Write-Host ""
Write-Host "═══════════════════════════════════════" -ForegroundColor Cyan
Write-Host "  🏎️  RR3 Community Server - Local Test" -ForegroundColor Green
Write-Host "═══════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""

# Get local IP address
Write-Host "🔍 Finding your local IP address..." -ForegroundColor Yellow
$localIP = (Get-NetIPAddress -AddressFamily IPv4 | 
    Where-Object { 
        $_.InterfaceAlias -like '*Wi-Fi*' -or 
        $_.InterfaceAlias -like '*Ethernet*' -or
        $_.InterfaceAlias -like '*LAN*'
    } | 
    Where-Object { $_.IPAddress -notlike '169.254.*' -and $_.IPAddress -notlike '127.*' } |
    Select-Object -First 1).IPAddress

if (-not $localIP) {
    Write-Host "❌ Could not find local IP address!" -ForegroundColor Red
    Write-Host "   Make sure you're connected to WiFi or Ethernet" -ForegroundColor Yellow
    pause
    exit 1
}

Write-Host "✅ Local IP found: $localIP" -ForegroundColor Green
Write-Host ""

# Display connection info
Write-Host "─────────────────────────────────────" -ForegroundColor DarkGray
Write-Host "📱 APK Configuration:" -ForegroundColor Cyan
Write-Host "   Server URL: http://${localIP}:5001" -ForegroundColor White
Write-Host ""
Write-Host "🌐 Admin Panel:" -ForegroundColor Cyan
Write-Host "   http://localhost:5001" -ForegroundColor White
Write-Host "   http://${localIP}:5001" -ForegroundColor White
Write-Host ""
Write-Host "🔌 API Endpoints:" -ForegroundColor Cyan
Write-Host "   Director: http://${localIP}:5001/director/api/android/getDirectionByPackage" -ForegroundColor White
Write-Host "   Assets:   http://${localIP}:5001/content/api/manifest" -ForegroundColor White
Write-Host "─────────────────────────────────────" -ForegroundColor DarkGray
Write-Host ""

# Check firewall rule
Write-Host "🔒 Checking Windows Firewall..." -ForegroundColor Yellow
$firewallRule = Get-NetFirewallRule -DisplayName "RR3 Community Server" -ErrorAction SilentlyContinue

if (-not $firewallRule) {
    Write-Host "⚠️  Port 5001 not open in firewall" -ForegroundColor Yellow
    Write-Host "   Creating firewall rule..." -ForegroundColor Yellow
    
    try {
        New-NetFirewallRule `
            -DisplayName "RR3 Community Server" `
            -Direction Inbound `
            -LocalPort 5001 `
            -Protocol TCP `
            -Action Allow `
            -Profile Any | Out-Null
        Write-Host "✅ Firewall rule created successfully" -ForegroundColor Green
    } catch {
        Write-Host "❌ Failed to create firewall rule (run as Administrator)" -ForegroundColor Red
        Write-Host "   You may need to manually allow port 5001" -ForegroundColor Yellow
    }
} else {
    Write-Host "✅ Firewall rule already exists" -ForegroundColor Green
}

Write-Host ""

# Check if server directory exists
if (-not (Test-Path "E:\rr3\RR3CommunityServer\RR3CommunityServer")) {
    Write-Host "❌ Server directory not found!" -ForegroundColor Red
    Write-Host "   Expected: E:\rr3\RR3CommunityServer\RR3CommunityServer" -ForegroundColor Yellow
    pause
    exit 1
}

# Check if dotnet is installed
Write-Host "🔍 Checking .NET installation..." -ForegroundColor Yellow
$dotnetVersion = dotnet --version 2>$null
if (-not $dotnetVersion) {
    Write-Host "❌ .NET SDK not found!" -ForegroundColor Red
    Write-Host "   Please install .NET 8 SDK: https://dotnet.microsoft.com/download" -ForegroundColor Yellow
    pause
    exit 1
}
Write-Host "✅ .NET $dotnetVersion installed" -ForegroundColor Green
Write-Host ""

# Check database
$dbPath = "E:\rr3\RR3CommunityServer\RR3CommunityServer\rr3community.db"
if (Test-Path $dbPath) {
    $dbSize = [math]::Round((Get-Item $dbPath).Length / 1KB, 2)
    Write-Host "✅ Database found ($dbSize KB)" -ForegroundColor Green
} else {
    Write-Host "⚠️  No database found (will be created on first run)" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "─────────────────────────────────────" -ForegroundColor DarkGray
Write-Host "📋 Quick Testing Checklist:" -ForegroundColor Cyan
Write-Host ""
Write-Host "  1. ✅ Server will start in a moment" -ForegroundColor White
Write-Host "  2. 📱 Configure APK with: http://${localIP}:5001" -ForegroundColor White
Write-Host "  3. 📡 Ensure phone is on same WiFi network" -ForegroundColor White
Write-Host "  4. 🎮 Install and launch modded APK" -ForegroundColor White
Write-Host "  5. 👀 Watch logs below for connection attempts" -ForegroundColor White
Write-Host ""
Write-Host "─────────────────────────────────────" -ForegroundColor DarkGray
Write-Host ""

Write-Host "🚀 Starting RR3 Community Server..." -ForegroundColor Green
Write-Host "   (Press Ctrl+C to stop)" -ForegroundColor DarkGray
Write-Host ""

# Change to server directory
Push-Location "E:\rr3\RR3CommunityServer\RR3CommunityServer"

# Start the server
try {
    dotnet run --urls "http://0.0.0.0:5001"
} finally {
    Pop-Location
}
