"""Why is POL09892 returning weeks=0?"""
import pandas as pd

sku = "POL09892"

# Sales (PromoTool uses sales_clean from main project usually)
try:
    sales = pd.read_csv("PromoTool/data/sales_clean.csv")
except Exception:
    sales = pd.read_csv("data/sales_clean.csv")
sub = sales[sales["sku"] == sku].sort_values(["year", "week"], ascending=False).head(52)
print(f"POL09892 sales rows in last 52w: {len(sub)}")
print()
cols = ["year", "week", "qty_retail", "qty_webshop", "is_any_promo",
        "is_retail_promo", "retail_discount_pct", "webshop_discount_pct"]
cols = [c for c in cols if c in sub.columns]
print(sub[cols].head(30).to_string())
print()

# ERP promo calendar
try:
    erp = pd.read_csv("PromoTool/data/erp_promo_calendar.csv")
except Exception:
    erp = pd.read_csv("data/erp_promo_calendar.csv")
erp_sku = erp[erp["sku"] == sku]
print(f"ERP promo rows for {sku}: {len(erp_sku)}")
print(erp_sku.head(30).to_string())
print()

# Run the actual exclusion logic
# Manually reimplement to see counts
sub2 = sales[sales["sku"] == sku].sort_values(["year", "week"], ascending=False).head(13)
promo_yws = set(zip(erp_sku["year"].astype(int), erp_sku["week"].astype(int)))

excluded = {"promo": 0, "zero": 0}
candidates = []
for _, r in sub2.iterrows():
    y, w = int(r["year"]), int(r["week"])
    if (y, w) in promo_yws:
        excluded["promo"] += 1
        continue
    rq = float(r.get("qty_retail", 0)) + float(r.get("qty_webshop", 0))
    if rq <= 0:
        excluded["zero"] += 1
        continue
    candidates.append((y, w, rq))
    if len(candidates) >= 4:
        break

print("=== NEW LOGIC (only ERP promo filter) ===")
print(f"  Lookback: 13 weeks")
print(f"  Excluded: {excluded}")
print(f"  Candidates (most recent 4 non-promo): {len(candidates)}")
for c in candidates:
    print(f"    CW{c[1]} ({c[0]}): qty {c[2]:.1f}")
if candidates:
    avg = sum(q for _, _, q in candidates) / len(candidates)
    print(f"  Baseline = avg of {len(candidates)} weeks = {avg:.1f} / wk")
