#!/usr/bin/env python3
"""Reproduce the historical FMCSA carrier-mix analysis using local published-count inputs.

Run: python reproduce.py
Only Python's standard library is required. No network requests are made.
The script validates source-count subtotals, all five delivered cohort rows,
percentage rounding and the within-sample composition difference.
"""
from __future__ import annotations
import csv
from decimal import Decimal, ROUND_HALF_UP
from pathlib import Path

ROOT = Path(__file__).resolve().parent

def read_csv(filename: str) -> list[dict[str, str]]:
    path = ROOT / filename
    if not path.is_file():
        raise FileNotFoundError(f'Required input missing: {path}')
    with path.open(encoding='utf-8', newline='') as f:
        rows = list(csv.DictReader(f))
    if not rows:
        raise ValueError(f'No records in {filename}')
    return rows

def pct(numerator: int, denominator: int) -> Decimal:
    if denominator <= 0 or numerator < 0 or numerator > denominator:
        raise ValueError('Invalid numerator or denominator')
    return Decimal(numerator) / Decimal(denominator) * 100

def main() -> None:
    inputs = read_csv('fmcsa-operation-inputs.csv')
    numeric = ('carriers', 'detained_stops', 'not_detained_stops', 'included_stops')
    base: dict[str, dict[str, int]] = {}
    for row in inputs:
        cid = row['cohort_id']
        if cid in base:
            raise ValueError(f'Duplicate base cohort: {cid}')
        values = {k: int(row[k]) for k in numeric}
        if values['detained_stops'] + values['not_detained_stops'] != values['included_stops']:
            raise ValueError(f'Source-count mismatch for {cid}')
        base[cid] = values
    if set(base) != {'for_hire_truckload', 'for_hire_ltl', 'private'}:
        raise ValueError('Unexpected set of source cohorts')
    groups = {k: dict(v) for k, v in base.items()}
    for cid, ids in {
        'for_hire_combined': ('for_hire_truckload', 'for_hire_ltl'),
        'all_included': ('for_hire_truckload', 'for_hire_ltl', 'private'),
    }.items():
        groups[cid] = {k: sum(base[x][k] for x in ids) for k in numeric}
    delivered = read_csv('truck-detention-carrier-mix-2026-09-11.csv')
    if len(delivered) != 5 or len({x['cohort_id'] for x in delivered}) != 5:
        raise ValueError('Expected exactly five unique delivered cohort rows')
    for row in delivered:
        g = groups[row['cohort_id']]
        for key in numeric:
            if g[key] != int(row[key]):
                raise ValueError(f'Delivered value mismatch: {row["cohort_id"]}/{key}')
        p = pct(g['detained_stops'], g['included_stops'])
        if abs(p-Decimal(row['detained_share_percent'])) > Decimal('0.000000000001'):
            raise ValueError('Stored percentage mismatch')
        rounded = p.quantize(Decimal('.01'), rounding=ROUND_HALF_UP)
        if rounded != Decimal(row['display_share_percent']):
            raise ValueError('Displayed percentage mismatch')
        print(f'{row["operation_type"]}: {g["detained_stops"]:,} / {g["included_stops"]:,} = {p:.12f}% ({rounded}%)')
    fh = pct(groups['for_hire_combined']['detained_stops'], groups['for_hire_combined']['included_stops'])
    pooled = pct(groups['all_included']['detained_stops'], groups['all_included']['included_stops'])
    gap = fh-pooled
    private_share = pct(base['private']['included_stops'], groups['all_included']['included_stops'])
    if gap.quantize(Decimal('.01')) != Decimal('8.66'):
        raise ValueError('Gap did not reproduce')
    if private_share.quantize(Decimal('.01')) != Decimal('58.28'):
        raise ValueError('Private stop share did not reproduce')
    print(f'For-hire minus pooled: {gap:.12f} percentage points')
    print(f'Private share of included stops: {private_share:.12f}%')
    print('All input, aggregate, percentage and rounding checks passed.')

if __name__ == '__main__':
    main()
