# DBAOps Sovereign™ — Install OS prerequisites (Windows via winget)
# Run in PowerShell 5.1+ or 7+ from repository root (elevated recommended).
# See: docs/INSTALL-REQUIREMENTS.md

param(
    [switch]$IncludeDocker,
    [switch]$IncludePowerShell7,
    [switch]$IncludeGit,
    [switch]$IncludeSqlExpress,
    [switch]$WhatIf
)

$ErrorActionPreference = "Stop"

function Test-Cmd($Name) {
    return [bool](Get-Command $Name -ErrorAction SilentlyContinue)
}

function Get-NodeMajor {
    if (-not (Test-Cmd node)) { return 0 }
    $raw = (& node -v 2>$null) -replace '^v', ''
    $maj = ($raw -split '\.')[0]
    return [int]$maj
}

function Invoke-WingetInstall {
    param([string]$Id, [string]$Label, [switch]$Required)
    if ($WhatIf) {
        Write-Host "[WhatIf] winget install -e --id $Id" -ForegroundColor Yellow
        return
    }
    Write-Host "Installing $Label ($Id)…" -ForegroundColor Cyan
    winget install -e --id $Id --accept-package-agreements --accept-source-agreements --silent
    if ($LASTEXITCODE -ne 0) {
        $msg = "winget exited $LASTEXITCODE for $Label ($Id). See docs/INSTALL-REQUIREMENTS.md for manual installers."
        if ($Required) { throw $msg }
        Write-Warning $msg
    }
}

if (-not (Test-Cmd winget)) {
    Write-Host "winget not found. Install 'App Installer' from the Microsoft Store, or install prerequisites manually:" -ForegroundColor Red
    Write-Host "  https://nodejs.org/  ·  https://www.docker.com/products/docker-desktop/" -ForegroundColor Yellow
    Write-Host "Full list: docs/INSTALL-REQUIREMENTS.md" -ForegroundColor Yellow
    exit 1
}

Write-Host "DBAOps Sovereign™ — prerequisites (Windows + winget)" -ForegroundColor Cyan
Write-Host ""

$nodeMaj = Get-NodeMajor
if ($nodeMaj -ge 18) {
    Write-Host "Node.js OK (major version $nodeMaj)." -ForegroundColor Green
} else {
    if ($nodeMaj -gt 0) {
        Write-Host "Node.js found but version is below 18 (have major $nodeMaj). Upgrading via winget…" -ForegroundColor Yellow
    }
    Invoke-WingetInstall -Id "OpenJS.NodeJS.LTS" -Label "Node.js LTS" -Required
    Write-Host "Open a NEW terminal and re-run this script to verify, or run: node -v" -ForegroundColor Yellow
}

if ($IncludeGit) {
    if (Test-Cmd git) {
        Write-Host "Git already on PATH." -ForegroundColor Green
    } else {
        Invoke-WingetInstall -Id "Git.Git" -Label "Git"
    }
}

if ($IncludePowerShell7) {
    if (Test-Cmd pwsh) {
        Write-Host "PowerShell 7 (pwsh) already on PATH." -ForegroundColor Green
    } else {
        Invoke-WingetInstall -Id "Microsoft.PowerShell" -Label "PowerShell 7"
    }
}

if ($IncludeDocker) {
    $dockerOk = Test-Cmd docker
    if (-not $dockerOk) {
        Invoke-WingetInstall -Id "Docker.DockerDesktop" -Label "Docker Desktop"
        Write-Host "Finish Docker Desktop first-run wizard, then reboot if prompted." -ForegroundColor Yellow
    } else {
        Write-Host "Docker CLI already on PATH." -ForegroundColor Green
    }
}

if ($IncludeSqlExpress) {
    Write-Host "SQL Server Express is large and may require reboot. Installing via winget…" -ForegroundColor Yellow
    Invoke-WingetInstall -Id "Microsoft.SQLServer.2022.Express" -Label "SQL Server 2022 Express"
}

Write-Host ""
Write-Host "Done. Next from repo root:" -ForegroundColor Green
Write-Host "  npm run setup" -ForegroundColor White
Write-Host "  npm start" -ForegroundColor White
Write-Host "Docs: docs/INSTALL-CROSS-PLATFORM.md" -ForegroundColor Gray
