# =============================================================================
# TestRun Pipeline
# =============================================================================
# Runs all PDFs in TestRun/input through the Project 44 pipeline:
#
#   input/narratives/*.pdf  -> Step 1 (OCR) -> Step 2 (Viewer MD)
#   input/appendices/*.pdf  -> Step 1 (OCR) -> Step 3 (Extraction)
#
# Outputs land in TestRun/outputs/ with step-prefixed filenames:
#   step2_{name}_claude-4-6_viewer.md
#   step4_{name}_claude-4-6_narratives.json
#   step4_{name}_claude-4-6_positions.json
#   step4_{name}_claude-4-6_appx_records.json
#   step4_{name}_claude-4-6_appx_positions.json
#
# Usage:
#   cd G:\IdeaProjects\AI-Prototype
#   .\TestRun\run_testrun_pipeline.ps1
#
# Requirements: ANTHROPIC_API_KEY in .env, Poppler on PATH, Python env active
# =============================================================================

$ErrorActionPreference = "Stop"

$PROJECT   = "G:\IdeaProjects\AI-Prototype"
$STEP1_TMP = "$PROJECT\TestRun\step1_temp"
$INPUT_NAR = "$PROJECT\TestRun\input\narratives"
$INPUT_APX = "$PROJECT\TestRun\input\appendices"
$OUTPUT    = "$PROJECT\TestRun\outputs"

New-Item -ItemType Directory -Force -Path $STEP1_TMP | Out-Null
New-Item -ItemType Directory -Force -Path $OUTPUT    | Out-Null

# =============================================================================
# STEP 1 - OCR all PDFs (narratives + appendices)
# =============================================================================

Write-Host ""
Write-Host "============================================================" -ForegroundColor Cyan
Write-Host " STEP 1 - OCR all input PDFs via Claude Sonnet 4.6"         -ForegroundColor Cyan
Write-Host "============================================================" -ForegroundColor Cyan

$allPdfs = @(
    (Get-ChildItem "$INPUT_NAR\*.pdf" -ErrorAction SilentlyContinue)
    (Get-ChildItem "$INPUT_APX\*.pdf" -ErrorAction SilentlyContinue)
)

if ($allPdfs.Count -eq 0) {
    Write-Host "  No PDFs found in input/narratives or input/appendices. Exiting." -ForegroundColor Yellow
    exit 1
}

foreach ($pdf in $allPdfs) {
    $expectedOut = "$STEP1_TMP\step1_$($pdf.BaseName)_claude-4-6.md"
    if (Test-Path $expectedOut) {
        Write-Host "  [SKIP] Step 1 already done: $($pdf.Name)" -ForegroundColor DarkGray
        continue
    }
    Write-Host "  OCR: $($pdf.Name)" -ForegroundColor White
    python "$PROJECT\scripts\step1_ocr_wardiaries_claude-4-6.py" `
        --single "$($pdf.FullName)" `
        --output_dir "$STEP1_TMP"
    if ($LASTEXITCODE -ne 0) {
        Write-Host "  ERROR: Step 1 failed for $($pdf.Name)" -ForegroundColor Red
    }
}

# =============================================================================
# STEP 2 - Viewer MD for narrative PDFs
# =============================================================================

Write-Host ""
Write-Host "============================================================" -ForegroundColor Cyan
Write-Host " STEP 2 - Viewer MD for narrative pages"                     -ForegroundColor Cyan
Write-Host "============================================================" -ForegroundColor Cyan

foreach ($pdf in (Get-ChildItem "$INPUT_NAR\*.pdf" -ErrorAction SilentlyContinue)) {
    $step1Md   = "$STEP1_TMP\step1_$($pdf.BaseName)_claude-4-6.md"
    $finalName = "step2_$($pdf.BaseName)_claude-4-6_viewer.md"
    $finalPath = "$OUTPUT\$finalName"

    if (-not (Test-Path $step1Md)) {
        Write-Host "  [SKIP] Step 1 output not found for $($pdf.Name)" -ForegroundColor Yellow
        continue
    }

    if (Test-Path $finalPath) {
        Write-Host "  [SKIP] Viewer MD already exists: $finalName" -ForegroundColor DarkGray
        continue
    }

    Write-Host "  Viewer MD: $($pdf.Name)" -ForegroundColor White
    python "$PROJECT\scripts\step2_json_to_viewer_md.py" `
        --input "$step1Md" `
        --output_dir "$OUTPUT"

    if ($LASTEXITCODE -ne 0) {
        Write-Host "  ERROR: Step 2 failed for $($pdf.Name)" -ForegroundColor Red
        continue
    }

    $rawOut = "$OUTPUT\step1_$($pdf.BaseName)_claude-4-6_viewer.md"
    if (Test-Path $rawOut) {
        Rename-Item -Path $rawOut -NewName $finalName
        Write-Host "  -> Saved: $finalName" -ForegroundColor Green
    }
}

# =============================================================================
# STEP 3 -> STEP 4 - Extraction for appendix PDFs
# =============================================================================

Write-Host ""
Write-Host "============================================================" -ForegroundColor Cyan
Write-Host " STEP 3 - Extraction for appendices (labelled step4)"        -ForegroundColor Cyan
Write-Host "============================================================" -ForegroundColor Cyan

$STEP3_TMP = "$PROJECT\TestRun\step3_temp"
New-Item -ItemType Directory -Force -Path $STEP3_TMP | Out-Null

foreach ($pdf in (Get-ChildItem "$INPUT_APX\*.pdf" -ErrorAction SilentlyContinue)) {
    $step1Md = "$STEP1_TMP\step1_$($pdf.BaseName)_claude-4-6.md"

    if (-not (Test-Path $step1Md)) {
        Write-Host "  [SKIP] Step 1 output not found for $($pdf.Name)" -ForegroundColor Yellow
        continue
    }

    $cleanStem        = $pdf.BaseName
    $expectedNarr     = "step4_${cleanStem}_claude-4-6_narratives.json"
    $expectedPosn     = "step4_${cleanStem}_claude-4-6_positions.json"
    $expectedAppxRec  = "step4_${cleanStem}_claude-4-6_appx_records.json"
    $expectedAppxPosn = "step4_${cleanStem}_claude-4-6_appx_positions.json"

    $allDone = (Test-Path "$OUTPUT\$expectedNarr") -and `
               (Test-Path "$OUTPUT\$expectedPosn") -and `
               (Test-Path "$OUTPUT\$expectedAppxRec") -and `
               (Test-Path "$OUTPUT\$expectedAppxPosn")

    if ($allDone) {
        Write-Host "  [SKIP] Extraction already complete: $($pdf.Name)" -ForegroundColor DarkGray
        continue
    }

    $ckptFile = "$STEP3_TMP\${cleanStem}_claude-4-6_checkpoint.json"
    if (Test-Path $ckptFile) {
        Write-Host "  [RESUME] Checkpoint found for $($pdf.Name)" -ForegroundColor Yellow
    }

    Write-Host "  Extracting: $($pdf.Name)" -ForegroundColor White
    python "$PROJECT\scripts\step3_extract-posn_claude-4-6.py" `
        --input "$step1Md" `
        --output_dir "$STEP3_TMP"

    if ($LASTEXITCODE -ne 0) {
        Write-Host "  ERROR: Step 3 failed for $($pdf.Name)" -ForegroundColor Red
        continue
    }

    foreach ($json in (Get-ChildItem "$STEP3_TMP\*.json" -ErrorAction SilentlyContinue)) {
        $dest = "$OUTPUT\step4_$($json.Name)"
        Move-Item -Path $json.FullName -Destination $dest -Force
        Write-Host "  -> Saved: step4_$($json.Name)" -ForegroundColor Green
    }
}

Remove-Item $STEP3_TMP -ErrorAction SilentlyContinue

# =============================================================================
# Summary
# =============================================================================

Write-Host ""
Write-Host "============================================================" -ForegroundColor Cyan
Write-Host " Pipeline complete. Outputs:"                                 -ForegroundColor Cyan
Write-Host "============================================================" -ForegroundColor Cyan

Get-ChildItem "$OUTPUT" | ForEach-Object {
    Write-Host "  $($_.Name)"
}

Write-Host ""
