"""Stage 1 v3 requirements model: work backward from the 448.5T target.
1. Target economy by layer, base mix (the v1 income-elasticity split on the repaired 2026 layer levels) and alternative
   mix (homothetic: every layer doubles); the v1 split as published (share_base) is kept for comparison (E1).
2. Requirements: each layer's target output x the best achievable 2035 ratio of every input (energy, labour, machine
   labour, compute, materials, capital, land, permission); the baseline / trend ratio alongside.
3. Supply: by layer from the repaired baseline (evidence-backed on course, unbacked, on-course shortfall carried,
   joint maximum); by input from the repaired Agent 4 rerun (on course and fastest precedent), plus aluminium,
   capital stock and cropland added here.
4. Missing, counted once: by layer missing = target - (evidence-backed on course - on-course shortfall), tier 1 = the
   part within the joint maximum, tier 2 = the rest, surplus = joint maximum beyond the target (never summed);
   by input missing = required - on-course supply, tier 1 up to the precedent supply, tier 2 beyond, surplus apart.
run(cfg) returns every table; cfg switches the five error tests (code/errors5.py). Nothing is floored: a negative
missing stays negative, the on-course shortfall is subtracted as it is."""
import math, os, sys

HERE = os.path.dirname(os.path.abspath(__file__)); sys.path.insert(0, HERE)
from common import (V, C, A, E, S, LAYERS, NO_PROD, ACCOUNTING, LAY, GL, LI, BOT, NBL, BM, D, LY, BT, PPF, NB, MM, LIv, EXIO,
                    WORK, M2SUM, BLV2, r6)
import inputs as I
from common import R as R_DIR
import pandas as pd

CODE = 'code/model.py'
EX = pd.read_csv(os.path.join(WORK, 'exio_extra.csv')).set_index('layer_id')
EXS = 'code/exio.py;A4:S52;A4:A09;A4:work/exio_world.npz'


def c(v, *deps, label=None):
    return C(v, *deps, code=CODE, label=label)


ZERO = E(0.0, CODE)
INV_LAYERS = ('C13', 'C14', 'C15')

# ================================================================== 1. target economy
TODAY = {L: LY(L, 'today_2026_ppp_bn') for L in LAYERS}
SUM26 = c(sum(x.v for x in TODAY.values()), *TODAY.values())
ETA = {L: V(GL.loc[L, 'eta'], GL.loc[L, 'eta_label'], 'B:gap_by_layer.csv:eta') for L in LAYERS}
B35, GAP, TGT = BM('BM03_base'), BM('BM05_base'), BM('BM04')
XG = c(GAP.v / B35.v, GAP, B35)


def mix_base():
    """v1 Engel split (B:code/bridge.py: share x ((1 + gap/baseline)^eta - 1), renormalised to the gap) on the repaired
    2026 layer shares, plus the IMF baseline split at the same shares; rescaled to exactly target A."""
    s = {L: c(TODAY[L].v / SUM26.v, TODAY[L], SUM26) for L in LAYERS}
    raw = {L: c(s[L].v * ((1 + XG.v) ** ETA[L].v - 1), s[L], XG, ETA[L]) for L in LAYERS}
    rs = sum(x.v for x in raw.values())
    t0 = {L: c(s[L].v * B35.v + GAP.v * raw[L].v / rs, s[L], B35, GAP, raw[L], *raw.values()) for L in LAYERS}
    k = TGT.v / sum(x.v for x in t0.values())
    return {L: c(t0[L].v * k, t0[L], TGT) for L in LAYERS}, s


def mix_alt():
    return {L: c(2 * TODAY[L].v, TODAY[L]) for L in LAYERS}


def mix_v1pub():
    t0 = {L: c(GL.loc[L, 'share_base'] * B35.v + GL.loc[L, 'required_base_ppp_bn'],
               V(GL.loc[L, 'share_base'], 'E', 'B:gap_by_layer.csv:share_base'), B35,
               V(GL.loc[L, 'required_base_ppp_bn'], GL.loc[L, 'required_base_ppp_bn_label'], 'B:gap_by_layer.csv:required_base_ppp_bn'))
          for L in LAYERS}
    k = TGT.v / sum(x.v for x in t0.values())
    return {L: c(t0[L].v * k, t0[L], TGT) for L in LAYERS}, sum(x.v for x in t0.values())


# ================================================================== 2. ratios (per bn 2026 int$ of layer output)
GDP9 = I.gdp_9()
TAU = dict(
    e_S=I.tau24(I.GEN24, I.GEN35S), e_N=I.tau24(I.GEN24, I.GEN35N),
    f_S=I.tau24(I.F24, I.F35S), f_N=I.tau24(I.F24, I.F35N),
    st_S=I.tau24(I.STL24, I.STL35S), st_N=I.tau24(I.STL24, I.STL35N),
    ce_S=I.tau24(I.CEM24, I.CEM35S), ce_N=I.tau24(I.CEM24, I.CEM35N),
    al_S=I.tau24(I.AL24, I.AL35S), al_N=I.tau24(I.AL24, I.AL35N),
    cu_S=I.tau_gcmo(I.CU25, I.CU30S, I.CU40S), cu_H=I.tau_gcmo(I.CU25, I.CU30H, I.CU40H),
    li_S=I.tau_gcmo(I.LI25, I.LI30S, I.LI40S), li_H=I.tau_gcmo(I.LI25, I.LI30H, I.LI40H),
    re_S=I.tau_gcmo(I.RE25, I.RE30S, I.RE40S), re_H=I.tau_gcmo(I.RE25, I.RE30H, I.RE40H))


def fac(best, alt):
    return c(TAU[best].v / TAU[alt].v, TAU[best], TAU[alt])


F_E, F_F, F_ST = fac('e_N', 'e_S'), fac('f_N', 'f_S'), fac('st_N', 'st_S')
F_CU, F_LI, F_RE = fac('cu_H', 'cu_S'), fac('li_H', 'li_S'), fac('re_H', 're_S')


def nb(k, L):
    x = NB(k, L)
    return x if x is not None else ZERO


# cement: 2026 intensities (Agent 4: non-C13 layers held at 2026; C13 2026 = its EXIOBASE share of 2026 cement / C13 2026)
CE26 = c(sum(D(f'D_CEM_{r}_2026').v for r in ('CHN', 'IND', 'ROW')) / 1000, *[D(f'D_CEM_{r}_2026') for r in ('CHN', 'IND', 'ROW')])
C13SH = MM('c13share_cement')


def cem26(L):
    if L == 'C13':
        return c(C13SH.v * CE26.v / TODAY['C13'].v, C13SH, CE26, TODAY['C13'])
    return nb('K10', L)


def calib(col, u26, table, srcx):
    """EXIOBASE per-EUR footprint -> units per bn 2026 int$ (PPP), calibrated to world use u26 in 2026 (Agent 4's calib)."""
    base = sum(table.loc[L, col] * TODAY[L].v / PPF(L).v for L in LAYERS if L not in NO_PROD)
    return {L: (ZERO if L in NO_PROD else c(u26.v * table.loc[L, col] / PPF(L).v / base, u26, TODAY[L], PPF(L), E(0, srcx)))
            for L in LAYERS}


AL26 = c(I.AL24.v * (I.AL35S.v / I.AL24.v) ** (2 / 11), I.AL24, I.AL35S)
I_AL26 = calib('aluminium', AL26, EX, EXS)
# capital stock (tn 2026 int$): PWT world K/Y 2023 x world GDP 2023, grown at the 2013-23 real capital trend to 2026
KY23, KYMIN, GK_TR, GK_MX = I.LCv('KY_2023'), I.LCv('KY_MIN'), I.LCv('GK_1323'), I.LCv('GK_MAX9')
GDP23 = c(D('D_GDP22').v * (D('D_GDP26').v / D('D_GDP22').v) ** 0.25, D('D_GDP22'), D('D_GDP26'))
K26 = c(KY23.v * GDP23.v / 1000 * (1 + GK_TR.v) ** 3, KY23, GDP23, GK_TR)
LI_CFC = LI[['cfc']].copy()
I_K26 = calib('cfc', K26, LI_CFC, EXIO + ';RQ_A04')
I_K26 = {L: (ZERO if L in NO_PROD else c(I_K26[L].v, I_K26[L], A(0, 'RQ_A04'))) for L in LAYERS}
# cropland (Mha): FAOSTAT 2024 grown at its 2014-24 trend to 2026; crop footprint from EXIOBASE
CL24, GCL_TR, GCL_MX, GY_TR, GY_MX = I.LCv('CL_LAST'), I.LCv('GCL_TREND'), I.LCv('GCL_MAX9'), I.LCv('GY_TREND'), I.LCv('GY_MAX9')
CL26 = c(CL24.v * (1 + GCL_TR.v) ** 2, CL24, GCL_TR)
I_CL26 = calib('crops_kt_per_meur', CL26, EX, EXS)
I_CL26 = {L: (ZERO if L in NO_PROD else c(I_CL26[L].v, I_CL26[L], A(0, 'RQ_A05'))) for L in LAYERS}

# labour: Agent 4 2035 intensities at trend productivity (pf) and at each sector's fastest 9-year productivity (pfmax)
SECT = ('A', 'C', 'F', 'GI', 'HJ', 'JP')
PF = {L: MM('pf', L) for L in LAYERS}
PFMAX = {L: c(sum(LI.loc[L, f'emp_share_{g}'] / (1 + D(f'D_PRODMAX_{g}').v) ** 9 for g in SECT),
              *[D(f'D_PRODMAX_{g}') for g in SECT], E(0, EXIO)) for L in LAYERS}
PFF, PFFMAX = c(1 / (1 + D('D_PROD_F').v) ** 9, D('D_PROD_F')), c(1 / (1 + D('D_PRODMAX_F').v) ** 9, D('D_PRODMAX_F'))
COG = {L: MM('cog', L) for L in LAYERS}
GWPWE = MM('gw_per_we')
HP, HO = BT('K18', 'supply_precedent_2035'), BT('K18', 'supply_on_course_2035')
ON03 = {L: (NB('K03', L, 'need_on_course_2035') or ZERO) for L in LAYERS}
ON04 = {L: (NB('K04', L, 'need_on_course_2035') or ZERO) for L in LAYERS}
E_ROB = c(I.FIG_KWH.v / I.FIG_H.v * I.HPW.v / 1000 * I.WE_HUM.v, I.FIG_KWH, I.FIG_H, I.HPW, I.WE_HUM)     # TWh per million WE
E_AI = c(GWPWE.v * 8.76 * I.PUE.v * I.LOAD.v, GWPWE, I.PUE, I.LOAD)                                # TWh per million WE
P_CU_U = c(I.P_CU.v * 1e6 / 1e9, I.P_CU)                           # bn US$ per Mt
P_LI_U = c(I.P_LCE.v * I.LCE_PER_LI.v * 1e3 / 1e9, I.P_LCE, I.LCE_PER_LI)   # bn US$ per kt Li
P_RE_U = c(I.P_NDPR.v * 1e6 / 1e9, I.P_NDPR)                       # bn US$ per kt
SIGMA = c(-I.CU_EL.v, I.CU_EL)
SIGMA_M = V(SIGMA.v, 'A', 'A4:A15;' + SIGMA.s)

INPUTS = [
    dict(id='K01', input='Electricity (generation and delivery)', unit='TWh per year (gross generation)', lever='LV3', method='fixed'),
    dict(id='K02', input='Fuels for final use (oil, gas, coal, non-electric)', unit='EJ per year (final consumption excl. electricity)', lever='LV3', method='fixed'),
    dict(id='K03', input='Machine cognition: cognitive work by AI agents', unit='million worker-equivalents in 2035', lever='LV1', method='fixed'),
    dict(id='K04', input='Physical machine labour: robots and autonomous machines', unit='million worker-equivalents in 2035', lever='LV2', method='fixed'),
    dict(id='K05', input='AI compute: accelerators in service (chip power)', unit='GW of AI chip TDP in service in 2035', lever='LV1', method='fixed'),
    dict(id='K06', input='Copper', unit='million t refined copper per year', lever='LV3', method='ces'),
    dict(id='K07', input='Lithium', unit='kt lithium content per year', lever='LV3', method='ces'),
    dict(id='K08', input='Rare earths (magnet REE: Nd, Pr, Dy, Tb)', unit='kt magnet rare earth elements per year', lever='LV3', method='ces'),
    dict(id='K09', input='Steel', unit='Mt crude steel per year', lever='LV3', method='fixed'),
    dict(id='K10', input='Cement', unit='Mt cement per year', lever='LV3', method='fixed'),
    dict(id='K11', input='Capital: saving to finance investment', unit='bn US$ (market) of gross saving per year', lever='LV6', method='fixed'),
    dict(id='K12', input='Land-use and building permission', unit='bn 2026 int$ of construction (C13) that must be permitted in 2035', lever='LV4', method='fixed'),
    dict(id='K13', input='Grid interconnection approval', unit='TWh per year of generation connected', lever='LV4', method='fixed'),
    dict(id='K14', input='Power transformers and grid equipment', unit='TWh per year of generation the delivered transformers can connect', lever='LV3', method='fixed'),
    dict(id='K15', input='Clinical evidence and approval', unit='bn 2026 int$ of health output (C05) that must be allowed in 2035', lever='LV5', method='fixed'),
    dict(id='K16', input='Professional licensing (machine work in licensed tasks)', unit='million machine worker-equivalents in 2035', lever='LV4', method='fixed'),
    dict(id='K17', input='Skilled construction and electrical trades (human)', unit='million construction workers (ISIC F) in 2035', lever='LV2', method='fixed'),
    dict(id='K18', input='Human workforce (all sectors)', unit='million workers in 2035', lever='LV2', method='fixed'),
    dict(id='K19', input='Output needing a permission current rules do not give (licence or discretionary approval)', unit='bn 2026 int$ of output in 2035', lever='LV4', method='info'),
    dict(id='K20', input='Aluminium', unit='Mt aluminium per year', lever='LV3', method='fixed'),
    dict(id='K21', input='Capital stock (fixed assets in use)', unit='tn 2026 int$ (PPP) of capital stock in 2035', lever='LV6', method='fixed'),
    dict(id='K22', input='Cropland', unit='million ha of cropland in 2035', lever='LV4', method='fixed'),
]
INFO = {k['id']: k for k in INPUTS}


def ratios(cfg):
    """Per input and layer: (ratio_best, basis, kind, ratio_alt, alt basis) for the inputs with a fixed per-output ratio.
    Labour-type inputs (K03, K04, K05, K16, K18) depend on the mix and are built in labour()."""
    steps = cfg.get('ratios') == 'steps'
    trend = cfg.get('labour') == 'trend'
    R = {}

    def put(k, L, best, alt, basis, kind, altb):
        R.setdefault(k, {})[L] = dict(best=(alt if steps and k in ('K01', 'K02', 'K06', 'K07', 'K08', 'K09', 'K10', 'K20') else best),
                                      alt=alt, basis=basis if not (steps and k in ('K01', 'K02', 'K06', 'K07', 'K08', 'K09', 'K10', 'K20')) else altb + ' [E2 test]',
                                      kind=kind, altb=altb)
    for L in LAYERS:
        z = L in NO_PROD
        e = nb('K01', L)
        put('K01', L, c(e.v * F_E.v, e, F_E), e, 'EXIOBASE electricity footprint (A4 rerun) x IEA NZE 2035 generation per unit of GDP relative to STEPS (WEO 2025 A.3c/A.5c)',
            'best achievable 2035 (lowest final-energy path, IEA NZE)', 'IEA STEPS 2035 path (Agent 4 rerun, baseline 2035 intensity)')
        f = nb('K02', L)
        put('K02', L, c(f.v * F_F.v, f, F_F), f, 'EXIOBASE fuel footprint (A4 rerun) x IEA NZE 2035 non-electric final consumption per unit of GDP relative to STEPS (WEO 2025 A.2c)',
            'best achievable 2035 (IEA NZE)', 'IEA STEPS 2035 path (Agent 4 rerun)')
        for k, F, nm in (('K06', F_CU, 'copper'), ('K07', F_LI, 'lithium'), ('K08', F_RE, 'magnet rare earth')):
            x = nb(k, L)
            put(k, L, c(x.v * F.v, x, F), x, f'EXIOBASE {nm} footprint (A4 rerun) x IEA GCMO 2026 High Demand 2035 {nm} demand per unit of GDP relative to STEPS: the mineral path that goes with the lowest-energy (electrified) path; GCMO 2026 has no NZE case',
                'pathway-consistent 2035 (GCMO High Demand, closest to NZE); more mineral per unit than the baseline', 'IEA GCMO 2026 STEPS 2035 path (Agent 4 rerun)')
        x = nb('K09', L)
        put('K09', L, c(x.v * F_ST.v, x, F_ST), x, 'EXIOBASE steel footprint (A4 rerun) x IEA NZE 2035 steel per unit of GDP relative to STEPS (WEO 2025 A.5c, material efficiency)',
            'best achievable 2035 (IEA NZE material efficiency)', 'IEA STEPS 2035 path (Agent 4 rerun)')
        x26 = cem26(L)
        put('K10', L, c(x26.v * TAU['ce_N'].v, x26, TAU['ce_N']), nb('K10', L), 'EXIOBASE cement footprint at 2026 (A4 rerun calibration) x IEA NZE 2035 cement per unit of GDP (WEO 2025 A.5c)',
            'best achievable 2035 (IEA NZE)', 'Agent 4 rerun: 2026 intensity held outside C13; C13 at the baseline on-course tonnes per unit of value')
        put('K20', L, c(I_AL26[L].v * TAU['al_N'].v, I_AL26[L], TAU['al_N']), c(I_AL26[L].v * TAU['al_S'].v, I_AL26[L], TAU['al_S']),
            'EXIOBASE aluminium footprint (products 108-109, code/exio.py) calibrated to 2026 world aluminium (IEA) x IEA NZE 2035 aluminium per unit of GDP (WEO 2025 A.5c)',
            'best achievable 2035 (IEA NZE)', 'IEA STEPS 2035 aluminium per unit of GDP (WEO 2025 A.5b)')
        inv = L in INV_LAYERS
        k11 = c(1 / PPF(L).v, PPF(L)) if inv else ZERO
        put('K11', L, k11, k11, 'investment layers: saving needed = output at market prices (1 / layer PPP factor), Agent 4 rerun',
            'identity (investment = saving at market prices); no ratio to improve', 'same')
        one = E(1.0, CODE)
        put('K12', L, one if L == 'C13' else ZERO, one if L == 'C13' else ZERO, 'C13 output must be permitted (Agent 4 rerun unit: bn int$ of construction)', 'identity', 'same')
        put('K15', L, one if L == 'C05' else ZERO, one if L == 'C05' else ZERO, 'C05 output must be allowed under approval rules (Agent 4 rerun unit)', 'identity', 'same')
        ky_alt = ZERO if z else c(I_K26[L].v * KYMIN.v / KY23.v, I_K26[L], KYMIN, KY23)
        put('K21', L, I_K26[L], ky_alt,
            "today's world capital-output ratio (PWT 11.0, 2023) held to 2035, split by layer with EXIOBASE capital consumption; no best-achievable 2035 ratio is sourced",
            "today's ratio held (only today's is sourced)", 'lowest world capital-output ratio 1990-2023 (PWT, 2005): best demonstrated, the low case')
        yb = c(1 / (1 + GY_MX.v) ** 9, GY_MX)
        yt = c(1 / (1 + GY_TR.v) ** 9, GY_TR)
        put('K22', L, c(I_CL26[L].v * yb.v, I_CL26[L], yb), c(I_CL26[L].v * yt.v, I_CL26[L], yt),
            'EXIOBASE primary-crop footprint (code/exio.py) calibrated to FAOSTAT cropland 2026; crop output per ha at its fastest 9-year growth (FAO, 1988-97)',
            'best achievable 2035 (fastest sourced yield growth)', 'crop output per ha on its 2014-24 trend')
        icw = nb('K17', L)
        best_cw = icw if trend else c(icw.v * PFFMAX.v / PFF.v, icw, PFFMAX, PFF)
        put('K17', L, best_cw, icw, 'EXIOBASE construction-employment footprint (A4 rerun) at the fastest 9-year construction output-per-worker growth (UN AMA / ILO, 2010-19)',
            'best achievable 2035 (fastest sourced productivity)', 'construction output per worker on its 2014-24 trend (Agent 4 rerun)')
    return R


def labour(T, cfg):
    """Total work at the best achievable productivity (fastest 9-year output per worker by sector); humans supply it up to
    their fastest-precedent supply (record employment ratio, Agent 4 K18), machines the rest (split cognitive / physical
    by the layer's high-skill share, Agent 4), on top of the machine work already inside the on-course trend."""
    trend = cfg.get('labour') == 'trend'
    iw = {}
    for L in LAYERS:
        t = nb('K18', L)
        iw[L] = t if (trend or t.v == 0) else c(t.v * PFMAX[L].v / PF[L].v, t, PFMAX[L], PF[L])
    W = {L: c(T[L].v * iw[L].v, T[L], iw[L]) for L in LAYERS}
    Wt = c(sum(x.v for x in W.values()), *W.values())
    hsh = c(min(1.0, HP.v / Wt.v), HP, Wt)
    out = dict(iw=iw, W=W, Wt=Wt, hsh=hsh)
    out['K18'] = {L: c(W[L].v * hsh.v, W[L], hsh) for L in LAYERS}
    MW = {L: c(W[L].v * (1 - hsh.v), W[L], hsh) for L in LAYERS}
    out['MW'] = MW
    out['K03'] = {L: c(ON03[L].v + MW[L].v * COG[L].v, ON03[L], MW[L], COG[L]) for L in LAYERS}
    out['K04'] = {L: c(ON04[L].v + MW[L].v * (1 - COG[L].v), ON04[L], MW[L], COG[L]) for L in LAYERS}
    out['K05'] = {L: c(out['K03'][L].v * GWPWE.v, out['K03'][L], GWPWE) for L in LAYERS}
    out['K16'] = {L: c(out['K03'][L].v + out['K04'][L].v, out['K03'][L], out['K04'][L]) for L in LAYERS}
    out['EM'] = {L: c(MW[L].v * (1 - COG[L].v) * E_ROB.v + MW[L].v * COG[L].v * E_AI.v, MW[L], COG[L], E_ROB, E_AI) for L in LAYERS}
    on16 = c(sum(ON03[L].v + ON04[L].v for L in LAYERS), *ON03.values(), *ON04.values())
    out['K16_son'] = c(on16.v + sum(MW[L].v * (1 - I.lic(L).v) for L in LAYERS), on16, *MW.values(), *[I.lic(L) for L in LAYERS])
    out['K16_sprec'] = c(on16.v + sum(MW[L].v * (1 - I.LIC_REFORM.v * I.lic(L).v) for L in LAYERS), on16, *MW.values(),
                         *[I.lic(L) for L in LAYERS], I.LIC_REFORM)
    return out


def perm_share(L):
    if L in NO_PROD:
        return ZERO
    p = I.PERM_SHARE if L in ('C13', 'C03') else E(0.0, CODE)
    return c(1 - (1 - I.lic(L).v) * (1 - p.v), I.lic(L), p)


def requirements(T, cfg):
    """{input: {layer: dict(req=V, ratio=V, dep=V (input per unit of output for output-depending), alt=V, ...)}}"""
    R = ratios(cfg)
    lab = labour(T, cfg)
    Q = {}
    for k in ('K02', 'K06', 'K07', 'K08', 'K09', 'K10', 'K11', 'K12', 'K15', 'K17', 'K20', 'K21', 'K22'):
        Q[k] = {}
        for L in LAYERS:
            r = R[k][L]
            Q[k][L] = dict(req=c(T[L].v * r['best'].v, T[L], r['best']), ratio=r['best'], dep=r['best'], ratio_alt=r['alt'],
                           req_altratio=c(T[L].v * r['alt'].v, T[L], r['alt']), basis=r['basis'], kind=r['kind'], altb=r['altb'], comp='')
    # electricity: footprint + electricity of the machine labour beyond the on-course fleet
    Q['K01'] = {}
    for L in LAYERS:
        r = R['K01'][L]
        fp = c(T[L].v * r['best'].v, T[L], r['best'])
        tot = c(fp.v + lab['EM'][L].v, fp, lab['EM'][L])
        rt = c(tot.v / T[L].v, tot, T[L]) if T[L].v != 0 else ZERO
        Q['K01'][L] = dict(req=tot, ratio=rt, dep=rt, ratio_alt=r['alt'], req_altratio=c(T[L].v * r['alt'].v + lab['EM'][L].v, T[L], r['alt'], lab['EM'][L]),
                           basis=r['basis'] + '; plus machine-labour electricity: robots 2.3 kWh per 5 h (Figure F.03) x 2,128 h per worker-year, AI agents chip GW per worker-equivalent (A4) x 8,760 h x PUE 1.44 (Uptime, 20 MW+) x load 1.0 [A]',
                           kind=r['kind'] + ' + machine labour', altb=r['altb'], comp='footprint + machine labour', fp=fp, em=lab['EM'][L])
    for k in ('K13', 'K14'):
        Q[k] = {L: dict(Q['K01'][L], basis='generation the target needs (K01) must be connected' if k == 'K13' else 'generation the target needs (K01) needs transformers',
                        comp='') for L in LAYERS}
    # labour-type inputs: ratio = required / output; dependency ratio = work per unit of output (one machine WE = one worker)
    for k, basis in (('K18', 'total work at the fastest 9-year output per worker of each producing sector (EXIOBASE employment footprint x UN AMA / ILO productivity maxima, BL D_PRODMAX; column work_ratio_2035) x the share humans supply at their fastest precedent (record employment ratio, Agent 4 K18); ratio_2035 = required / output'),
                     ('K03', "on-course AI work (Agent 4) + the cognitive share (Agent 4 high-skill split) of the work humans cannot supply at their precedent"),
                     ('K04', "on-course robot fleet (Agent 4) + the physical share of the work humans cannot supply at their precedent"),
                     ('K05', 'machine cognition x chip GW per worker-equivalent (Agent 4: GB300 TDP x Epoch agent population)'),
                     ('K16', 'all machine work (K03 + K04); current rules allow it outside the licensed share of work (BLS licensed share by industry, A4v1)')):
        Q[k] = {}
        for L in LAYERS:
            req = lab[k][L]
            rt = c(req.v / T[L].v, req, T[L]) if T[L].v != 0 else ZERO
            dep = lab['iw'][L] if k != 'K05' else c(lab['iw'][L].v * GWPWE.v, lab['iw'][L], GWPWE)
            Q[k][L] = dict(req=req, ratio=rt, dep=dep, ratio_alt=None, req_altratio=None, basis=basis, work=lab['iw'][L],
                           kind=('best achievable 2035 (fastest sourced productivity)' if cfg.get('labour') != 'trend' else 'trend productivity [E3 test]'),
                           altb='', comp='')
    # permission share
    Q['K19'] = {}
    for L in LAYERS:
        p = perm_share(L)
        inc = c(T[L].v - LY(L, 'on_course_2035_ppp_bn').v, T[L], LY(L, 'on_course_2035_ppp_bn'))
        req = ZERO if L in NO_PROD else c(inc.v * p.v, inc, p)
        Q['K19'][L] = dict(req=req, ratio=p, dep=None, ratio_alt=None, req_altratio=None,
                           basis='share of output above on course in licensed work (BLS licensed share by industry, A4v1) or under discretionary building permission (0.30 for C13 and C03, A4:A04)',
                           kind='today (only today is sourced)', altb='', comp='', inc=inc)
    return Q, lab


# ================================================================== 3. supply by input
PERM_UP = A(0.50, 'A4:A05')


def supply(k, lab, T):
    """(on course 2035, fastest precedent 2035, precedent text) for input k."""
    if k == 'K12':
        # permission binds only in the discretionary share (A4:A04); outside it any amount can be built under current rules
        oc = LY('C13', 'on_course_2035_ppp_bn')
        sh = I.PERM_SHARE
        son = c((1 - sh.v) * T['C13'].v + sh.v * oc.v, sh, T['C13'], oc)
        spr = c((1 - sh.v) * T['C13'].v + sh.v * oc.v * (1 + PERM_UP.v), sh, T['C13'], oc, PERM_UP)
        return son, spr, ('Auckland 2016 upzoning (+50% consents within 5 years, A4:A05) in the 30% of construction under discretionary '
                          'permission (A4:A04); outside it current rules permit any amount (Agent 4 capped that part at capacity)')
    if k == 'K16':
        return lab['K16_son'], lab['K16_sprec'], 'machine work allowed outside licensed tasks now; fastest reform precedent halves the licensed share (A4:A11)'
    if k == 'K19':
        return None, None, 'covered by K12, K15 and K16 (supply of permissions)'
    if k == 'K20':
        return I.AL35S, c(AL26.v * I.BX_RATIO.v, AL26, I.BX_RATIO), f'world bauxite mine output {I.BX_Y}-{I.BX_Y + 9} x{I.BX_R:.2f} in 9 years (USGS via OWID), post-1950 maximum; post-1985 maximum {I.BX_Y85}-{I.BX_Y85 + 9} x{I.BX_R85:.2f} is the low case'
    if k == 'K21':
        return (c(K26.v * (1 + GK_TR.v) ** 9, K26, GK_TR), c(K26.v * (1 + GK_MX.v) ** 9, K26, GK_MX),
                f"world real capital stock at its fastest 9-year growth ({I.LCNOTE['GK_MAX9'].split('(')[-1].rstrip(')')}, PWT 11.0); on course = 2013-23 trend")
    if k == 'K22':
        return (c(CL26.v * (1 + GCL_TR.v) ** 9, CL26, GCL_TR), c(CL26.v * (1 + GCL_MX.v) ** 9, CL26, GCL_MX),
                f"world cropland at its fastest 9-year expansion ({I.LCNOTE['GCL_MAX9'].split('(')[-1].rstrip(')')}, FAOSTAT); on course = 2014-24 trend")
    return BT(k, 'supply_on_course_2035'), BT(k, 'supply_precedent_2035'), BOT.loc[k, 'fastest_precedent']


def ces_ratio(alpha, cfrac, sigma):
    if cfrac <= 0:
        return 0.0
    rho = (sigma - 1) / sigma
    return (alpha * cfrac ** rho + (1 - alpha)) ** (1 / rho)


PRICE = {'K06': P_CU_U, 'K07': P_LI_U, 'K08': P_RE_U}


def tiers(req, son, spr):
    """Signed missing, tier 1 (up to the precedent supply), tier 2 (beyond it), surplus at precedent (apart)."""
    miss = c(req.v - son.v, req, son)
    top = max(spr.v, son.v)
    eps = 1e-9 * max(1.0, abs(req.v))                               # float noise (e.g. humans capped exactly at their precedent)
    t2v = req.v - top if req.v - top > eps else 0.0                 # tier rule: zero when the precedent covers it
    t1v = min(req.v, top) - son.v if min(req.v, top) - son.v > eps else 0.0   # tier rule: zero when on course covers it
    t1 = c(t1v, req, son, spr)
    t2 = c(t2v, req, son, spr)
    sur = c(spr.v - req.v if spr.v - req.v > eps else 0.0, req, spr)          # spare at precedent, reported apart
    return miss, t1, t2, sur


def input_side(T, Q, lab, cfg):
    rows, il = [], []
    for meta in INPUTS:
        k = meta['id']
        req = c(sum(Q[k][L]['req'].v for L in LAYERS), *[Q[k][L]['req'] for L in LAYERS])
        son, spr, prec = supply(k, lab, T)
        row = dict(meta=meta, req=req, son=son, spr=spr, prec=prec)
        lay = {}
        if son is None:
            row.update(miss=None, t1=None, t2=None, sur=None, dep=None, dep2=None, status='information (supply in K12, K15, K16)')
            for L in LAYERS:
                lay[L] = dict(req=Q[k][L]['req'], miss=None, dep=None, dep2=None, t2=None)
            row['lay'] = lay
            rows.append(row)
            continue
        miss, t1, t2, sur = tiers(req, son, spr)
        cov_on = son.v / req.v if req.v > 0 else 1.0
        cov_pr = max(son.v, spr.v) / req.v if req.v > 0 else 1.0
        ces = meta['method'] == 'ces' and cfg.get('minerals') != 'fixed'
        dsum, d2sum = [], []
        for L in LAYERS:
            q = Q[k][L]
            sh = q['req'].v / req.v if req.v else 0.0
            m_l = c(miss.v * sh, miss, q['req'], req)
            t2_l = c(t2.v * sh, t2, q['req'], req)
            if ces:
                sig = SIGMA if k == 'K06' else SIGMA_M
                alpha = c(q['ratio'].v * PPF(L).v * PRICE[k].v, q['ratio'], PPF(L), PRICE[k])
                dep = c(T[L].v * (1 - ces_ratio(alpha.v, cov_on, sig.v)) if cov_on < 1 else 0.0, T[L], alpha, sig, son, req)
                dep2 = c(T[L].v * (1 - ces_ratio(alpha.v, cov_pr, sig.v)) if cov_pr < 1 else 0.0, T[L], alpha, sig, spr, req)
            else:
                d = q['dep']
                dep = c(m_l.v / d.v if (d.v > 0 and miss.v > 0) else 0.0, m_l, d)      # no output depends on a covered input
                dep2 = c(t2_l.v / d.v if (d.v > 0 and t2.v > 0) else 0.0, t2_l, d)
            dsum.append(dep); d2sum.append(dep2)
            lay[L] = dict(req=q['req'], miss=m_l, dep=dep, dep2=dep2, t2=t2_l)
        dep_t = c(sum(x.v for x in dsum), *dsum)
        dep2_t = c(sum(x.v for x in d2sum), *d2sum)
        status = 'covered on course' if miss.v <= 0 else ('tier 1 (closable at precedent)' if t2.v <= 0 else 'tier 2 (beyond precedent)')
        row.update(miss=miss, t1=t1, t2=t2, sur=sur, dep=dep_t, dep2=dep2_t, status=status, lay=lay, cov_on=cov_on, cov_pr=cov_pr)
        rows.append(row)
    return rows


# ================================================================== 4. layers: missing and tiers
# on-course shortfall claims by input and layer (R: Agent 4 rerun); the layer shortfall is their union (largest claim)
BLKL = pd.read_csv(os.path.join(R_DIR, 'agent4_bottlenecks', 'blocked_by_layer.csv')).dropna(subset=['baseline_shortfall_ppp_bn'])
SH_INPUTS = sorted(BLKL.input_id.unique())


def shortfall_split(L, sh, cfg):
    """Lead decision (2026-11-15): the part of a layer's on-course shortfall whose inputs are covered at their fastest
    precedent (R: coverage_total_at_precedent >= 1) is closable at precedent, so it belongs in tier 1; the part an input
    below 1 still claims stays in tier 2. Under the union rule that part is the largest claim of the uncovered inputs.
    cfg shortfall_rule='tier2' restores the previous rule (whole shortfall in tier 2); cfg cov_override tests the rule."""
    if cfg.get('shortfall_rule') == 'tier2':
        return E(0.0, CODE), sh, []
    unc, used = [], []
    for k in SH_INPUTS:
        row = BLKL[(BLKL.input_id == k) & (BLKL.layer_id == L)]
        if row.empty:
            continue
        cov = BT(k, 'coverage_total_at_precedent')
        if k in cfg.get('cov_override', {}):
            cov = A(cfg['cov_override'][k], CODE)
        claim = V(row.iloc[0].baseline_shortfall_ppp_bn, row.iloc[0].baseline_shortfall_ppp_bn_label, f'R:agent4_bottlenecks/blocked_by_layer.csv:{k}')
        used.append((k, claim, cov))
        if cov.v < 1:
            unc.append(claim)
    deps = [x for _, cl, cv in used for x in (cl, cv)]
    u = max((x.v for x in unc), default=0.0)                 # union of the uncovered inputs' claims (largest claim)
    u = min(u, sh.v)                                          # a claim cannot exceed the layer union (rounding in R's file)
    unc_v = c(u, sh, *deps)
    cov_v = c(sh.v - u, sh, *deps)
    return cov_v, unc_v, used


def layer_side(T, cfg):
    out = {}
    for L in LAYERS:
        ev, ub, sh, mj = LY(L, 'on_course_evidence_ppp_bn'), LY(L, 'on_course_unbacked_ppp_bn'), LY(L, 'baseline_shortfall_ppp_bn'), LY(L, 'max_joint_2035_ppp_bn')
        net = c(ev.v - sh.v, ev, sh)                                 # shortfall carried as it is (never floored)
        miss = c(T[L].v - net.v, T[L], net)                          # signed
        fu, bl = LY(L, 'further_2035_ppp_bn'), LY(L, 'blocked_union_ppp_bn')
        shc, shu, _ = shortfall_split(L, sh, cfg)
        # room for tier 1 = joint maximum + on-course shortfall covered at precedent - on course net
        #                 = unbacked + further - blocked + covered shortfall (R: joint.py identity), from its parts
        cap = c(ub.v + fu.v - bl.v + shc.v, ub, fu, bl, shc)
        t1 = c(min(miss.v, cap.v) if miss.v > 0 else 0.0, miss, cap)   # tier rule: nothing to close when on course covers the target
        t2 = c(miss.v - t1.v if miss.v > 0 else 0.0, miss, t1)
        top = c(mj.v + shc.v, mj, shc)                               # most the layer can produce at its fastest precedent
        sur = c(max(0.0, top.v - T[L].v), top, T[L])                 # beyond the target: reported apart, never summed
        cnt = c(min(net.v, T[L].v), net, T[L])                       # on course counted toward the target
        beyond = c(net.v - cnt.v, net, cnt)                          # on course beyond the target (part of surplus)
        out[L] = dict(T=T[L], ev=ev, ub=ub, sh=sh, shc=shc, shu=shu, net=net, mj=mj, top=top, miss=miss, cap=cap, t1=t1, t2=t2,
                      sur=sur, cnt=cnt, beyond=beyond)
    return out


def run(cfg=None):
    cfg = cfg or {}
    tb, sh26 = mix_base()
    v1p, v1sum = mix_v1pub()
    mixes = {'base': (v1p if cfg.get('mix') == 'v1pub' else tb), 'alt': mix_alt()}
    res = dict(cfg=cfg, mixes=mixes, share26=sh26, v1pub=v1p, v1pub_sum=v1sum, target_base_engel=tb)
    for m, T in mixes.items():
        Q, lab = requirements(T, cfg)
        res[m] = dict(T=T, Q=Q, lab=lab, inputs=input_side(T, Q, lab, cfg), layers=layer_side(T, cfg))
    return res


def summary(res, m='base'):
    """Totals of one mix (plain floats) for comparisons."""
    ly = res[m]['layers']
    s = {x: sum(ly[L][x].v for L in LAYERS) for x in ('T', 'ev', 'ub', 'sh', 'shc', 'shu', 'net', 'mj', 'top', 'miss', 't1', 't2', 'sur', 'cnt', 'beyond')}
    s['layers'] = {L: dict(t1=ly[L]['t1'].v, t2=ly[L]['t2'].v, T=ly[L]['T'].v) for L in LAYERS}
    s['inputs'] = {r['meta']['id']: dict(status=r['status'], req=r['req'].v, miss=(r['miss'].v if r['miss'] else None),
                                         t1=(r['t1'].v if r['t1'] else None), t2=(r['t2'].v if r['t2'] else None),
                                         dep=(r['dep'].v if r['dep'] else None)) for r in res[m]['inputs']}
    return s


if __name__ == '__main__':
    res = run()
    for m in ('base', 'alt'):
        s = summary(res, m)
        print(m, {k: round(v) for k, v in s.items() if isinstance(v, float)})
        for k, v in s['inputs'].items():
            print('  ', k, v['status'], {x: (round(y, 2) if isinstance(y, float) else y) for x, y in v.items() if x != 'status'})
