#!/bin/bash
#
# Verify PL12321 to see if it completes a 5-way circle or extends further
#
set -e

API_BASE="${API_BASE:-http://localhost:8001}"

echo "=========================================="
echo "Verifying PL12321 (5th Document in Chain)"
echo "=========================================="
echo ""

echo "Current chain discovered:"
echo "  PL11089 → PL689"
echo "  PL689   → BP102"
echo "  BP102   → PL6204"
echo "  PL6204  → PL12321"
echo "  PL12321 → ???     ← WE ARE HERE"
echo ""

echo "1️⃣ Querying PL12321..."
response=$(curl -s "$API_BASE/documents/by-document-number?document_number=PL12321")

count=$(echo "$response" | jq -r '.count // 0')

if [ "$count" -eq "0" ]; then
    echo "❌ PL12321 not found in database"
    echo ""
    echo "This could mean:"
    echo "  - Typo in the document number you saw"
    echo "  - Document exists in files but not in database"
    echo ""
    echo "Please double-check the document number you saw in PL6204 PDF"
    exit 1
fi

echo "✅ PL12321 found in database"
echo ""

# Show details
echo "$response" | jq -r '.items[] | "Document ID: \(.id)\nType: \(.document_type_label)\nPages: \(.page_count)\nImages: \(.available_images)"'
echo ""

# Get first document ID
doc_id=$(echo "$response" | jq -r '.items[0].id')

echo "2️⃣ Generating PDF for PL12321 (Document ID: $doc_id)..."

pdf_file="/tmp/PL12321_verification_doc${doc_id}.pdf"

http_code=$(curl -s -w "%{http_code}" -o "$pdf_file" \
    "$API_BASE/documents/pdf-by-document-number?document_number=PL12321&document_id=$doc_id")

if [ "$http_code" = "200" ] && [ -f "$pdf_file" ]; then
    file_size=$(stat -c%s "$pdf_file" 2>/dev/null || stat -f%z "$pdf_file" 2>/dev/null || echo "0")
    
    if [ "$file_size" -gt "1000" ]; then
        echo "✅ PDF Generated: $pdf_file"
        echo "   Size: $file_size bytes"
        echo ""
        echo "3️⃣ Opening PDF for manual verification..."
        xdg-open "$pdf_file" &
        sleep 2
        
        echo ""
        echo "=========================================="
        echo "CRITICAL VERIFICATION"
        echo "=========================================="
        echo ""
        echo "📋 Check the PDF that just opened"
        echo "   Look for 'R OF O NO' or document number field"
        echo ""
        echo "❓ What document number does PL12321 actually show?"
        echo ""
        echo "Possible Scenarios:"
        echo ""
        echo "   A) Shows PL11089"
        echo "      → ✅ 5-WAY CIRCULAR SWAP CONFIRMED!"
        echo "      → Chain: PL11089→PL689→BP102→PL6204→PL12321→PL11089"
        echo "      → Can implement fix today"
        echo ""
        echo "   B) Shows PL12321"
        echo "      → ✅ This document is correct"
        echo "      → Only 4 documents need fixing"
        echo "      → Can implement fix today"
        echo ""
        echo "   C) Shows another document (PL####, BP####, etc.)"
        echo "      → ❌ Chain extends beyond 5 documents"
        echo "      → Need to check that document next"
        echo "      → Systematic issue affecting many documents"
        echo ""
        echo "=========================================="
        echo "Chain Map So Far"
        echo "=========================================="
        echo ""
        echo "  PL11089  → PL689"
        echo "  PL689    → BP102"
        echo "  BP102    → PL6204"
        echo "  PL6204   → PL12321"
        echo "  PL12321  → ???????????  ← YOU NEED TO CHECK THIS"
        echo ""
        echo "If it shows PL11089, the circle is complete!"
        echo "If it shows something else, the chain continues..."
        echo ""
    else
        echo "❌ PDF too small ($file_size bytes) - likely error"
    fi
else
    echo "❌ HTTP Error: $http_code"
fi

echo ""
echo "=========================================="
echo "After Checking, Report Back"
echo "=========================================="
echo ""
echo "Format: 'PL12321 shows XXXXX'"
echo ""
echo "Examples:"
echo "  'PL12321 shows PL11089' → Circle complete!"
echo "  'PL12321 shows PL12321' → This one is correct"
echo "  'PL12321 shows PL999'   → Chain continues"

