"""Does every league_data CSV contain the division its FILENAME claims?

Each football-data.co.uk row carries its own `Div` code, so the file is
self-identifying and a mismatch is unambiguous -- no name matching or guesswork.

WHY THIS MATTERS. These CSVs are the final fallback for bet resolution
(daily_signals.resolve_pending_bets -> live_data, then API-Football, then these).
A file labelled E0 that actually holds EC rows does not error; it just fails to
find the fixture, or worse matches a same-named club in the wrong division.

Usage:  python research\\audit_league_files.py
"""
import csv
import io
from collections import Counter
from pathlib import Path

LEAGUE_DIR = Path(r'E:\soccer\league_data')


def internal_divs(p):
    # utf-8-SIG, not utf-8. football-data.co.uk ships some of these files with a
    # BOM, which makes the first header cell parse as '﻿Div' -- so every
    # r.get('Div') returns None and the file reads as EMPTY rather than as
    # mismatched. First pass at this audit reported 60 "empty" files of 150KB+
    # and zero mismatches, which is how the bug announced itself.
    raw = p.read_bytes().replace(b'\x00', b'')
    rows = csv.DictReader(io.StringIO(raw.decode('utf-8-sig', errors='replace')))
    c = Counter()
    for r in rows:
        d = (r.get('Div') or '').strip()
        if d:
            c[d] += 1
    return c


def main():
    bad, ok, empty = [], 0, []
    for p in sorted(LEAGUE_DIR.glob('*.csv')):
        stem = p.stem
        if '_' not in stem:
            continue
        want, season = stem.rsplit('_', 1)
        c = internal_divs(p)
        if not c:
            empty.append((stem, p.stat().st_size))
            continue
        got, n = c.most_common(1)[0]
        if got != want:
            bad.append((season, want, got, n, sum(c.values())))
        else:
            ok += 1

    print(f'files checked : {ok + len(bad) + len(empty)}')
    print(f'  correct     : {ok}')
    print(f'  MISFILED    : {len(bad)}')
    print(f'  empty       : {len(empty)}')
    print()
    if bad:
        print(f'{"season":8} {"filename says":14} {"file contains":14} {"rows":>6}')
        print('-' * 50)
        for season, want, got, n, tot in sorted(bad):
            print(f'{season:8} {want:14} {got:14} {tot:>6}')
        print()
        seasons = sorted({b[0] for b in bad})
        print('affected seasons:', ' '.join(seasons))
    if empty:
        print()
        print('empty files:', ', '.join(f'{s} ({b}b)' for s, b in empty))


if __name__ == '__main__':
    main()
