param(
    [string]$BaseUrl = "http://localhost",
    [string]$Hospital = "njch",
    [int]$Days = 30,
    [string]$EvidenceDir = "evidence"
)

$ErrorActionPreference = "Stop"
$stamp = Get-Date -Format "yyyyMMdd_HHmmss"
$root = Join-Path $PSScriptRoot ".."
$outDir = Join-Path $root "$EvidenceDir\E20_H1065_$stamp"
New-Item -ItemType Directory -Force -Path $outDir | Out-Null

$qaUrl = "$BaseUrl/public/executive-review-dashboard-qa.php?hospital=$Hospital&days=$Days"
$dashboardUrl = "$BaseUrl/public/index.php?page=action-review-dashboard&lang=en&days=$Days"

Write-Host "Phase 10.6.5 — Executive Review Dashboard QA" -ForegroundColor Cyan
Write-Host "QA: $qaUrl"

$qa = Invoke-WebRequest -UseBasicParsing -Uri $qaUrl -TimeoutSec 60
$qa.Content | Set-Content -Encoding UTF8 (Join-Path $outDir "executive_review_dashboard_qa.txt")

$dashboard = Invoke-WebRequest -UseBasicParsing -Uri $dashboardUrl -TimeoutSec 60
$dashboard.Content | Set-Content -Encoding UTF8 (Join-Path $outDir "executive_review_dashboard.html")

$summary = @()
$summary += "Phase: 10.6.5"
$summary += "Hospital: $Hospital"
$summary += "Days: $Days"
$summary += "QA HTTP: $($qa.StatusCode)"
$summary += "Dashboard HTTP: $($dashboard.StatusCode)"
$summary += "QA FAIL lines: $((($qa.Content -split "`n") | Where-Object { $_ -match '^\[FAIL\]' }).Count)"
$summary += "QA PASS lines: $((($qa.Content -split "`n") | Where-Object { $_ -match '^\[PASS\]' }).Count)"
$summary | Set-Content -Encoding UTF8 (Join-Path $outDir "summary.txt")

$zip = Join-Path $root "$EvidenceDir\evidence_E20_H1065_$stamp.zip"
Compress-Archive -Path "$outDir\*" -DestinationPath $zip -Force

Write-Host "Evidence: $zip" -ForegroundColor Green
if ($qa.Content -match '^\[FAIL\]' -or $dashboard.StatusCode -ne 200) {
    Write-Host "Result: FAIL" -ForegroundColor Red
    exit 1
}
Write-Host "Result: PASS" -ForegroundColor Green
