#!/usr/bin/env python3
"""Generate a human-readable review CSV from the BofA statement."""

import csv, sys
from datetime import datetime
from categorize import classify, parse_amount

PAYEE_CLEANUP = [
    ("WIRE TYPE:WIRE IN DATE:", "Wire IN --"),
    ("WIRE TYPE:WIRE OUT DATE:", "Wire OUT --"),
    ("WIRE TYPE:BOOK OUT DATE:", "Wire OUT (internal) --"),
    ("WIRE TYPE:INTL OUT DATE:", "Intl Wire OUT --"),
    ("WIRE TYPE:INTL IN DATE:", "Intl Wire IN --"),
]

def short_desc(desc):
    d = desc
    for old, new in PAYEE_CLEANUP:
        if old.upper() in d.upper():
            d = new + " " + d[len(old):].strip()
            break
    # Trim wire transaction noise
    import re
    d = re.sub(r'DATE:\s*\d{6}.*?(?=ORIG:|BNF:|PMT|$)', '', d, flags=re.DOTALL)
    d = re.sub(r'TRN:XXXXXXXXXX\w+', '', d)
    d = re.sub(r'SEQ:\S+', '', d)
    d = re.sub(r'ID:\S+', '', d)
    d = re.sub(r'PMT DET:\S+', '', d)
    d = re.sub(r'SERVICE REF:\S+', '', d)
    d = re.sub(r'RELATED REF:\S+', '', d)
    d = re.sub(r'\s{2,}', ' ', d).strip()
    # For card purchases strip the date/card noise
    d = re.sub(r'\d{2}/\d{2} PURCHASE.*?DEBIT CARD \*\d+', '', d)
    d = re.sub(r'\s{2,}', ' ', d).strip()
    return d[:100]

def main():
    src = '/Users/aicomputer/.openclaw/workspace/accounting/boa-2026-ytd.csv'
    out = '/Users/aicomputer/.openclaw/workspace/accounting/brixton-2026-review.csv'

    with open(src, newline='', encoding='utf-8-sig') as f:
        lines = f.read().split('\n')

    header_idx = next(i for i, l in enumerate(lines) if l.startswith('Date,'))
    reader = list(csv.reader(lines[header_idx:]))
    headers = reader[0]

    rows_out = []
    for row in reader[1:]:
        if len(row) < 3 or not row[0] or row[0] == 'Date':
            continue
        date_str, desc, amt_str = row[0], row[1], row[2]
        if not amt_str:
            continue
        try:
            amt = parse_amount(amt_str)
            dt = datetime.strptime(date_str, '%m/%d/%Y')
        except Exception:
            continue

        account, payee = classify(desc)
        flag = "REVIEW" if account == "Expenses:Uncategorized" else ""

        rows_out.append({
            'Date': dt.strftime('%Y-%m-%d'),
            'Description': short_desc(desc),
            'Amount': f"{amt:,.2f}",
            'Clawstin Category': account,
            'Payee (guessed)': payee,
            'Flag': flag,
            'Your Notes / Corrections': '',
        })

    with open(out, 'w', newline='') as f:
        w = csv.DictWriter(f, fieldnames=[
            'Date', 'Description', 'Amount',
            'Clawstin Category', 'Payee (guessed)', 'Flag',
            'Your Notes / Corrections'
        ])
        w.writeheader()
        w.writerows(rows_out)

    flagged = sum(1 for r in rows_out if r['Flag'])
    print(f"Written: {out}")
    print(f"Total rows: {len(rows_out)} | Flagged for review: {flagged}")

    # Print flagged rows to console
    if flagged:
        print("\nItems flagged REVIEW:")
        for r in rows_out:
            if r['Flag']:
                print(f"  {r['Date']}  {r['Amount']:>12}  {r['Description'][:60]}")

main()
