# SPDX-License-Identifier: MIT
# Copyright (c) 2026 PLAO-DAS
"""Reproduce the archived calculations only. Offline; standard library by default.

Optional --check-originals rechecks the eight original files (openpyxl required).
Optional --plot redraws the same indices (matplotlib required; English labels).
This portable edition is not a byte-identical copy of the original environment-bound scripts.
"""
from pathlib import Path
from decimal import Decimal, getcontext
import csv, json, hashlib, argparse, re, calendar

getcontext().prec=28
D=Decimal
ROOT=Path(__file__).resolve().parents[1]
def read(p):return list(csv.DictReader(p.open(encoding='utf-8-sig')))
def digest(p):return hashlib.sha256(p.read_bytes()).hexdigest()
def direction(v):return 'increase' if v>0 else 'decrease' if v<0 else 'unchanged'
def percent(a,b):return (a/b-1)*100
def serialized(v):return '' if v is None else str(round(v,10))
def dump_csv(path,rows):
    with path.open('w',encoding='utf-8-sig',newline='') as f:
        w=csv.DictWriter(f,fieldnames=list(rows[0]));w.writeheader();w.writerows(rows)

def check_originals(folder,nominal,cpi):
    import openpyxl
    catalog=read(ROOT/'sources/originals_required.csv')
    paths={r['source_id']:folder/r['original_filename'] for r in catalog}
    for r in catalog:
        p=paths[r['source_id']]
        if not p.is_file():raise ValueError('Missing original file: '+r['original_filename'])
        if digest(p)!=r['original_sha256']:
            raise ValueError('Original hash differs; do not silently substitute a new vintage: '+r['original_filename'])
    books={sid:openpyxl.load_workbook(path,read_only=True,data_only=True) for sid,path in paths.items() if sid!='ons_cpi_data'}
    for r in nominal:
        sn,cell=re.fullmatch(r"sheet '([^']+)' cell ([A-Z]+\d+)",r['source_locator']).groups()
        assert D(str(books[r['source_id']][sn][cell].value))==D(r['expenditure_gbp_household_week'])
    raw=list(csv.reader(paths['ons_cpi_data'].open(encoding='utf-8-sig')))
    assert raw[0][1]=='CPI INDEX 03 : CLOTHING AND FOOTWEAR 2015=100'
    assert ['CDID','D7BW'] in raw
    for r in cpi:
        line=int(re.search(r'CSV row (\d+)',r['source_locator'])[1])
        label=r['period'][:4]+' '+calendar.month_abbr[int(r['period'][5:])].upper()
        assert raw[line-1][0]==label and D(raw[line-1][1])==D(r['value'])
    return {'status':'PASS','original_files':8,'LCF_cells':70,'CPI_cells':84}

def draw(rows,path):
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    from matplotlib.lines import Line2D
    fig,axes=plt.subplots(2,5,figsize=(15.5,8.1),sharex=True,sharey=True)
    for ax,d in zip(axes.flat,range(1,11)):
        group=[r for r in rows if r['income_decile']==d];years=[r['fye'] for r in group]
        for field,color,style,marker in [('nominal_index_FYE2019_100','#2877A5','-','o'),('real_index_FYE2019_100','#C76C2D','--','s')]:
            lookup={r['fye']:float(r[field]) for r in group}
            ax.scatter(years,[lookup[y] for y in years],s=19,color=color,marker=marker,zorder=4)
            for segment in [(2019,2020),(2021,2022),(2024,2025)]:
                ax.plot(segment,[lookup[y] for y in segment],color=color,linestyle=style,linewidth=1.6)
        ax.axhline(100,color='#9AA3AB',linewidth=.8)
        ax.axvspan(2020.7,2021.3,color='#DCE2E7',alpha=.55)
        for pos in [2022.5,2023.5]:ax.axvline(pos,color='#B4BBC2',linestyle=':',linewidth=1)
        ax.set_title('D%d | latest real %+.1f%%'%(d,float(group[-1]['real_yoy_pct'])),fontsize=10)
        ax.set_xticks([2019,2021,2023,2025]);ax.set_ylim(25,115);ax.grid(axis='y',color='#E5E9EC')
    fig.suptitle('UK clothing & footwear: all six nominal increases remain positive in real terms',x=.055,y=.97,ha='left',fontsize=16)
    fig.legend(handles=[Line2D([0],[0],color='#2877A5',label='Nominal'),Line2D([0],[0],color='#C76C2D',linestyle='--',label='Real, FYE2025 prices')],loc='upper right',bbox_to_anchor=(.96,.93),ncol=2,frameon=False)
    fig.subplots_adjust(left=.055,right=.98,top=.82,bottom=.20,hspace=.42,wspace=.16)
    fig.text(.055,.14,'FYE = year ending March; indices FYE2019=100. Method breaks at FYE2023 and FYE2024 are not joined.',fontsize=10)
    fig.text(.055,.10,'Source: ONS LCF Table 3.1 and CPI D7BW. Common CPI; descriptive survey means; not purchase quantities.',fontsize=10)
    fig.text(.055,.06,'Portable redraw of archived results; labels, fonts and PNG bytes differ from the published original figure.',fontsize=10)
    fig.savefig(path,dpi=180);plt.close(fig)

def main():
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--output',type=Path,default=ROOT/'reproduced')
    parser.add_argument('--plot',action='store_true')
    parser.add_argument('--check-originals',type=Path,metavar='DIRECTORY')
    args=parser.parse_args()
    nominal=read(ROOT/'data/uk_income_clothing_analysis.csv')
    cpi=read(ROOT/'data/ons_clothing_cpi_monthly.csv')
    reference=read(ROOT/'data/uk_income_clothing_real_analysis.csv')
    assert len(nominal)==70 and len(cpi)==84 and len(reference)==70
    assert len({(r['fye'],r['income_decile']) for r in nominal})==70
    assert len({r['period'] for r in cpi})==84
    assert all(r['seasonal_adjustment']=='NSA' and r['base_period']=='2015=100' and r['series_id']=='GB_ONS_D7BW' for r in cpi)
    x={(int(r['fye']),int(r['income_decile'])):D(r['expenditure_gbp_household_week']) for r in nominal}
    prices={r['period']:D(r['value']) for r in cpi}
    means={}
    for y in range(2019,2026):
        months=[f'{y-1}-{m:02}' for m in range(4,13)]+[f'{y}-{m:02}' for m in range(1,4)]
        assert all(m in prices for m in months),'Missing CPI month; no interpolation allowed'
        means[y]=sum((prices[m] for m in months),D(0))/12
    real={(y,d):v*(means[2025]/means[y]) for (y,d),v in x.items()}
    expected={(int(r['fye']),int(r['income_decile'])):r for r in reference}
    rows=[];comparisons=0
    for y in range(2019,2026):
        for d in range(1,11):
            old=expected[y,d]
            assert old['period_start']==f'{y-1}-04-01' and old['period_end']==f'{y}-03-31'
            ny=None if y==2019 else percent(x[y,d],x[y-1,d]);ry=None if y==2019 else percent(real[y,d],real[y-1,d])
            numerical=dict(nominal_expenditure_gbp_household_week=x[y,d],real_expenditure_gbp_household_week=real[y,d],
                cpi_fy_average=means[y],cpi_reference_fy_average=means[2025],deflation_factor=means[2025]/means[y],
                nominal_yoy_pct=ny,real_yoy_pct=ry,cpi_yoy_pct=None if y==2019 else percent(means[y],means[y-1]),
                nominal_index_FYE2019_100=100*x[y,d]/x[2019,d],real_index_FYE2019_100=100*real[y,d]/real[2019,d],
                nominal_change_from_FYE2019_pct=percent(x[y,d],x[2019,d]),real_change_from_FYE2019_pct=percent(real[y,d],real[2019,d]))
            for field,value in numerical.items():
                if value is None:assert old[field]==''
                else:assert abs(D(old[field])-value)<D('0.00000001'),(y,d,field)
                comparisons+=1
            flags=dict(nominal_yoy_direction='' if ny is None else direction(ny),real_yoy_direction='' if ry is None else direction(ry),
                yoy_direction_differs='' if ny is None else str(direction(ny)!=direction(ry)).lower(),
                yoy_strict_sign_reversal='' if ny is None else str(ny*ry<0).lower(),
                from_base_direction_differs=str(direction(numerical['nominal_change_from_FYE2019_pct'])!=direction(numerical['real_change_from_FYE2019_pct'])).lower())
            assert all(old[k]==v for k,v in flags.items())
            rows.append(dict(fye=y,income_decile=d,**{k:serialized(v) for k,v in numerical.items()},**flags))
    latest=[r for r in rows if r['fye']==2025]
    nominal_up=[r['income_decile'] for r in latest if D(r['nominal_yoy_pct'])>0]
    real_up=[r['income_decile'] for r in latest if D(r['real_yoy_pct'])>0]
    assert nominal_up==real_up==[2,4,7,8,9,10]
    assert all(D(r['real_change_from_FYE2019_pct'])<0 for r in latest)
    original_check={'status':'NOT_RUN','reason':'Original files are not included; use --check-originals to recheck source cells and original hashes.'}
    if args.check_originals:original_check=check_originals(args.check_originals,nominal,cpi)
    args.output.mkdir(parents=True,exist_ok=True)
    dump_csv(args.output/'uk_income_clothing_real_analysis.csv',rows)
    summary=dict(scope='Reproduction of archived calculations only; not new research or independent statistical validation',status='PASS',
        rows=70,cpi_months=84,numerical_field_comparisons=comparisons,tolerance='absolute 1e-8, exported decimal rounding',
        nominal_increase_deciles=nominal_up,real_increase_deciles=real_up,original_source_check=original_check,
        newly_tested_significance=False,imputation=False)
    if args.plot:draw(rows,args.output/'uk_income_clothing_real_chart.png')
    (args.output/'reproduction_check.json').write_text(json.dumps(summary,ensure_ascii=False,indent=2))
    print(json.dumps(summary,ensure_ascii=False,indent=2))

if __name__=='__main__':main()
