import csv
from dateutil import parser
from datetime import datetime, timedelta

# Config values
DATE_TOLERANCE_DAYS = 7
AMOUNT_TOLERANCE_PERCENT = 0.05
CONFIDENCE_THRESHOLD = 0.8

# Receipt data
receipt_date = datetime(2025, 2, 7)
receipt_amount = 1412.5
receipt_vendor = "Ajai Srivastava CPA, Accounting Services & Taxes"

print("=== DEBUGGING AJAI RECEIPT MATCH ===")
print(f"Receipt Date: {receipt_date}")
print(f"Receipt Amount: ${receipt_amount}")
print(f"Receipt Vendor: {receipt_vendor}")
print(f"Date Tolerance: {DATE_TOLERANCE_DAYS} days")
print(f"Amount Tolerance: {AMOUNT_TOLERANCE_PERCENT * 100}%")
print()

# Check CSV transaction
csv_transaction = {
    "date": "2/18/2025",
    "amount": -1412.5,
    "vendor": "Ajai Srivastava"
}

# Parse CSV date
csv_date = parser.parse(csv_transaction["date"])
csv_amount = csv_transaction["amount"]
csv_vendor = csv_transaction["vendor"]

print("=== CSV TRANSACTION ===")
print(f"CSV Date: {csv_date}")
print(f"CSV Amount: ${csv_amount}")
print(f"CSV Vendor: {csv_vendor}")
print()

# Check date tolerance
date_diff = abs((receipt_date - csv_date).days)
date_match = date_diff <= DATE_TOLERANCE_DAYS

print("=== DATE CHECK ===")
print(f"Date Difference: {date_diff} days")
print(f"Date Match: {date_match}")
print(f"Tolerance: {DATE_TOLERANCE_DAYS} days")
print()

# Check amount tolerance
amount_tolerance = receipt_amount * AMOUNT_TOLERANCE_PERCENT
amount_diff = abs(receipt_amount - abs(csv_amount))  # Use absolute value for negative amounts
amount_match = amount_diff <= amount_tolerance

print("=== AMOUNT CHECK ===")
print(f"Receipt Amount: ${receipt_amount}")
print(f"CSV Amount (abs): ${abs(csv_amount)}")
print(f"Amount Difference: ${amount_diff}")
print(f"Amount Tolerance: ${amount_tolerance}")
print(f"Amount Match: {amount_match}")
print()

# Check vendor similarity
vendor_similarity = "Ajai Srivastava" in receipt_vendor
print("=== VENDOR CHECK ===")
print(f"Receipt Vendor: {receipt_vendor}")
print(f"CSV Vendor: {csv_vendor}")
print(f"Vendor Similarity: {vendor_similarity}")
print()

# Overall result
print("=== RESULT ===")
if date_match and amount_match:
    print("✅ Transaction would pass initial filtering")
    print("Would proceed to AI matching stage")
else:
    print("❌ Transaction filtered out before AI matching")
    if not date_match:
        print(f"  - Date difference ({date_diff} days) > tolerance ({DATE_TOLERANCE_DAYS} days)")
    if not amount_match:
        print(f"  - Amount difference (${amount_diff}) > tolerance (${amount_tolerance})") 