"""Reproduce AMR's source-defined crude stock comparison, standard-library Python.

Doc: Strategic and commercial crude stocks analytical edition
Status: Fixed November 14, 2026 source edition
Owner: American Mineral Registry
Last verified: 2026-11-14
Depends on: pinned stock-matched-input.csv and original EIA workbooks
Run: python3 analysis.py
"""
from pathlib import Path
from decimal import Decimal,getcontext
from fractions import Fraction
from collections import Counter
import csv,json,hashlib
getcontext().prec=34
ROOT=Path(__file__).resolve().parent;DATA=ROOT/'data';DATA.mkdir(exist_ok=True);(ROOT/'qa').mkdir(exist_ok=True);D=Decimal
URLS={'commercial':'https://www.eia.gov/dnav/pet/hist/LeafHandler.ashx?n=PET&s=MCESTUS1&f=M','spr':'https://www.eia.gov/dnav/pet/hist/LeafHandler.ashx?n=PET&s=MCSSTUS1&f=M','total':'https://www.eia.gov/dnav/pet/hist/LeafHandler.ashx?n=PET&s=MCRSTUS1&f=M'}
CHECKS=[]
def check(name,ok):CHECKS.append(name);assert ok,name
def out(name,rows):
    check(name+' complete',bool(rows) and all(v is not None and str(v)!='' for r in rows for v in r.values()))
    with(DATA/name).open('w',newline='')as f:w=csv.DictWriter(f,fieldnames=list(rows[0]));w.writeheader();w.writerows(rows)
def pct(a,b):return D(a)/D(b)*100
def sign(n):return 'rising' if n>0 else 'falling' if n<0 else 'unchanged'
def main():
    raw=list(csv.DictReader((ROOT/'sources/stock-matched-input.csv').open()));allrows=[]
    for r in raw:
        a={k:int(r[k+'_thousand_barrels']) for k in ['commercial','spr','total']};check(r['month']+' components',a['commercial']+a['spr']==a['total']);check(r['month']+' nonnegative',all(x>=0 for x in a.values()))
        row=dict(month=r['month'],commercial_thousand_barrels=a['commercial'],spr_thousand_barrels=a['spr'],total_thousand_barrels=a['total'],spr_share_percent=pct(a['spr'],a['total']),commercial_share_percent=pct(a['commercial'],a['total']),observation='month-end stock, not monthly average or flow',commercial_source=URLS['commercial'],spr_source=URLS['spr'],total_source=URLS['total']);allrows.append(row)
        check(r['month']+' independent SPR share',abs(row['spr_share_percent']-D(Fraction(a['spr']*100,a['total']).numerator)/D(Fraction(a['spr']*100,a['total']).denominator))<D('1e-27'))
    expected=[f'{y}-{m:02}' for y in range(2005,2026) for m in range(1,13)];history=[r for r in allrows if r['month']<='2025-12'];check('252 complete fixed months',[r['month'] for r in history]==expected)
    recent=[r for r in allrows if r['month']>'2025-12'];check('Eight separate 2026 observations',[r['month'] for r in recent]==[f'2026-{m:02}' for m in range(1,9)])
    out('monthly-stocks-2005-2025.csv',history);out('recent-observed-months-2026.csv',recent)
    changes=[]
    for before,after in zip(history,history[1:]):
        dif={k:after[k+'_thousand_barrels']-before[k+'_thousand_barrels'] for k in ['commercial','spr','total']};check(after['month']+' monthly bridge',dif['total']==dif['commercial']+dif['spr'])
        changes.append(dict(month=after['month'],previous_month=before['month'],**{k+'_change_thousand_barrels':v for k,v in dif.items()},spr_direction=sign(dif['spr']),commercial_direction=sign(dif['commercial']),spr_current_thousand_barrels=after['spr_thousand_barrels'],spr_previous_thousand_barrels=before['spr_thousand_barrels'],commercial_current_thousand_barrels=after['commercial_thousand_barrels'],commercial_previous_thousand_barrels=before['commercial_thousand_barrels'],total_current_thousand_barrels=after['total_thousand_barrels'],total_previous_thousand_barrels=before['total_thousand_barrels']))
    out('consecutive-month-stock-changes.csv',changes)
    ends=[r for r in history if r['month'].endswith('-12')];out('december-year-end-stocks.csv',ends)
    annual=[]
    for a,b in zip(ends,ends[1:]):
        row=dict(year=b['month'][:4],prior_year=a['month'][:4])
        for k in ['spr','commercial','total']:
            before=a[k+'_thousand_barrels'];after=b[k+'_thousand_barrels'];row.update({k+'_previous_thousand_barrels':before,k+'_current_thousand_barrels':after,k+'_change_thousand_barrels':after-before,k+'_change_percent':(D(after)/before-1)*100})
        check('Annual bridge '+row['year'],row['spr_change_thousand_barrels']+row['commercial_change_thousand_barrels']==row['total_change_thousand_barrels']);annual.append(row)
    out('matched-december-annual-changes.csv',annual)
    extremes=[]
    for y in range(2005,2026):
        group=[r for r in history if r['month'].startswith(str(y))];dec=group[-1]
        for k in ['spr','commercial','total']:
            key=k+'_thousand_barrels';lo=min(r[key] for r in group);hi=max(r[key] for r in group)
            extremes.append(dict(year=y,series=k,minimum_thousand_barrels=lo,minimum_months=';'.join(r['month'] for r in group if r[key]==lo),maximum_thousand_barrels=hi,maximum_months=';'.join(r['month'] for r in group if r[key]==hi),december_thousand_barrels=dec[key],within_year_range_thousand_barrels=hi-lo,december_minus_minimum_thousand_barrels=dec[key]-lo,maximum_minus_december_thousand_barrels=hi-dec[key],source_url=URLS[k]))
    out('within-year-month-end-extremes.csv',extremes)
    matrix=[]
    for a in ['rising','falling','unchanged']:
        for b in ['rising','falling','unchanged']:
            n=sum(r['spr_direction']==a and r['commercial_direction']==b for r in changes);matrix.append(dict(spr_direction=a,commercial_direction=b,observed_months=n,matched_comparisons=len(changes),share_percent=pct(n,len(changes))))
    check('Direction matrix denominator',sum(r['observed_months'] for r in matrix)==251);out('joint-stock-direction-frequencies.csv',matrix)
    large=[]
    for k in ['spr','commercial','total']:
        key=k+'_change_thousand_barrels'
        for direction,reverse in [('largest increase',True),('largest decrease',False)]:
            eligible=[r for r in changes if r[key]>0] if reverse else [r for r in changes if r[key]<0]
            ranked=sorted(eligible,key=lambda r:(-r[key] if reverse else r[key],r['month']))[:5]
            for i,r in enumerate(ranked,1):large.append(dict(series=k,direction=direction,rank=i,month=r['month'],previous_month=r['previous_month'],change_thousand_barrels=r[key],previous_thousand_barrels=r[k+'_previous_thousand_barrels'],current_thousand_barrels=r[k+'_current_thousand_barrels'],source_url=URLS[k]))
    out('largest-monthly-stock-changes.csv',large)
    latest=recent[-1];lastend=ends[-1];out('comparison-snapshots.csv',[lastend,latest]);recentbridge=[]
    for k in ['spr','commercial','total']:
        recentbridge.append(dict(series=k,previous_month='2025-12',latest_month=latest['month'],previous_thousand_barrels=lastend[k+'_thousand_barrels'],latest_thousand_barrels=latest[k+'_thousand_barrels'],change_thousand_barrels=latest[k+'_thousand_barrels']-lastend[k+'_thousand_barrels'],change_percent=(D(latest[k+'_thousand_barrels'])/lastend[k+'_thousand_barrels']-1)*100,source_url=URLS[k]))
    out('latest-observed-versus-december.csv',recentbridge)
    overview=[]
    for k in ['spr','commercial','total']:
        key=k+'_thousand_barrels';lo=min(history,key=lambda r:r[key]);hi=max(history,key=lambda r:r[key]);overview.append(dict(series=k,fixed_first_month=history[0]['month'],fixed_last_month=history[-1]['month'],first_thousand_barrels=history[0][key],last_thousand_barrels=history[-1][key],minimum_month=lo['month'],minimum_thousand_barrels=lo[key],maximum_month=hi['month'],maximum_thousand_barrels=hi[key],source_url=URLS[k]))
    out('fixed-panel-summary.csv',overview)
    opposite=sum((r['spr_change_thousand_barrels']>0 and r['commercial_change_thousand_barrels']<0) or (r['spr_change_thousand_barrels']<0 and r['commercial_change_thousand_barrels']>0) for r in changes)
    result=dict(status='PASS',checks=len(CHECKS),fixed_months=252,monthly_changes=251,year_ends=21,annual_changes=20,current_observed_months=8,opposite_direction_months=opposite,opposite_share_percent=pct(opposite,251),latest=latest,last_december=lastend,worst_annual_decline=min(annual,key=lambda r:r['total_change_thousand_barrels']),summary=overview,direction_matrix=matrix)
    (DATA/'analysis-summary.json').write_text(json.dumps(result,default=str,indent=2)+'\n');(ROOT/'qa/results.json').write_text(json.dumps(result,default=str,indent=2)+'\n');print(json.dumps({k:v for k,v in result.items() if k in ['status','checks','fixed_months','opposite_direction_months','opposite_share_percent']},default=str))
if __name__=='__main__':main()
