# ==============================================================================
#  lf_mri_platform -- One-time installation script
#  Run as: .\install.ps1
#  Requires: Windows 10/11, PowerShell 5.1+, internet access
# ==============================================================================
param(
    [string]$PythonExe = "python"
)

$ErrorActionPreference = "Stop"
$Root    = $PSScriptRoot
$GuiDir  = Join-Path $Root "apps\gui"
$LibsDir = Join-Path $Root "libs\lf-scanner"

function Write-Step($msg) { Write-Host "`n==> $msg" -ForegroundColor Cyan }
function Write-OK($msg)   { Write-Host "    [OK] $msg" -ForegroundColor Green }
function Write-Warn($msg) { Write-Host "    [!!] $msg" -ForegroundColor Yellow }
function Write-Fail($msg) { Write-Host "    [FAIL] $msg" -ForegroundColor Red; exit 1 }

# -- 1. Check prerequisites ----------------------------------------------------
Write-Step "Checking prerequisites"

try {
    $pyVer = & $PythonExe --version 2>&1
    if ($pyVer -match "3\.(1[0-9]|[0-9]+)") { Write-OK "Python: $pyVer" }
    else { Write-Fail "Python 3.10+ required, found: $pyVer" }
} catch { Write-Fail "Python not found. Install from https://python.org (add to PATH)" }

try {
    $dockerVer = & docker --version 2>&1
    Write-OK "Docker: $dockerVer"
} catch {
    Write-Warn "Docker not found -- backend services will not start without it."
    Write-Warn "Install Docker Desktop: https://www.docker.com/products/docker-desktop/"
}

try {
    $composeVer = & docker compose version 2>&1
    Write-OK "Docker Compose: $composeVer"
} catch { Write-Warn "Docker Compose not found (included in Docker Desktop >= 3.0)" }

# -- 2. Create Python virtual environment for the GUI -------------------------
Write-Step "Creating Python virtual environment for GUI"

$venvPath = Join-Path $GuiDir ".venv"
if (-not (Test-Path $GuiDir)) { Write-Fail "GUI directory not found: $GuiDir" }

if (Test-Path $venvPath) {
    Write-Warn "Venv already exists at $venvPath -- skipping creation"
} else {
    & $PythonExe -m venv $venvPath
    Write-OK "Created venv at $venvPath"
}

$pip = Join-Path $venvPath "Scripts\pip.exe"

# -- 3. Install GUI dependencies ----------------------------------------------
Write-Step "Installing GUI dependencies"

$guiReqs = Join-Path $GuiDir "requirements.txt"
if (Test-Path $guiReqs) {
    & $pip install -r $guiReqs --quiet
    Write-OK "GUI requirements installed"
}

# Install lf-scanner as an editable package
$lfScannerPyproject = Join-Path $LibsDir "pyproject.toml"
$lfScannerSetup     = Join-Path $LibsDir "setup.py"

if ((Test-Path $lfScannerPyproject) -or (Test-Path $lfScannerSetup)) {
    & $pip install -e $LibsDir --quiet
    Write-OK "lf-scanner installed (editable)"
} else {
    Write-Warn "lf-scanner has no pyproject.toml/setup.py -- adding to PYTHONPATH only"
}

# -- 4. Copy .env for Docker services -----------------------------------------
Write-Step "Setting up Docker environment config"

$envFile    = Join-Path $Root ".env"
$envExample = Join-Path $Root ".env.example"

if (-not (Test-Path $envFile)) {
    Copy-Item $envExample $envFile
    Write-OK ".env created from .env.example"
} else {
    Write-Warn ".env already exists -- not overwriting"
}

# -- 5. Create desktop shortcuts ----------------------------------------------
Write-Step "Creating desktop shortcuts"

$desktopPath   = [Environment]::GetFolderPath("Desktop")
$pythonExePath = Join-Path $venvPath "Scripts\python.exe"
$appScript     = Join-Path $GuiDir "app.py"

$wsh = New-Object -ComObject WScript.Shell

# GUI only
$sc = $wsh.CreateShortcut("$desktopPath\LF-MRI GUI.lnk")
$sc.TargetPath       = $pythonExePath
$sc.Arguments        = "`"$appScript`""
$sc.WorkingDirectory = $Root
$sc.Description      = "LF-MRI System GUI"
$sc.Save()
Write-OK "Shortcut: 'LF-MRI GUI.lnk' on Desktop"

# Start all
$scAll = $wsh.CreateShortcut("$desktopPath\LF-MRI Start All.lnk")
$scAll.TargetPath       = "powershell.exe"
$scAll.Arguments        = "-ExecutionPolicy Bypass -File `"$Root\start.ps1`""
$scAll.WorkingDirectory = $Root
$scAll.Description      = "Start LF-MRI services + GUI"
$scAll.Save()
Write-OK "Shortcut: 'LF-MRI Start All.lnk' on Desktop"

# -- 6. Summary ---------------------------------------------------------------
Write-Host ""
Write-Host "============================================================" -ForegroundColor Green
Write-Host "  Installation complete!" -ForegroundColor Green
Write-Host "============================================================" -ForegroundColor Green
Write-Host ""
Write-Host "  Next steps:"
Write-Host "  1. Start all services + GUI:   .\start.ps1"
Write-Host "  2. GUI only (no services):     .\start.ps1 -GuiOnly"
Write-Host "  3. Hardware mode:              .\start.ps1 -Mode real"
Write-Host "  4. Stop services:              .\stop.ps1"
Write-Host ""
Write-Host "  Config: edit .env to change ports or switch mode (plug/real)"
Write-Host ""
