# Environment check script
# 设置控制台编码为 UTF-8，避免乱码
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
chcp 65001 | Out-Null

Write-Host "Checking development environment..." -ForegroundColor Cyan
Write-Host "================================" -ForegroundColor Cyan

# Check Node.js
Write-Host "`nChecking Node.js..." -ForegroundColor Yellow
$nodeVersion = node --version 2>$null
if ($nodeVersion) {
    Write-Host "[OK] Node.js: $nodeVersion" -ForegroundColor Green
} else {
    Write-Host "[X] Node.js is not installed" -ForegroundColor Red
    # Download nodejs
    Write-Host "Downloading Node.js..." -ForegroundColor Yellow
    npm install -g node
    if ($LASTEXITCODE -ne 0) {
        Write-Host "[X] Node.js download failed" -ForegroundColor Red
        exit 1
    }
    Write-Host "[OK] Node.js downloaded successfully" -ForegroundColor Green
    node --version
}

# Check npm
Write-Host "`nChecking npm..." -ForegroundColor Yellow
$npmVersion = npm --version 2>$null
if ($npmVersion) {
    Write-Host "[OK] npm: $npmVersion" -ForegroundColor Green
} else {
    Write-Host "[X] npm is not installed" -ForegroundColor Red
    # Download npm
    Write-Host "Downloading npm..." -ForegroundColor Yellow
    npm install -g npm
    if ($LASTEXITCODE -ne 0) {
        Write-Host "[X] npm download failed" -ForegroundColor Red
        exit 1
    }
    Write-Host "[OK] npm downloaded successfully" -ForegroundColor Green
    npm --version
}

# Check if dependencies are installed
Write-Host "`nChecking project dependencies..." -ForegroundColor Yellow

# 调用 nodejs-dependencies-install.js 脚本进行依赖检查和安装
$nodeDependenciesScript = Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Path) "nodejs\dependences\nodejs-dependencies-install.js"

if (Test-Path $nodeDependenciesScript) {
    node $nodeDependenciesScript
    if ($LASTEXITCODE -ne 0) {
        Write-Host "[X] Node dependencies check/installation failed" -ForegroundColor Red
        exit 1
    }
} else {
    Write-Host "[X] nodejs-dependencies-install.js not found at: $nodeDependenciesScript" -ForegroundColor Red
    Write-Host "[WARN] Continuing without dependency check..." -ForegroundColor Yellow
}

# Check if python is installed
Write-Host "`nChecking if python is installed..." -ForegroundColor Yellow
$pythonVersion = python --version 2>$null
if ($pythonVersion) {
    Write-Host "[OK] python: $pythonVersion" -ForegroundColor Green
} else {
    Write-Host "[X] python is not installed" -ForegroundColor Red
    # Download python
    Write-Host "Downloading python..." -ForegroundColor Yellow
    npm install -g python
    if ($LASTEXITCODE -ne 0) {
        Write-Host "[X] python download failed" -ForegroundColor Red
        exit 1
    }
    Write-Host "[OK] python downloaded successfully" -ForegroundColor Green
    python --version
}

# check pip is installed
Write-Host "`nChecking pip is installed..." -ForegroundColor Yellow
$pipVersion = pip --version 2>$null
if ($pipVersion) {
    Write-Host "[OK] pip: $pipVersion" -ForegroundColor Green
} else {
    Write-Host "[X] pip is not installed" -ForegroundColor Red
    Write-Host "Installing pip..." -ForegroundColor Yellow
    pip install --upgrade pip
    if ($LASTEXITCODE -ne 0) {
        Write-Host "[X] pip installation failed" -ForegroundColor Red
        exit 1
    }
    Write-Host "[OK] pip installed successfully" -ForegroundColor Green
    pip --version
}


#check python virtual environment
Write-Host "`nChecking python virtual environment..." -ForegroundColor Yellow
$venvPath = "./python/env"
if (Test-Path $venvPath) {
    Write-Host "[OK] python virtual environment exists at: $venvPath" -ForegroundColor Green
} else {
    Write-Host "[X] python virtual environment is not installed" -ForegroundColor Yellow
    Write-Host "Creating python virtual environment..." -ForegroundColor Yellow
    python -m venv $venvPath
    if ($LASTEXITCODE -ne 0) {
        Write-Host "[X] python virtual environment creation failed" -ForegroundColor Red
        Write-Host "[WARN] Continuing without virtual environment..." -ForegroundColor Yellow
    } else {
        Write-Host "[OK] python virtual environment created successfully" -ForegroundColor Green
    }
}

# check python dependencies
Write-Host "`nChecking python dependencies..." -ForegroundColor Yellow

# 调用 python-enviroment-install.py 脚本进行依赖检查和安装（安装到虚拟环境）
$pythonDependenciesScript = Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Path) "python\python-enviroment-install.py"

if (Test-Path $pythonDependenciesScript) {
    python $pythonDependenciesScript
    if ($LASTEXITCODE -ne 0) {
        Write-Host "[X] Python dependencies check/installation failed" -ForegroundColor Red
        exit 1
    }
} else {
    Write-Host "[X] python-enviroment-install.py not found at: $pythonDependenciesScript" -ForegroundColor Red
    Write-Host "[WARN] Continuing without Python dependency check..." -ForegroundColor Yellow
}

Write-Host "`n================================" -ForegroundColor Cyan
Write-Host "Environment check completed!" -ForegroundColor Green
Write-Host "All dependencies are ready. You can now start the project." -ForegroundColor Green
