"""
Refresh league_data CSVs from football-data.co.uk.
Downloads current + prior season for all 22 leagues.
Scheduled weekly via Task Scheduler.
"""
import csv
import io
import urllib.request
import os
from collections import Counter
from datetime import datetime

OUT_DIR = r'E:\soccer\league_data'


def file_div(raw):
    """Majority `Div` code inside a football-data CSV, or None.

    utf-8-SIG: some of these ship with a BOM, which turns the first header cell
    into '\ufeffDiv' and makes every Div lookup return None.
    """
    try:
        text = raw.decode('utf-8-sig', errors='replace')
    except Exception:
        return None
    c = Counter()
    for r in csv.DictReader(io.StringIO(text)):
        d = (r.get('Div') or '').strip()
        if d:
            c[d] += 1
    return c.most_common(1)[0][0] if c else None

LEAGUES = [
    'E0','E1','E2','E3','EC','SP1','SP2','I1','I2','D1','D2',
    'F1','F2','N1','B1','P1','T1','G1','SC0','SC1','SC2','SC3',
]

def current_seasons():
    y, m = datetime.now().year, datetime.now().month
    start = y if m >= 7 else y - 1
    cur = f"{str(start)[2:]}{str(start+1)[2:]}"
    prev = f"{str(start-1)[2:]}{str(start)[2:]}"
    return [prev, cur]

def main():
    seasons = current_seasons()
    print(f"=== Refreshing league data — {datetime.now().strftime('%Y-%m-%d %H:%M')} ===")
    print(f"Seasons: {seasons}")

    updated = 0
    failed = 0
    rejected = []

    # VALIDATE THE CONTENT, NOT JUST THE SIZE.
    #
    # On 2026-08-17 this saved EC (National League) data as E0_2627.csv, P1 as
    # SP1_2627.csv and SC2 as SP2_2627.csv -- byte-identical duplicates of the
    # other league's file. The old check was `size < 200`, which a complete but
    # WRONG csv passes trivially, so three divisions carried another country's
    # results for five days with nothing logged. These files are the last-resort
    # source for bet resolution, so wrong rows are worse than missing ones.
    #
    # The source does it via content negotiation: https .../E0.csv currently
    # answers `300 Multiple Choices`, and depending on headers it can serve a
    # neighbouring file instead. We cannot fix their server, so verify here --
    # every row carries its own Div code, making the file self-identifying.
    #
    # Download to a temp path and only swap it in once the Div matches, so a bad
    # fetch can never overwrite a good file that is already on disk.
    for season in seasons:
        for lg in LEAGUES:
            url = f'https://www.football-data.co.uk/mmz4281/{season}/{lg}.csv'
            path = os.path.join(OUT_DIR, f'{lg}_{season}.csv')
            tmp = path + '.tmp'
            try:
                req = urllib.request.Request(
                    url, headers={'User-Agent': 'Mozilla/5.0',
                                  'Accept': 'text/csv,application/csv,*/*'})
                with urllib.request.urlopen(req, timeout=60) as r:
                    raw = r.read()
                if len(raw) < 200:
                    failed += 1
                    continue
                got = file_div(raw)
                if got != lg:
                    rejected.append((f'{lg}_{season}', got or '(no Div column)'))
                    failed += 1
                    continue
                with open(tmp, 'wb') as fh:
                    fh.write(raw)
                os.replace(tmp, path)
                updated += 1
            except Exception as e:
                print(f"  FAIL: {lg}_{season} — {e}")
                failed += 1
            finally:
                if os.path.exists(tmp):
                    try:
                        os.remove(tmp)
                    except OSError:
                        pass

    print(f"Done: {updated} updated, {failed} failed")
    if rejected:
        print(f"  REJECTED {len(rejected)} download(s) whose content was the wrong division:")
        for name, got in rejected:
            print(f"    {name}: served Div={got}")
        print("  The existing file on disk was left untouched. Run "
              "research\\audit_league_files.py to see whether it is also wrong.")

if __name__ == '__main__':
    main()
