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\E25_H1072_$stamp"
New-Item -ItemType Directory -Force -Path $outDir | Out-Null

$qaUrl = "$BaseUrl/public/leadership-huddle-executive-readability-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.2 - Executive Leadership Typography and Readability 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 "executive_readability_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

    $reportNeedsLogin = ($report.Content -match 'login-body') -or ($report.Content -match '<title>Sign in')
    $reportArNeedsLogin = ($reportAr.Content -match 'login-body') -or ($reportAr.Content -match '<title>Sign in')

    $htmlMarkers = @(
        "Today's leadership focus",
        "Today's decision agenda",
        "WHY NOW",
        "DECISION REQUIRED"
    )
    $missingMarkers = @()
    if (-not $reportNeedsLogin) {
        foreach ($marker in $htmlMarkers) {
            if ($report.Content.IndexOf($marker, [System.StringComparison]::OrdinalIgnoreCase) -lt 0) {
                $missingMarkers += $marker
            }
        }
    }

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

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

    $zip = Join-Path $root "$EvidenceDir\evidence_E25_H1072_$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 "English report authentication required: $reportNeedsLogin"
    Write-Host "Arabic report authentication required: $reportArNeedsLogin"
    Write-Host "Missing executive markers: $($missingMarkers.Count)"

    if (
        $failCount -gt 0 -or
        $missingMarkers.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
}
