#!/bin/bash
#
# Final test of ALL documents with the fix
#
set -e

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

echo "=========================================="
echo "FINAL TEST: All Documents with Fix"
echo "=========================================="
echo ""

echo "📊 Expected Results:"
echo "  ✅ PL689   → 1 page of PL689 content"
echo "  ✅ BP102   → 1 page of BP102 content (not 195, but CORRECT content)"
echo "  ✅ PL6204  → 1 page of PL6204 content"
echo "  ✅ PL12321 → 1 page of PL12321 content"
echo "  ❌ PL11089 → 1 page of PL689 content (real file not found)"
echo ""

echo "⚠️  Note: Mapped documents show only 1 page to avoid wrong file mixing"
echo "   This is a trade-off: Correct content but incomplete pages"
echo ""

echo "1️⃣ Restarting server..."
cd /home/plagis/workspace/plagis_aumentum
pkill -9 -f "python.*aumentum_api" 2>/dev/null || true
sleep 2

source venv/bin/activate
python3 aumentum_api.py > /tmp/api_final_test.log 2>&1 &
sleep 8

if ! curl -s "http://localhost:8001/health" > /dev/null 2>&1; then
    echo "❌ Server failed to start"
    exit 1
fi

echo "✅ Server started"
echo ""

echo "2️⃣ Clearing cache..."
rm -rf /tmp/aumentum_pdfs/*
echo "✅ Cache cleared"
echo ""

echo "3️⃣ Testing all documents..."
echo ""

# Test each document
test_document() {
    local doc=$1
    local doc_id=$2
    local expected_content=$3
    
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "Testing: $doc"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    
    pdf="/tmp/FINAL_${doc}.pdf"
    
    http_code=$(curl -s -w "%{http_code}" -o "$pdf" \
        "$API_BASE/documents/pdf-by-document-number?document_number=$doc&document_id=$doc_id" 2>/dev/null)
    
    if [ "$http_code" = "200" ] && [ -f "$pdf" ]; then
        if file "$pdf" | grep -q "PDF"; then
            size=$(stat -c%s "$pdf" 2>/dev/null || stat -f%z "$pdf")
            pages=$(pdfinfo "$pdf" 2>/dev/null | grep "Pages:" | awk '{print $2}' || echo "?")
            
            echo "✅ PDF Generated"
            echo "   File: $pdf"
            echo "   Size: $size bytes"
            echo "   Pages: $pages"
            echo "   Expected content: $expected_content"
            echo ""
            echo "   📄 VERIFY: xdg-open $pdf"
            return 0
        else
            echo "❌ Not a PDF"
            return 1
        fi
    else
        echo "❌ HTTP Error: $http_code"
        return 1
    fi
}

test_document "PL689" "10000000012415" "PL689"
echo ""

test_document "BP102" "10000000014368" "BP102"
echo ""

test_document "PL6204" "10000000018017" "PL6204"
echo ""

test_document "PL12321" "10000000020989" "PL12321"
echo ""

test_document "PL11089" "10000000013787" "PL689 (unfixed)"
echo ""

echo "=========================================="
echo "Generated PDFs - Please Verify"
echo "=========================================="
echo ""

ls -lh /tmp/FINAL_*.pdf 2>/dev/null | awk '{print $9, "-", $5}'

echo ""
echo "🔍 Open all PDFs to verify content:"
echo ""
echo "for f in /tmp/FINAL_*.pdf; do xdg-open \$f & sleep 1; done"
echo ""

echo "=========================================="
echo "Expected Results"
echo "=========================================="
echo ""

echo "| Document | PDF Shows | Pages | Status |"
echo "|----------|-----------|-------|--------|"
echo "| PL689    | PL689     | 1-2   | ✅ Fixed |"
echo "| BP102    | BP102     | 1     | ✅ Fixed (limited pages) |"
echo "| PL6204   | PL6204    | 1     | ✅ Fixed (limited pages) |"
echo "| PL12321  | PL12321   | 1     | ✅ Fixed (limited pages) |"
echo "| PL11089  | PL689     | 1-2   | ❌ Unfixed (file not found) |"
echo ""

echo "⚠️  Known Limitation:"
echo "   Mapped documents show only first page to prevent wrong file mixing"
echo "   To get all pages, need transaction-based page discovery (future enhancement)"
echo ""

echo "Check server logs:"
echo "  tail -100 /tmp/api_final_test.log | grep -E '(APPLYING|Skipping filesystem)'"
echo ""

