#!/bin/bash
#
# Quick verification of PL6204 to confirm circular pattern
#
set -e

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

echo "=========================================="
echo "Verifying PL6204 (Completing the Circle?)"
echo "=========================================="
echo ""

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

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

if [ "$count" -eq "0" ]; then
    echo "❌ PL6204 not found in database"
    echo ""
    echo "This means the circular pattern might be different."
    echo "Please check if there's a typo in the document number you saw."
    exit 1
fi

echo "✅ PL6204 found in database"
echo ""

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

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

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

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

http_code=$(curl -s -w "%{http_code}" -o "$pdf_file" \
    "$API_BASE/documents/pdf-by-document-number?document_number=PL6204&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 "MANUAL VERIFICATION REQUIRED"
        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 PL6204 actually show?"
        echo ""
        echo "   A) PL11089  → ✅ CIRCLE CONFIRMED! 4-way circular swap"
        echo "   B) PL689    → ❌ Different pattern"
        echo "   C) BP102    → ❌ Different pattern"  
        echo "   D) PL6204   → ✅ This one is correct!"
        echo "   E) Other    → ❌ More complex issue"
        echo ""
        echo "=========================================="
        echo "WHAT TO DO NEXT"
        echo "=========================================="
        echo ""
        echo "If PL6204 shows PL11089:"
        echo "  → Perfect! 4-way circular swap confirmed"
        echo "  → I can implement the fix immediately"
        echo "  → Tell me: 'PL6204 shows PL11089'"
        echo ""
        echo "If PL6204 shows PL6204:"
        echo "  → Good! This document is correct"
        echo "  → Only 3 documents need fixing"
        echo "  → Tell me: 'PL6204 shows PL6204 (correct)'"
        echo ""
        echo "If PL6204 shows something else:"
        echo "  → More investigation needed"
        echo "  → Tell me what you see"
        echo ""
    else
        echo "❌ PDF too small ($file_size bytes) - likely error"
    fi
else
    echo "❌ HTTP Error: $http_code"
fi

