
from app.services.fuzzy_matcher import normalize_customer_name, normalize_pn_for_matching
from app.routers.lab import parse_date
from datetime import datetime

def debug_lab_logic():
    print("--- Debugging Lab Logic Normalization ---")
    
    # Test Data from User Scenario
    sample_cust = "Semisales Co., LTD"
    sample_pn = "PSMQC098N10LS2-AU_R2_002A1"
    sample_date_str = "20250913" # From previous debug output

    order_cust = "SEMISALES"
    order_pn = "PSMQC098N10LS2-AU_R2_002A1"
    order_date_str = "2025-06-05" # From previous debug output

    # Normalization
    norm_sample_cust = normalize_customer_name(sample_cust)
    norm_order_cust = normalize_customer_name(order_cust)
    
    norm_sample_pn = normalize_pn_for_matching(sample_pn)
    norm_order_pn = normalize_pn_for_matching(order_pn)

    print(f"Sample Customer '{sample_cust}' -> '{norm_sample_cust}'")
    print(f"Order Customer  '{order_cust}'  -> '{norm_order_cust}'")
    print(f"Customer Match: {norm_sample_cust == norm_order_cust}")

    print(f"Sample PN '{sample_pn}' -> '{norm_sample_pn}'")
    print(f"Order PN  '{order_pn}'  -> '{norm_order_pn}'")
    print(f"PN Match: {norm_sample_pn == norm_order_pn}")

    # Key Check
    sample_key = (norm_sample_cust, norm_sample_pn)
    order_key = (norm_order_cust, norm_order_pn)
    print(f"Sample Key: {sample_key}")
    print(f"Order Key:  {order_key}")
    print(f"Key Match: {sample_key == order_key}")

    # Date Parsing Check
    print("\n--- Date Parsing Check ---")
    s_date = parse_date(sample_date_str)
    o_date = parse_date(order_date_str)
    print(f"Sample Date Raw: '{sample_date_str}' -> Parsed: {s_date}")
    print(f"Order Date Raw:  '{order_date_str}'  -> Parsed: {o_date}")

    if s_date and o_date:
        diff = (o_date - s_date).days
        print(f"Date Diff (Order - Sample): {diff} days")
        if diff < 0:
            print("WARNING: Order is BEFORE Sample. Velocity calculation might filter this out if checking diff >= 0.")

if __name__ == "__main__":
    debug_lab_logic()
