#!/usr/bin/env python3
"""Test AI analyze endpoint med CVR filter"""
import requests
import pdfplumber
import json
from pathlib import Path

# Extract PDF text
pdf_path = Path("uploads/5082481.pdf")
all_text = []
with pdfplumber.open(pdf_path) as pdf:
    for page in pdf.pages:
        page_text = page.extract_text(layout=True, x_tolerance=2, y_tolerance=2)
        if page_text:
            all_text.append(page_text)

full_text = "\n".join(all_text)

# Call AI analyze endpoint
print("🧪 Testing AI analyze endpoint...")
response = requests.post(
    'http://localhost:8000/api/v1/supplier-invoices/ai/analyze',
    json={'pdf_text': full_text[:2000]},  # First 2000 chars as in the actual code
    headers={'Content-Type': 'application/json'}
)

print(f"Status: {response.status_code}")

if response.status_code == 200:
    result = response.json()
    print(f"\n✅ AI Analysis Result:")
    print(f"   CVR: {result.get('cvr')}")
    print(f"   Invoice Number: {result.get('invoice_number')}")
    print(f"   Date: {result.get('invoice_date')}")
    print(f"   Total: {result.get('total_amount')}")
    print(f"   Detection Patterns: {result.get('detection_patterns')}")
    
    # Check CVR filter
    found_cvr = result.get('cvr')
    OWN_CVR = "44687369"
    if found_cvr == OWN_CVR:
        print(f"\n❌ FAIL: AI returned OWN_CVR {OWN_CVR} - filter didn't work!")
    elif found_cvr == "29522790":
        print(f"\n✅ PASS: AI found correct vendor CVR {found_cvr} (DCS ApS)")
    else:
        print(f"\n⚠️  WARNING: AI found CVR {found_cvr} - unexpected value")
else:
    print(f"❌ Error: {response.text}")
