import csv
from pathlib import Path
from collections import defaultdict

base = Path(r"C:\Users\Administrator\OneDrive\fx_macro_intraday\data\processed")

files_to_check = [
    "usdjpy_trades_real_costs.csv",      # UJ backtest (confirmed)
    "monthly_pnl_final.csv",             # candidate EU backtest
    "monthly_pnl_validated.csv",
    "monthly_pnl_v2.csv",
]

for fname in files_to_check:
    p = base / fname
    print("=" * 72)
    print(f"FILE: {fname}")
    print("=" * 72)
    if not p.exists():
        print("  (not found at this path)")
        # try one level up
        alt = base.parent / "processed" / "trades" / fname
        print(f"  trying: {alt} -> exists={alt.exists()}")
        continue
    try:
        with open(p, newline="", encoding="utf-8") as f:
            reader = csv.reader(f)
            rows = list(reader)
        header = rows[0]
        print(f"  Rows (incl header): {len(rows)}")
        print(f"  Columns: {header}")

        # find date and pnl columns
        # detect a date column and a pnl column
        def find_col(cands):
            for i, h in enumerate(header):
                if h.strip().lower() in cands:
                    return i
            return None

        # figure out year/month columns if present
        yi = find_col({"year"})
        mi = find_col({"month"})
        # pnl preference: pnl_real > pnl > dollar_pnl_real > pnl_bt
        pnl_i = None
        for pref in ["pnl_real", "pnl", "dollar_pnl_real", "pnl_bt"]:
            pnl_i = find_col({pref})
            if pnl_i is not None:
                pnl_col_name = pref
                break

        # bucket by year-month if we have year+month, else skip
        if yi is not None and mi is not None and pnl_i is not None:
            monthly = defaultdict(float)
            count = defaultdict(int)
            for r in rows[1:]:
                if len(r) <= max(yi, mi, pnl_i):
                    continue
                try:
                    y = int(r[yi]); m = int(r[mi]); v = float(r[pnl_i])
                except ValueError:
                    continue
                monthly[(y, m)] += v
                count[(y, m)] += 1
            # show only 2025-2026 to keep it short, plus totals
            print(f"  Using pnl column: '{pnl_col_name}'")
            print(f"  Total trades: {sum(count.values())}")
            print("  Recent months (2025-2026):")
            for (y, m) in sorted(monthly.keys()):
                if y >= 2025:
                    print(f"    {y}-{m:02d}:  {monthly[(y,m)]:>10.2f}  ({count[(y,m)]} trades)")
            # full-year totals
            yearly = defaultdict(float)
            for (y, m), v in monthly.items():
                yearly[y] += v
            print("  Yearly totals:")
            for y in sorted(yearly.keys()):
                print(f"    {y}: {yearly[y]:>12.2f}")
        else:
            print(f"  (could not find year/month/pnl columns; year_i={yi} month_i={mi} pnl_i={pnl_i})")
            print(f"  First data row: {rows[1] if len(rows)>1 else 'none'}")
    except Exception as e:
        print(f"  Error: {e}")
    print()
