#!/bin/bash
#
# Get the actual node associations from Alfresco tables
# This will show us which nodes are associated with which document numbers
#
set -e

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

echo "=========================================="
echo "Getting Alfresco Node Associations"
echo "=========================================="
echo ""

echo "Using API debug endpoint to compare documents..."
echo ""

# Compare the problem documents
for doc in "PL11089" "PL689"; do
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "Document: $doc"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    
    response=$(curl -s "$API_BASE/debug/compare-document-numbers?doc1=${doc}&doc2=NONE" 2>/dev/null)
    
    echo "Alfresco Properties:"
    echo "$response" | jq -r '.alfresco_properties[] | "  Node: \(.node_id), Value: \"\(.string_value)\", Property: \(.property_name)"'
    
    echo ""
done

echo "=========================================="
echo "Creating URL Mapping"
echo "=========================================="
echo ""

# For each document, get what URL it's associated with
for doc in "PL11089" "PL689" "BP102" "PL6204" "PL12321"; do
    doc_id=$(curl -s "$API_BASE/documents/by-document-number?document_number=$doc" | jq -r '.items[0].id // empty')
    
    if [ ! -z "$doc_id" ]; then
        echo "$doc (First Doc ID: $doc_id)"
    fi
done

echo ""
echo "=========================================="
echo "CRITICAL INFO NEEDED"
echo "=========================================="
echo ""

echo "From earlier diagnostic output, we know:"
echo ""
echo "  PL11089 is associated with: store://2015/3/26/15/8/3eee6f3f-0b98-41b9-a6cb-2c4488152fed.bin"
echo "  PL689 is associated with:   store://2015/3/17/10/10/879dcd53-f552-4e82-858f-7e868e60a275.bin"
echo ""
echo "But from PDFs you verified:"
echo ""
echo "  3eee6f3f...fed.bin contains PL689 content"
echo "  879dcd53...275.bin contains BP102 content"
echo ""
echo "So the DATABASE is wrong - nodes have wrong string_value labels."
echo ""
echo "=========================================="
echo "SOLUTION APPROACH"
echo "=========================================="
echo ""
echo "Since the database has wrong associations, we have 2 options:"
echo ""
echo "Option A: Python Workaround (Safe, No DB Changes)"
echo "  - Map each document to the correct file URL"
echo "  - Implement in aumentum_browser_service.py"
echo "  - Can deploy TODAY"
echo ""
echo "Option B: Database Fix (Permanent, Requires Admin)"
echo "  - UPDATE alf_node_properties.string_value"
echo "  - Backup database first"
echo "  - Requires careful testing"
echo "  - Timeline: 1-2 days with testing"
echo ""

read -p "Which option do you prefer? (A/B): " choice

if [ "$choice" = "A" ] || [ "$choice" = "a" ]; then
    echo ""
    echo "✅ Proceeding with Python Workaround"
    echo ""
    echo "I will create a comprehensive ASSOCIATION_FIXES mapping"
    echo "that redirects each document to fetch the correct file's content."
    echo ""
    echo "This requires knowing:"
    echo "  1. Which file URL actually contains PL11089 content"
    echo "  2. Which file URL actually contains PL689 content"  
    echo "  3. Which file URL actually contains BP102 content"
    echo "  4. Which file URL actually contains PL6204 content"
    echo "  5. Which file URL actually contains PL12321 content"
    echo ""
    echo "From your PDF verification, please confirm:"
    echo "  - PL12321 shows which document number? (This completes the chain)"
else
    echo ""
    echo "✅ Proceeding with Database Fix"
    echo ""
    echo "This will require SQL UPDATE statements."
    echo "We'll create a backup first, then update alf_node_properties."
    echo ""
fi

