#!/bin/bash
#
# Check which documents are still showing wrong content
#
set -e

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

echo "=========================================="
echo "Checking Remaining Issues"
echo "=========================================="
echo ""

echo "✅ WORKING CORRECTLY:"
echo "  - PL689   ✅"
echo "  - PL6204  ✅"
echo "  - PL12321 ✅"
echo ""

echo "❌ STILL NEED TO CHECK:"
echo "  - BP102   ?"
echo "  - PL11089 ? (expected to fail - real file not found)"
echo "  - Others  ?"
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "1️⃣ Testing BP102..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# Clear cache
rm -f /tmp/aumentum_pdfs/BP102*.pdf

# Test BP102
curl -s "http://localhost:8001/documents/pdf-by-document-number?document_number=BP102&document_id=10000000014368" \
  -o /tmp/CHECK_BP102.pdf

if [ -f /tmp/CHECK_BP102.pdf ]; then
    file_type=$(file /tmp/CHECK_BP102.pdf | cut -d: -f2)
    size=$(stat -c%s /tmp/CHECK_BP102.pdf 2>/dev/null || stat -f%z /tmp/CHECK_BP102.pdf 2>/dev/null)
    
    echo "File type: $file_type"
    echo "File size: $size bytes"
    
    if file /tmp/CHECK_BP102.pdf | grep -q "PDF"; then
        echo "✅ BP102 PDF generated successfully"
        echo "   Opening for manual verification..."
        xdg-open /tmp/CHECK_BP102.pdf &
        sleep 2
        
        echo ""
        echo "📋 MANUAL CHECK: What document number does BP102 PDF show?"
        echo "   Expected: BP102"
        echo "   If shows BP102 → ✅ Working!"
        echo "   If shows PL6204 → ❌ Mapping not applied"
        echo "   If shows other → ❌ Different issue"
    else
        echo "❌ BP102 failed - not a PDF"
        cat /tmp/CHECK_BP102.pdf
    fi
else
    echo "❌ BP102 file not created"
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "2️⃣ Checking BP102 mapping status..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# Check if BP102 is in the mapping
echo "Checking CORRECT_FILE_MAPPING for BP102:"
grep -A4 "'BP102'" /home/plagis/workspace/plagis_aumentum/aumentum_browser_service.py | head -6

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "3️⃣ Testing PL11089 (expected to fail)..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

rm -f /tmp/aumentum_pdfs/PL11089*.pdf

curl -s "http://localhost:8001/documents/pdf-by-document-number?document_number=PL11089&document_id=10000000013787" \
  -o /tmp/CHECK_PL11089.pdf

if [ -f /tmp/CHECK_PL11089.pdf ]; then
    if file /tmp/CHECK_PL11089.pdf | grep -q "PDF"; then
        echo "✅ PL11089 PDF generated"
        echo "   (Will show PL689 content - real PL11089 file not found)"
        xdg-open /tmp/CHECK_PL11089.pdf &
    else
        echo "❌ PL11089 failed"
        cat /tmp/CHECK_PL11089.pdf
    fi
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "4️⃣ Check server logs for mapping application..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

echo "Looking for 'APPLYING FILE URL FIX' messages in logs..."

# Find the latest log file
log_file=$(ls -t /tmp/api*.log /tmp/server.log 2>/dev/null | head -1)

if [ -f "$log_file" ]; then
    echo "Checking: $log_file"
    echo ""
    grep -A3 "APPLYING FILE URL FIX" "$log_file" | tail -20
else
    echo "No log file found"
    echo "Server might be running in foreground - check that terminal"
fi

echo ""
echo "=========================================="
echo "Summary"
echo "=========================================="
echo ""

echo "✅ Working: PL689, PL6204, PL12321"
echo "❓ Check: BP102 - verify the PDF manually"
echo "❌ Expected fail: PL11089 (real file not found)"
echo ""

echo "📋 Report back:"
echo "   1. Does BP102 PDF show BP102 content? (Yes/No)"
echo "   2. If NO, what does it show?"
echo "   3. Are there other documents showing wrong content?"
echo ""

