$ErrorActionPreference = "Stop"

$EnvName = "ndc_1c_mvp"
$EnvPython = Join-Path $env:USERPROFILE "miniconda3\envs\$EnvName\python.exe"
if (-not (Test-Path $EnvPython)) {
  $EnvPython = Join-Path $env:USERPROFILE "Miniconda3\envs\$EnvName\python.exe"
}
if (-not (Test-Path $EnvPython)) {
  throw "Python for env '$EnvName' not found. Expected: $EnvPython"
}

& $EnvPython -m odata_probe.fetch_metadata
& $EnvPython -m odata_probe.list_entity_sets
& $EnvPython -m odata_probe.probe_entities
& $EnvPython -m odata_probe.dump_sample_links
& $EnvPython scripts/deep_probe_subconto_join.py
& $EnvPython scripts/deep_probe_subconto.py
& $EnvPython scripts/recon_slot3_gap.py
& $EnvPython scripts/deep_probe_accounting_mvp_gate.py
& $EnvPython scripts/check_deeper_access_readiness.py

$ProjectRoot = Resolve-Path (Join-Path $PSScriptRoot "..")
$GateReport = Join-Path $ProjectRoot "logs\deep_accounting_mvp_gate.json"
if (-not (Test-Path $GateReport)) {
  throw "Gate report not found: $GateReport"
}
$Gate = Get-Content -Raw $GateReport | ConvertFrom-Json
$Slot3Report = Join-Path $ProjectRoot "logs\slot3_recon_report.json"

if (Test-Path $Slot3Report) {
  $Slot3 = Get-Content -Raw $Slot3Report | ConvertFrom-Json
  $slot3Summary = "slot3_non_null_rows=$($Slot3.totals.rows_with_non_null_slot3_total), slot3_joined_rows=$($Slot3.totals.rows_with_joined_slot3_total)"
} else {
  $slot3Summary = "slot3_report=missing"
}

if ($Gate.final_verdict -ne "OData sufficient for MVP accounting ontology") {
  throw "MVP gate failed: $($Gate.final_verdict); $slot3Summary"
}
