#!/usr/bin/env powershell
# EG Windows 构建环境检查脚本

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  EG 构建环境检查" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan

$Issues = @()
$Warnings = @()

# 检查 Python
Write-Host "`n[1/6] 检查 Python..." -ForegroundColor Yellow
try {
    $PythonVersion = python --version 2>&1
    if ($PythonVersion -match "Python (\d+)\.(\d+)") {
        $Major = [int]$Matches[1]
        $Minor = [int]$Matches[2]
        if ($Major -ge 3 -and $Minor -ge 8) {
            Write-Host "  ✓ $PythonVersion (符合要求)" -ForegroundColor Green
        } else {
            Write-Host "  ⚠ $PythonVersion (建议 3.8+)" -ForegroundColor Yellow
            $Warnings += "Python 版本较旧"
        }
    }
} catch {
    Write-Host "  ✗ Python 未找到" -ForegroundColor Red
    $Issues += "需要安装 Python 3.8+"
}

# 检查 pip
Write-Host "`n[2/6] 检查 pip..." -ForegroundColor Yellow
try {
    $PipVersion = pip --version 2>&1
    Write-Host "  ✓ $PipVersion" -ForegroundColor Green
} catch {
    Write-Host "  ✗ pip 未找到" -ForegroundColor Red
    $Issues += "需要安装 pip"
}

# 检查 PowerShell 执行策略
Write-Host "`n[3/6] 检查 PowerShell 执行策略..." -ForegroundColor Yellow
$Policy = Get-ExecutionPolicy
if ($Policy -eq "Restricted") {
    Write-Host "  ✗ 当前策略: $Policy (需要修改)" -ForegroundColor Red
    $Issues += "执行策略需要设置为 RemoteSigned: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser"
} else {
    Write-Host "  ✓ 当前策略: $Policy" -ForegroundColor Green
}

# 检查必要模块
Write-Host "`n[4/6] 检查 Python 模块..." -ForegroundColor Yellow
$Modules = @("nuitka", "pyinstaller")
foreach ($mod in $Modules) {
    $result = pip show $mod 2>$null
    if ($result) {
        $version = ($result | Select-String "Version:").ToString().Split(":")[1].Trim()
        Write-Host "  ✓ $mod $version" -ForegroundColor Green
    } else {
        Write-Host "  ⚠ $mod 未安装 (将自动安装)" -ForegroundColor Yellow
    }
}

# 检查项目依赖
Write-Host "`n[5/6] 检查项目依赖..." -ForegroundColor Yellow
$ReqFile = "$PSScriptRoot\..\requirements\requirements.txt"
if (Test-Path $ReqFile) {
    Write-Host "  ✓ 找到 requirements.txt" -ForegroundColor Green
    
    # 检查几个关键包
    $KeyPackages = @("Panda3D", "PyQt5", "openvr", "imgui-bundle")
    foreach ($pkg in $KeyPackages) {
        $result = pip show $pkg 2>$null
        if ($result) {
            Write-Host "  ✓ $pkg" -ForegroundColor Green
        } else {
            Write-Host "  ⚠ $pkg 未安装" -ForegroundColor Yellow
        }
    }
} else {
    Write-Host "  ✗ 未找到 requirements.txt" -ForegroundColor Red
    $Warnings += "未找到依赖文件"
}

# 检查 Inno Setup
Write-Host "`n[6/6] 检查 Inno Setup..." -ForegroundColor Yellow
$ISCCPaths = @(
    "C:\Program Files (x86)\Inno Setup 6\ISCC.exe",
    "C:\Program Files\Inno Setup 6\ISCC.exe"
)
$Found = $false
foreach ($path in $ISCCPaths) {
    if (Test-Path $path) {
        Write-Host "  ✓ 找到: $path" -ForegroundColor Green
        $Found = $true
        break
    }
}
if (!$Found) {
    Write-Host "  ⚠ Inno Setup 未找到 (可选，用于创建安装程序)" -ForegroundColor Yellow
    Write-Host "    下载: https://jrsoftware.org/isdl.php" -ForegroundColor Gray
    $Warnings += "Inno Setup 未安装 (可选)"
}

# 检查编译器
Write-Host "`n[7/6] 检查 C++ 编译器..." -ForegroundColor Yellow
$CL = Get-Command cl -ErrorAction SilentlyContinue
$GCC = Get-Command gcc -ErrorAction SilentlyContinue

if ($CL) {
    Write-Host "  ✓ 找到 MSVC (cl)" -ForegroundColor Green
} elseif ($GCC) {
    Write-Host "  ✓ 找到 GCC" -ForegroundColor Green
} else {
    Write-Host "  ⚠ 未找到 C++ 编译器" -ForegroundColor Yellow
    Write-Host "    Nuitka 将尝试自动安装 MinGW64 或使用已安装的 Visual Studio" -ForegroundColor Gray
    $Warnings += "未找到 C++ 编译器 (Nuitka 会尝试自动解决)"
}

# 总结
Write-Host "`n========================================" -ForegroundColor Cyan
if ($Issues.Count -eq 0 -and $Warnings.Count -eq 0) {
    Write-Host "  ✓ 环境检查通过!" -ForegroundColor Green
    Write-Host "  可以开始构建" -ForegroundColor Green
    exit 0
} else {
    if ($Issues.Count -gt 0) {
        Write-Host "  发现问题 ($($Issues.Count)):" -ForegroundColor Red
        foreach ($issue in $Issues) {
            Write-Host "    - $issue" -ForegroundColor Red
        }
    }
    if ($Warnings.Count -gt 0) {
        Write-Host "  警告 ($($Warnings.Count)):" -ForegroundColor Yellow
        foreach ($warn in $Warnings) {
            Write-Host "    - $warn" -ForegroundColor Yellow
        }
    }
    exit 1
}
