# UV 智能启动脚本 - 自动修复 PATH 并启动应用

Write-Host "🚀 UV 智能启动脚本" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan

# 自动修复 PATH 环境变量
function Fix-Path-Auto {
    Write-Host "🔧 检查并修复 PATH 环境变量..." -ForegroundColor Yellow
    
    $pythonScriptsPath = "C:\Users\马艺洁\AppData\Local\Programs\Python\Python312\Scripts"
    
    # 检查是否需要修复
    $needsFix = $false
    
    if (-not ($env:PATH -like "*$pythonScriptsPath*")) {
        Write-Host "✅ 添加正确的 Python Scripts 路径" -ForegroundColor Green
        $env:PATH = "$pythonScriptsPath;" + $env:PATH
        $needsFix = $true
    }
    
    if ($env:PATH -like "*C:;Users*") {
        Write-Host "✅ 修复 C:;Users 格式错误" -ForegroundColor Green
        $env:PATH = $env:PATH.Replace('C:;Users', 'C:\Users')
        $needsFix = $true
    }
    
    if ($env:PATH -like "*;%S;stemRoot%*") {
        Write-Host "✅ 修复 ;%S;stemRoot% 格式错误" -ForegroundColor Green
        $env:PATH = $env:PATH.Replace(';%S;stemRoot%', ';%SystemRoot%')
        $needsFix = $true
    }
    
    if (-not $needsFix) {
        Write-Host "✅ PATH 环境变量正常" -ForegroundColor Green
    }
}

# 执行修复
Fix-Path-Auto

# 切换到项目目录
Set-Location "d:\HuaweiMoveData\Users\马艺洁\Desktop\MLwork\bigwork"

# 验证 UV 命令
Write-Host "🔍 验证 UV 命令可用性..." -ForegroundColor Cyan
try {
    $uvVersion = uv --version 2>$null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ UV 命令可用: $uvVersion" -ForegroundColor Green
        Write-Host "🚀 使用 UV 启动 Streamlit 应用..." -ForegroundColor Green
        uv run streamlit run src/streamlit_app.py
    } else {
        throw "UV command failed"
    }
} catch {
    Write-Host "⚠️ UV 命令不可用，尝试使用 python -m uv..." -ForegroundColor Yellow
    python -m uv run streamlit run src/streamlit_app.py
}