# 启动检澜后端（释放 5000 端口后使用本项目 venv）
$ErrorActionPreference = 'Stop'
$root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
$backend = Join-Path $root 'BridgeDiseaseBackend-main'
$python = Join-Path $backend '.venv\Scripts\python.exe'

Get-NetTCPConnection -LocalPort 5000 -State Listen -ErrorAction SilentlyContinue |
  ForEach-Object { Stop-Process -Id $_.OwningProcess -Force -ErrorAction SilentlyContinue }
Start-Sleep -Seconds 2

$env:SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:bridgedisease_root@127.0.0.1:3307/bridge_disease?charset=utf8mb4'
$env:FLASK_RUN_HOST = '127.0.0.1'
$env:FLASK_RUN_PORT = '5000'

Set-Location $backend
& $python run.py
