#!/usr/bin/env python3
"""Reproduce the 1000pound.com 2025 competition-entry analysis using only Python's standard library.

python3 analyze-powerlifting.py openpowerlifting.zip output-directory
Official export: https://openpowerlifting.gitlab.io/opl-csv/bulk-csv.html
Data: OpenPowerlifting, public domain. This analysis code: MIT, 2026 1000 Pound Club.
"""
import csv, hashlib, io, json, pathlib, statistics, sys, zipfile
from collections import Counter

THRESHOLD_KG = 453.59237  # exactly 1000 international pounds
START, END = '2025-01-01', '2026-01-01'

def number(value):
    try:
        return float(value)
    except (ValueError, TypeError):
        return None

def summarize(rows, basis, group, label):
    totals = sorted(r['total'] for r in rows)
    n = len(totals)
    above = sum(t >= THRESHOLD_KG for t in totals)
    return dict(basis=basis, group=group, cohort=label, entries=n, at_or_above=above,
                below=n-above, share_at_or_above=round(above/n*100, 4) if n else None,
                median_total_kg=statistics.median(totals) if n else None)

def aggregate(entries):
    result = []
    best = {}
    for row in entries:
        key = (row['name'], row['sex'])
        if key not in best or row['total'] > best[key]['total']:
            best[key] = row
    for basis, rows in [('meet_entries', entries), ('best_per_lifter', list(best.values()))]:
        for sex in sorted({r['sex'] for r in rows}):
            cohort = [r for r in rows if r['sex'] == sex]
            result.append(summarize(cohort, basis, 'sex', sex))
            if basis != 'meet_entries':
                continue
            bands = [('≤60 kg', lambda w: w is not None and w <= 60),
                     ('>60–75 kg', lambda w: w is not None and 60 < w <= 75),
                     ('>75–90 kg', lambda w: w is not None and 75 < w <= 90),
                     ('>90–110 kg', lambda w: w is not None and 90 < w <= 110),
                     ('>110 kg', lambda w: w is not None and w > 110),
                     ('Unknown bodyweight', lambda w: w is None)]
            for label, check in bands:
                result.append(summarize([r for r in cohort if check(r['bw'])], basis, 'bodyweight', sex+' / '+label))
            for label, check in [('Under 18', lambda a: a is not None and a < 18),
                                 ('18–39', lambda a: a is not None and 18 <= a < 40),
                                 ('40+', lambda a: a is not None and a >= 40),
                                 ('Unknown age', lambda a: a is None)]:
                result.append(summarize([r for r in cohort if check(r['age'])], basis, 'age', sex+' / '+label))
            for tested in [True, False]:
                result.append(summarize([r for r in cohort if r['tested'] == tested], basis, 'tested', sex+' / '+('Tested=Yes' if tested else 'Not marked tested')))
    return result

def main(archive, destination):
    out = pathlib.Path(destination)
    out.mkdir(parents=True, exist_ok=True)
    digest = hashlib.file_digest(open(archive, 'rb'), 'sha256').hexdigest()
    counts, meets, examples = Counter(), {}, {}
    with zipfile.ZipFile(archive) as z:
        filename = next(n for n in z.namelist() if n.endswith('.csv'))
        reader = csv.DictReader(io.TextIOWrapper(z.open(filename), encoding='utf-8-sig', newline=''))
        for row in reader:
            counts['source_rows'] += 1
            if not START <= row['Date'] < END:
                continue
            counts['rows_in_2025'] += 1
            if row['Equipment'] != 'Raw' or row['Event'] != 'SBD':
                continue
            counts['raw_sbd_rows'] += 1
            total = number(row['TotalKg'])
            if not total or total <= 0 or row['Place'] in ('DQ', 'DD', 'NS') or any((number(row[k]) or 0) <= 0 for k in ['Best3SquatKg', 'Best3BenchKg', 'Best3DeadliftKg']):
                counts['excluded_no_valid_total'] += 1
                continue
            counts['valid_rows_before_dedup'] += 1
            key = tuple(row[k] for k in ['Name', 'Sex', 'Federation', 'Date', 'MeetCountry', 'MeetState', 'MeetTown', 'MeetName'])
            value = dict(name=row['Name'], sex=row['Sex'], total=total, bw=number(row['BodyweightKg']), age=number(row['Age']), tested=row['Tested']=='Yes')
            if key in meets:
                counts['duplicate_meet_rows_removed'] += 1
                if meets[key]['total'] != total:
                    counts['duplicate_rows_with_different_total'] += 1
            if key not in meets or total > meets[key]['total']:
                meets[key] = value
            # Published rounded scores supply independent coefficient test fixtures.
            if row['Sex'] in ('M', 'F') and row['Sex'] not in examples and value['bw'] and row.get('Dots'):
                examples[row['Sex']] = {k:row[k] for k in ['Name','Sex','BodyweightKg','TotalKg','Dots','Date','Federation','MeetName']}
    entries = list(meets.values())
    counts['deduplicated_meet_entries'] = len(entries)
    counts['named_lifters'] = len({(r['name'], r['sex']) for r in entries})
    aggregates = aggregate(entries)
    result = dict(snapshot='2026-11-14', source_revision='6b8787a8', source_csv=filename,
                  archive_sha256=digest, source_url='https://openpowerlifting.gitlab.io/opl-csv/files/openpowerlifting-latest.zip',
                  analysis_date='2026-11-18', start_inclusive=START, end_exclusive=END,
                  threshold_kg=THRESHOLD_KG, counts=dict(counts), aggregates=aggregates)
    (out/'powerlifting-2025-summary.json').write_text(json.dumps(result, indent=2, ensure_ascii=False)+'\n')
    with (out/'powerlifting-2025-aggregates.csv').open('w', newline='') as f:
        writer = csv.DictWriter(f, fieldnames=list(aggregates[0]))
        writer.writeheader(); writer.writerows(aggregates)
    (out/'dots-source-fixtures.json').write_text(json.dumps(examples, indent=2)+'\n')
    print(json.dumps({'counts':dict(counts), 'main':[a for a in aggregates if a['group']=='sex'], 'dots_fixtures':examples}, indent=2))

if __name__ == '__main__':
    main(*sys.argv[1:])
