
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from app.models import get_db, SampleRecord, OrderRecord
from app.routers.lab import get_conversions, build_order_lookups, parse_date
from app.services.fuzzy_matcher import normalize_pn_for_matching, normalize_customer_name

db = next(get_db())

print("--- DEBUG START ---")
# 1. Check Date Parsing
print(f"Date '45292' parses to: {parse_date('45292')}")

# 2. Check Raw Data Count
s_count = db.query(SampleRecord).count()
o_count = db.query(OrderRecord).count()
print(f"Total Samples: {s_count}, Total Orders: {o_count}")

# 3. Check Top Data
print("\n--- Top 3 Samples ---")
samples = db.query(SampleRecord).limit(3).all()
for s in samples:
    print(f"S: {s.customer} (ID:{s.cust_id}) | PN: {s.pn} | Date: {s.date}")

print("\n--- Top 3 Orders ---")
orders = db.query(OrderRecord).limit(3).all()
for o in orders:
    print(f"O: {o.customer} (ID:{o.cust_id}) | PN: {o.pn} | Date: {o.date}")

# 4. Check Lookups
lookup_id, lookup_name = build_order_lookups(db.query(OrderRecord).all())
print(f"\nLookup ID Size: {len(lookup_id)}")
print(f"Lookup Name Size: {len(lookup_name)}")
if len(lookup_name) > 0:
    first_key = list(lookup_name.keys())[0]
    print(f"Example Name Key: {first_key} -> {lookup_name[first_key]}")

# 5. Check Conversions
print("\n--- Run Logic ---")
conversions = get_conversions(db)
print(f"Total Conversions Found: {len(conversions)}")
for c in conversions[:3]:
    print(c)

print("--- DEBUG END ---")
