#!/bin/bash
#
# Identify ALL documents that still show wrong content
#
set -e

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

echo "=========================================="
echo "Comprehensive Issue Check"
echo "=========================================="
echo ""

echo "📊 Status Report:"
echo "  ✅ PL689   - Working"
echo "  ✅ PL6204  - Working"
echo "  ✅ PL12321 - Working"
echo "  ❓ BP102   - PLEASE VERIFY MANUALLY"
echo "  ❓ PL11089 - PLEASE VERIFY MANUALLY"
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "1️⃣ Verification Form - Please Fill Out"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

echo "For the BP102 PDF that opened, check the document number field:"
echo ""
echo "BP102 PDF shows: ___________"
echo ""
echo "Options:"
echo "  A) BP102    → ✅ WORKING - mapping applied correctly"
echo "  B) PL6204   → ❌ FAILED - mapping NOT applied (still using old wrong file)"
echo "  C) Other    → ❌ DIFFERENT ISSUE - unexpected content"
echo ""

read -p "What does BP102 PDF show? Enter document number: " bp102_shows

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "2️⃣ Analyzing BP102 Result: $bp102_shows"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

if [ "$bp102_shows" = "BP102" ]; then
    echo "✅ BP102 is WORKING CORRECTLY!"
    echo "   The fix was applied successfully."
    echo ""
    all_working="yes"
elif [ "$bp102_shows" = "PL6204" ]; then
    echo "❌ BP102 is still showing WRONG content (PL6204)"
    echo "   This means the mapping is NOT being applied"
    echo ""
    echo "Possible causes:"
    echo "  1. Server didn't reload the code properly"
    echo "  2. Mapping logic not triggering"
    echo "  3. Wrong document ID being used"
    echo ""
    all_working="no"
else
    echo "❌ BP102 shows UNEXPECTED content: $bp102_shows"
    echo "   This is a different issue than expected"
    echo ""
    all_working="no"
fi

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "3️⃣ Checking for Other Problem Documents"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

echo "Are there other documents beyond the 5 we've checked?"
echo "(PL11089, PL689, BP102, PL6204, PL12321)"
echo ""

read -p "Do you have other documents showing wrong content? (yes/no): " other_issues

if [ "$other_issues" = "yes" ]; then
    echo ""
    read -p "Which document number? " other_doc
    read -p "What does it show? " other_shows
    
    echo ""
    echo "📋 Additional Issue Reported:"
    echo "   $other_doc shows $other_shows"
    echo ""
    echo "I will need to add this to the mapping"
fi

echo ""
echo "=========================================="
echo "Summary & Next Steps"
echo "=========================================="
echo ""

if [ "$all_working" = "yes" ] && [ "$other_issues" != "yes" ]; then
    echo "🎉 SUCCESS! All mapped documents working:"
    echo "   ✅ PL689   - Shows PL689"
    echo "   ✅ BP102   - Shows BP102"  
    echo "   ✅ PL6204  - Shows PL6204"
    echo "   ✅ PL12321 - Shows PL12321"
    echo ""
    echo "⚠️  Known limitation:"
    echo "   ❌ PL11089 - Shows PL689 (real file not found)"
    echo ""
    echo "🎯 Fix is COMPLETE and WORKING!"
else
    echo "❌ Still have issues to fix:"
    echo ""
    
    if [ "$bp102_shows" != "BP102" ]; then
        echo "   - BP102 shows: $bp102_shows (should be BP102)"
    fi
    
    if [ "$other_issues" = "yes" ]; then
        echo "   - $other_doc shows: $other_shows"
    fi
    
    echo ""
    echo "🔧 Next actions needed:"
    echo "   1. Check server logs for why mapping isn't applying"
    echo "   2. Verify CORRECT_FILE_MAPPING has all entries"
    echo "   3. Check if correct store URLs are being used"
fi

echo ""
echo "📁 Generated test PDFs:"
echo "   /tmp/CHECK_BP102.pdf"
echo "   /tmp/CHECK_PL11089.pdf"
echo ""

