#!/usr/bin/env python3
"""
Test script for the improved PDF field extraction
Run this to see what fields are being found and mapped from your PDFs
"""

import sys
import logging
from pathlib import Path

# Configure logging to see debug messages
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Import the extractor class
from file_watcher_service import PDFDataExtractor

def test_pdf_extraction(pdf_path: str):
    """Test PDF extraction on a specific file"""
    print(f"\n=== TESTING PDF EXTRACTION ===")
    print(f"File: {pdf_path}")
    
    if not Path(pdf_path).exists():
        print(f"ERROR: File not found: {pdf_path}")
        return
    
    extractor = PDFDataExtractor()
    
    print(f"\nExtracting data...")
    data = extractor.extract_from_pdf(pdf_path)
    
    print(f"\n=== EXTRACTION RESULTS ===")
    print(f"Total extracted fields: {len(data)}")
    
    if data:
        print(f"\nExtracted field data:")
        for field_name, field_value in data.items():
            print(f"  {field_name}: '{field_value}'")
    else:
        print("No data extracted!")
    
    print(f"\n=== ANALYSIS ===")
    
    # Check for key missing fields you mentioned
    missing_fields = []
    expected_fields = {
        'address': 'Address information',
        'rent_mortgage': 'Rent/mortgage payment',
        'first_name': 'First name',
        'last_name': 'Last name',
        'time_on_job': 'Time on job',
        'employer_address': 'Employer address',
        'employer_city': 'Employer city',
        'vehicle_year': 'Vehicle year',
        'vehicle_make': 'Vehicle make',
        'vehicle_model': 'Vehicle model'
    }
    
    for field, description in expected_fields.items():
        if field not in data or not data[field]:
            missing_fields.append(f"{description} ({field})")
    
    if missing_fields:
        print(f"Still missing fields:")
        for missing in missing_fields:
            print(f"  - {missing}")
    else:
        print(f"All key fields found!")

def main():
    if len(sys.argv) != 2:
        print("Usage: python test_improved_extraction.py <pdf_file_path>")
        print()
        print("Example:")
        print('python test_improved_extraction.py "D:\\Customer Docs\\2025\\08 - August\\Bradley Hauck\\Bradley Hauck Credit App.pdf"')
        return
    
    pdf_path = sys.argv[1]
    test_pdf_extraction(pdf_path)

if __name__ == "__main__":
    main()