#!/usr/bin/env python3
"""Test CVR filter i AI analyze"""
import pdfplumber
import json
import re
from pathlib import Path

# Extract PDF text
pdf_path = Path("/app/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)

print("=== PDF TEXT (first 1500 chars) ===")
print(full_text[:1500])
print("\n")

# Find CVR numbers (support both CVR and DK prefix)
cvr_pattern = r'(?:CVR|Momsnr\.?|DK)[:\s-]*(\d{8})'
cvr_matches = re.findall(cvr_pattern, full_text)
print(f"=== Found CVR numbers: {cvr_matches}")
print()

# Check for OWN_CVR
OWN_CVR = "44687369"
if OWN_CVR in cvr_matches:
    print(f"⚠️  WARNING: Found OWN_CVR {OWN_CVR} - should be filtered!")
else:
    print(f"✅ OWN_CVR {OWN_CVR} not in CVR list (good)")
    
# Check if we found DCS CVR
DCS_CVR = "29522790"
if DCS_CVR in cvr_matches:
    print(f"✅ Found vendor CVR {DCS_CVR} (DCS ApS) - correct!")
else:
    print(f"❌ Did NOT find vendor CVR {DCS_CVR}")

print()
print("=== Expected behavior ===")
print("AI should find CVR 29522790 (DCS ApS)")
print(f"AI should IGNORE CVR {OWN_CVR} (BMC Denmark)")
