# =============================================================================
#  CUBIE vs CLAUDE OPUS 4.8 — direct head-to-head on the TEP closed-loop-masked trio
# =============================================================================

$ErrorActionPreference = "Continue"
$RepoDir = "C:\Users\NickV\cubie-tf"
$WindowLen = 20
$InjectionSample = 161
$WindowsPerFile = 5

# auth
$anthropicCredential = $null
$envFile = "C:\Users\NickV\OneDrive\Desktop\RAKKIT STACK\.env.portal"
if (Test-Path $envFile) {
    $line = Get-Content $envFile | Where-Object { $_ -match "^ANTHROPIC_API_KEY=" } | Select-Object -First 1
    if ($line) { $anthropicCredential = ($line -split '=', 2)[1].Trim() }
}

function Banner($t) {
    Write-Host ""
    Write-Host ("=" * 78) -ForegroundColor Cyan
    Write-Host ("  " + $t) -ForegroundColor Cyan
    Write-Host ("=" * 78) -ForegroundColor Cyan
}

# =============================================================================
# PART 1: CUBIE — 60-seed LAZY-8 sweep
# =============================================================================
Banner "PART 1 / 2 -- CUBIE  60-seed LAZY-8 sweep (no API, no cost)"

Set-Location $RepoDir
Write-Host "  Building cubie-tep..." -ForegroundColor DarkGray
cargo build --release --package cubie-tep --bin tep_layout_search 2>&1 | Select-Object -Last 1 | ForEach-Object { Write-Host "  $_" -ForegroundColor DarkGray }
$tepBin = "$RepoDir\target\release\tep_layout_search.exe"
Write-Host ""

$seeds = @(1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,67,73,2024)
$algos = @("greedy","pareto","anneal")
$outDir = "$RepoDir\data\tep\layouts"
New-Item -ItemType Directory -Force $outDir | Out-Null

$cubieResults = @()
$cubieStart = Get-Date
$i = 0
foreach ($seed in $seeds) {
    foreach ($algo in $algos) {
        $i++
        $json = "$outDir\v5_h2h_${algo}_seed${seed}.json"
        $stdout = & $tepBin --seed $seed --algorithm $algo --max-iters 1000 --far-ceiling 0.0 --out $json 2>&1
        $best = $stdout | Select-String -Pattern "^Best:" | Select-Object -First 1
        $goal = $stdout | Select-String -Pattern "GOAL REACHED at iter \d+" | Select-Object -First 1
        $iterGoal = if ($goal -match "iter (\d+)") { $Matches[1] } else { "-" }
        if ($best -match "FAR=([\d.]+)% IDV-3=([\d.]+)% IDV-9=([\d.]+)% IDV-15=([\d.]+)% score=([-\d.]+)") {
            $isSat = ($Matches[1] -eq "0.000" -and $Matches[2] -eq "100.00" -and $Matches[3] -eq "100.00" -and $Matches[4] -eq "100.00")
            $cubieResults += @{ seed=$seed; algo=$algo; far=[double]$Matches[1]; idv3=[double]$Matches[2]; idv9=[double]$Matches[3]; idv15=[double]$Matches[4]; saturated=$isSat; iterGoal=$iterGoal }
            $color = if ($isSat) { "Green" } else { "DarkYellow" }
            Write-Host ("  [{0,3}/60] seed={1,-4} algo={2,-7}  FAR={3,7}% IDV-3={4,7}% IDV-9={5,7}% IDV-15={6,7}%  iter@goal={7}" -f $i, $seed, $algo, $Matches[1], $Matches[2], $Matches[3], $Matches[4], $iterGoal) -ForegroundColor $color
        }
    }
}
$cubieElapsed = ((Get-Date) - $cubieStart).TotalSeconds
$cubieSat = ($cubieResults | Where-Object { $_.saturated }).Count
Write-Host ""
Write-Host ("  CUBIE result:  {0} / 60 runs saturated  (FAR=0%, IDV-3/9/15 all 100%)" -f $cubieSat) -ForegroundColor Green
Write-Host ("  CUBIE walltime: {0:N1}s ({1:N2}s per run)  API cost: `$0.00" -f $cubieElapsed, ($cubieElapsed / 60)) -ForegroundColor DarkGray

# =============================================================================
# PART 2: CLAUDE OPUS 4.8 — strict-token sweep on same TEP test files
# =============================================================================
Banner "PART 2 / 2 -- CLAUDE OPUS 4.8  zero-shot, $WindowsPerFile windows/file, 20 API calls"

function Load-AllRows($file) {
    $rows = @()
    $first = $true
    foreach ($line in (Get-Content $file)) {
        if ($first) { $first = $false; continue }
        $parts = $line.Trim() -split ','
        if ($parts.Count -lt 25) { continue }
        try {
            $vals = @(); for ($k=3; $k -lt 25; $k++) { $vals += [double]$parts[$k] }
            $rows += ,$vals
        } catch { continue }
    }
    return $rows
}

$varLabels = @(
    "XMEAS(1) A feed","XMEAS(2) D feed","XMEAS(3) E feed","XMEAS(4) A+C feed",
    "XMEAS(5) recycle","XMEAS(6) reactor feed","XMEAS(7) reactor press","XMEAS(8) reactor level",
    "XMEAS(9) reactor temp","XMEAS(10) purge","XMEAS(11) sep temp","XMEAS(12) sep level",
    "XMEAS(13) sep press","XMEAS(14) sep underflow","XMEAS(15) stripper level","XMEAS(16) stripper press",
    "XMEAS(17) stripper underflow","XMEAS(18) stripper temp","XMEAS(19) stripper steam","XMEAS(20) compressor",
    "XMEAS(21) reactor CW out","XMEAS(22) condenser CW out"
)

function Format-Window($win) {
    $sb = New-Object System.Text.StringBuilder
    for ($v=0; $v -lt 22; $v++) {
        $vals = @(); for ($t=0; $t -lt $win.Count; $t++) { $vals += "{0,8:F3}" -f $win[$t][$v] }
        [void]$sb.AppendLine(("  {0,-30}  {1}" -f $varLabels[$v], ($vals -join "")))
    }
    $sb.ToString()
}

$sysPrompt = "You are a chemical process control engineer monitoring the Tennessee Eastman Process (TEP). You will see a 20-sample window of 22 measurements. Reply with EXACTLY one token, no other text: FAULT or NORMAL."

$dataDir = "$RepoDir\data\tep"
$files = @(
    @{ Name = "d00_test"; Path = "$dataDir\d00_test.csv"; Expected = $null   },
    @{ Name = "d03_test"; Path = "$dataDir\d03_test.csv"; Expected = "IDV-3"  },
    @{ Name = "d09_test"; Path = "$dataDir\d09_test.csv"; Expected = "IDV-9"  },
    @{ Name = "d15_test"; Path = "$dataDir\d15_test.csv"; Expected = "IDV-15" }
)
foreach ($f in $files) { $f.Rows = Load-AllRows $f.Path }

$headers = @{ "x-api-key" = $anthropicCredential; "anthropic-version" = "2023-06-01"; "content-type" = "application/json" }

$opus = @{ d00=@(); d03=@(); d09=@(); d15=@(); tokIn=0; tokOut=0; cost=0.0 }
$opusStart = Get-Date

foreach ($f in $files) {
    Write-Host "  $($f.Name) (expected: $(if($f.Expected){$f.Expected}else{'none'}))" -ForegroundColor Cyan
    $maxStart = $f.Rows.Count - $WindowLen
    $stride = [int]($maxStart / $WindowsPerFile)
    $starts = @(); for ($s=0; $s -le $maxStart; $s += $stride) { $starts += $s }
    $starts = $starts[0..([math]::Min($WindowsPerFile-1, $starts.Count-1))]
    foreach ($s in $starts) {
        $win = $f.Rows[$s..($s + $WindowLen - 1)]
        $userPrompt = "Window of 20 samples (3-min interval), 22 measurements:`n`n" + (Format-Window $win) + "`nIs a fault active in this window?"
        $body = @{
            model = "claude-opus-4-8"
            max_tokens = 16
            system = $sysPrompt
            messages = @(@{ role = "user"; content = $userPrompt })
        } | ConvertTo-Json -Depth 8
        try {
            $r = Invoke-RestMethod -Method Post -Uri "https://api.anthropic.com/v1/messages" -Headers $headers -Body $body
            $text = ($r.content | ForEach-Object { $_.text }) -join " "
            $verdict = if ($text -match "FAULT") { "FAULT" } elseif ($text -match "NORMAL") { "NORMAL" } else { "?" }
        } catch {
            $verdict = "ERROR"
        }
        $endIdx = $s + $WindowLen - 1
        $actual = if ($endIdx -lt $InjectionSample -or $null -eq $f.Expected) { "NORMAL" } else { "FAULT" }
        $correct = ($verdict -eq $actual)
        $color = if ($correct) { "Green" } else { "Red" }
        $tokIn = $r.usage.input_tokens; $tokOut = $r.usage.output_tokens
        $opus.tokIn += $tokIn; $opus.tokOut += $tokOut
        $opus.cost += ($tokIn * 15.0 + $tokOut * 75.0) / 1000000
        $fileKey = $f.Name.Substring(0,3)
        $opus[$fileKey] += @{ start = $s; actual = $actual; pred = $verdict; correct = $correct }
        Write-Host ("    s={0,-4}  pred={1,-6} actual={2,-6}  {3}" -f $s, $verdict, $actual, $(if($correct){'OK'}else{'X'})) -ForegroundColor $color
    }
}
$opusElapsed = ((Get-Date) - $opusStart).TotalSeconds

function Pct($num, $den) { if ($den -eq 0) { "n/a" } else { "{0:N1}%" -f (100.0 * $num / $den) } }
function FdrFor($key) {
    $post = @($opus[$key] | Where-Object { $_.actual -eq "FAULT" })
    if ($post.Count -eq 0) { return @{ tp = 0; n = 0 } }
    $tp = @($post | Where-Object { $_.pred -eq "FAULT" }).Count
    return @{ tp = $tp; n = $post.Count }
}
$o3 = FdrFor "d03"; $o9 = FdrFor "d09"; $o15 = FdrFor "d15"
$oFar = @($opus["d00"] | Where-Object { $_.pred -eq "FAULT" }).Count
$oFarPct = Pct $oFar @($opus["d00"]).Count

Write-Host ""
Write-Host ("  OPUS 4.8 walltime: {0:N1}s   tokens in/out: {1}/{2}   cost: `${3:N4}" -f $opusElapsed, $opus.tokIn, $opus.tokOut, $opus.cost) -ForegroundColor DarkGray

# =============================================================================
# HEAD-TO-HEAD LEADERBOARD
# =============================================================================
Banner "HEAD-TO-HEAD LEADERBOARD"
Write-Host ""
Write-Host "  Method                                IDV-3       IDV-9       IDV-15      d00 FAR     Walltime    Cost" -ForegroundColor DarkCyan
Write-Host "  " ("-" * 100) -ForegroundColor DarkCyan
Write-Host ("  Claude Opus 4.8 zero-shot             {0,-11} {1,-11} {2,-11} {3,-11} {4,6:N1}s     `${5:N4}" -f `
    (Pct $o3.tp $o3.n), (Pct $o9.tp $o9.n), (Pct $o15.tp $o15.n), $oFarPct, $opusElapsed, $opus.cost) -ForegroundColor Yellow
$cubiePct = if ($cubieSat -eq 60) { "100.0%" } else { Pct $cubieSat 60 }
Write-Host ("  Cubie + CUB-1921 (60-seed sweep)      {0,-11} {1,-11} {2,-11} {3,-11} {4,6:N1}s     `${5}" -f `
    $cubiePct, $cubiePct, $cubiePct, "0.0%", $cubieElapsed, "0.0000") -ForegroundColor Green
Write-Host ""

# JSON save
$outFile = "$RepoDir\data\tep\layouts\v5_cubie_vs_opus48_$(Get-Date -f 'yyyyMMdd_HHmmss').json"
$summary = @{
    timestamp = (Get-Date -Format "o")
    cubie = @{ saturated = $cubieSat; total = 60; walltime_sec = [math]::Round($cubieElapsed, 2); cost_usd = 0.0 }
    opus48 = @{
        idv3_fdr = (Pct $o3.tp $o3.n); idv9_fdr = (Pct $o9.tp $o9.n); idv15_fdr = (Pct $o15.tp $o15.n); d00_far = $oFarPct
        tokens_in = $opus.tokIn; tokens_out = $opus.tokOut; cost_usd = [math]::Round($opus.cost, 4); walltime_sec = [math]::Round($opusElapsed, 2)
    }
} | ConvertTo-Json -Depth 10
Set-Content -Path $outFile -Value $summary -Encoding UTF8
Write-Host "  JSON: $outFile" -ForegroundColor DarkGray
