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

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

$qaUrl = "$BaseUrl/public/leadership-huddle-purpose-ux-qa.php?hospital=$Hospital"
$reportUrl = "$BaseUrl/public/index.php?page=leadership-huddle&lang=en"
$reportArUrl = "$BaseUrl/public/index.php?page=leadership-huddle&lang=ar"

Write-Host "Phase 10.7.1 - Leadership Huddle Purpose and Executive UX QA" -ForegroundColor Cyan
Write-Host "Hospital: $Hospital"
Write-Host "QA URL: $qaUrl"

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

    $report = Invoke-WebRequest -UseBasicParsing -Uri $reportUrl -TimeoutSec 60
    $report.Content | Set-Content -Encoding UTF8 (Join-Path $outDir "leadership_huddle_en.html")

    $reportAr = Invoke-WebRequest -UseBasicParsing -Uri $reportArUrl -TimeoutSec 60
    $reportAr.Content | Set-Content -Encoding UTF8 (Join-Path $outDir "leadership_huddle_ar.html")

    $qaLines = $qa.Content -split "`r?`n"
    $failCount = @($qaLines | Where-Object { $_ -match '^\[FAIL\]' }).Count
    $passCount = @($qaLines | Where-Object { $_ -match '^\[PASS\]' }).Count

    $htmlChecks = @(
        "Today's leadership focus",
        "What changed since yesterday?",
        "Today's decision agenda",
        "Who owns what?",
        "What was decided today?"
    )
    $htmlMissing = @()
    $reportNeedsLogin = ($report.Content -match 'login-body') -or ($report.Content -match '<title>Sign in')
    if (-not $reportNeedsLogin) {
        foreach ($marker in $htmlChecks) {
            if ($report.Content.IndexOf($marker, [System.StringComparison]::OrdinalIgnoreCase) -lt 0) {
                $htmlMissing += $marker
            }
        }
    }

    $summary = @()
    $summary += "Phase: 10.7.1"
    $summary += "Hospital: $Hospital"
    $summary += "QA HTTP: $($qa.StatusCode)"
    $summary += "English report HTTP: $($report.StatusCode)"
    $summary += "Arabic report HTTP: $($reportAr.StatusCode)"
    $summary += "QA FAIL lines: $failCount"
    $summary += "QA PASS lines: $passCount"
    $summary += "Report authentication required: $reportNeedsLogin"
    $summary += "Missing executive UX markers: $($htmlMissing.Count)"
    $summary += "Write operations: NONE"
    $summary | Set-Content -Encoding UTF8 (Join-Path $outDir "summary.txt")

    if ($htmlMissing.Count -gt 0) {
        $htmlMissing | Set-Content -Encoding UTF8 (Join-Path $outDir "missing_markers.txt")
    }

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

    Write-Host "Evidence: $zip" -ForegroundColor Green
    Write-Host "QA PASS lines: $passCount"
    Write-Host "QA FAIL lines: $failCount"
    Write-Host "Report authentication required: $reportNeedsLogin"
    Write-Host "Missing executive UX markers: $($htmlMissing.Count)"

    if (
        $failCount -gt 0 -or
        $htmlMissing.Count -gt 0 -or
        $qa.StatusCode -ne 200 -or
        $report.StatusCode -ne 200 -or
        $reportAr.StatusCode -ne 200
    ) {
        Write-Host "Result: FAIL" -ForegroundColor Red
        exit 1
    }

    Write-Host "Result: PASS" -ForegroundColor Green
    exit 0
}
catch {
    $errorText = $_ | Out-String
    $errorText | Set-Content -Encoding UTF8 (Join-Path $outDir "error.txt")
    Write-Host "Result: ERROR" -ForegroundColor Red
    Write-Host $errorText -ForegroundColor Red
    exit 2
}
