"""Sourced constants for the requirements model (each a V with label and source id). Bare ids are rows of this
folder's sources.csv (written by code/sources.py from SRC below); prefixed ids point at earlier work.
Numbers labelled S were read on the page or file itself (the IEA PDFs read in place from stage1-v2/agent4_bottlenecks/raw,
the Figure and Uptime pages, the FAOSTAT and WDI files)."""
import math, os, sys
import pandas as pd

HERE = os.path.dirname(os.path.abspath(__file__)); sys.path.insert(0, HERE)
from common import V, C, S, E, A, V2, WORK

CODE = 'code/inputs.py'

# ------------------------------------------------------------------ this folder's sources (rows of sources.csv)
WEO = 'https://iea.blob.core.windows.net/assets/0a7a40a4-5dcb-4d6e-a7ad-76a1c90ec8eb/WorldEnergyOutlook2025.pdf'
GCMO = 'https://iea.blob.core.windows.net/assets/19c60aa0-ee07-4a20-a138-33c967ac5008/GlobalCriticalMineralsOutlook2026.pdf'
SRC = [
    dict(source_id='RQ_NZE_GEN', url=WEO, title='World Energy Outlook 2025, Table A.3c World electricity sector (Net Zero Emissions by 2050)', publisher='IEA',
         date_published='2025-11-12', exact_number='48 118', unit='TWh total generation, world, 2035 (NZE)',
         where_in_source="Annex A, Table A.3c, row 'Total generation', column 2035 (p.428); read in A4:raw/iea_weo2025/weo2025.txt", primary='yes'),
    dict(source_id='RQ_NZE_TFC', url=WEO, title='World Energy Outlook 2025, Table A.2c World total final consumption (Net Zero Emissions by 2050)', publisher='IEA',
         date_published='2025-11-12', exact_number='408', unit='EJ total final consumption, world, 2035 (NZE)',
         where_in_source="Annex A, Table A.2c, row 'Total final consumption', 2035 (p.426)", primary='yes'),
    dict(source_id='RQ_NZE_ELF', url=WEO, title='World Energy Outlook 2025, Table A.2c World total final consumption (Net Zero Emissions by 2050)', publisher='IEA',
         date_published='2025-11-12', exact_number='133', unit='EJ electricity in total final consumption, world, 2035 (NZE)',
         where_in_source="Annex A, Table A.2c, row 'Electricity', 2035 (p.426)", primary='yes'),
    dict(source_id='RQ_NZE_STL', url=WEO, title='World Energy Outlook 2025, Table A.5c World economic and activity indicators (NZE)', publisher='IEA',
         date_published='2025-11-12', exact_number='1 946', unit='Mt steel production, world, 2035 (NZE)',
         where_in_source="Annex A, Table A.5c, Industrial production, row 'Steel', 2035 (p.430)", primary='yes'),
    dict(source_id='RQ_NZE_CEM', url=WEO, title='World Energy Outlook 2025, Table A.5c World economic and activity indicators (NZE)', publisher='IEA',
         date_published='2025-11-12', exact_number='3 881', unit='Mt cement production, world, 2035 (NZE)',
         where_in_source="Annex A, Table A.5c, Industrial production, row 'Cement', 2035 (p.430)", primary='yes'),
    dict(source_id='RQ_NZE_AL', url=WEO, title='World Energy Outlook 2025, Table A.5c World economic and activity indicators (NZE)', publisher='IEA',
         date_published='2025-11-12', exact_number='138', unit='Mt aluminium production, world, 2035 (NZE)',
         where_in_source="Annex A, Table A.5c, Industrial production, row 'Aluminium', 2035 (p.430)", primary='yes'),
    dict(source_id='RQ_STEPS_CEM24', url=WEO, title='World Energy Outlook 2025, Table A.5b World economic and activity indicators (Stated Policies)', publisher='IEA',
         date_published='2025-11-12', exact_number='3 950', unit='Mt cement production, world, 2024',
         where_in_source="Annex A, Table A.5b, Industrial production, row 'Cement', 2024 (p.424)", primary='yes'),
    dict(source_id='RQ_STEPS_CEM35', url=WEO, title='World Energy Outlook 2025, Table A.5b World economic and activity indicators (Stated Policies)', publisher='IEA',
         date_published='2025-11-12', exact_number='4 200', unit='Mt cement production, world, 2035 (STEPS)',
         where_in_source="Annex A, Table A.5b, Industrial production, row 'Cement', 2035 (p.424)", primary='yes'),
    dict(source_id='RQ_STEPS_AL24', url=WEO, title='World Energy Outlook 2025, Table A.5b World economic and activity indicators (Stated Policies)', publisher='IEA',
         date_published='2025-11-12', exact_number='113', unit='Mt aluminium production, world, 2024',
         where_in_source="Annex A, Table A.5b, Industrial production, row 'Aluminium', 2024 (p.424)", primary='yes'),
    dict(source_id='RQ_STEPS_AL35', url=WEO, title='World Energy Outlook 2025, Table A.5b World economic and activity indicators (Stated Policies)', publisher='IEA',
         date_published='2025-11-12', exact_number='137', unit='Mt aluminium production, world, 2035 (STEPS)',
         where_in_source="Annex A, Table A.5b, Industrial production, row 'Aluminium', 2035 (p.424)", primary='yes'),
    dict(source_id='RQ_HDS_CU30', url=GCMO, title='Global Critical Minerals Outlook 2026, Annex: Copper demand (High Demand)', publisher='IEA',
         date_published='2026-05', exact_number='31 967', unit='kt Cu refined copper demand 2030, High Demand Scenario',
         where_in_source="Annex 'Copper demand', row 'Total demand', High Demand 2030 (p.346); read in A4:raw/iea_gcmo2026/gcmo2026.txt", primary='yes'),
    dict(source_id='RQ_HDS_CU40', url=GCMO, title='Global Critical Minerals Outlook 2026, Annex: Copper demand (High Demand)', publisher='IEA',
         date_published='2026-05', exact_number='35 719', unit='kt Cu refined copper demand 2040, High Demand Scenario',
         where_in_source="Annex 'Copper demand', row 'Total demand', High Demand 2040 (p.346)", primary='yes'),
    dict(source_id='RQ_HDS_LI30', url=GCMO, title='Global Critical Minerals Outlook 2026, Annex: Lithium demand (High Demand)', publisher='IEA',
         date_published='2026-05', exact_number='540', unit='kt Li lithium demand 2030, High Demand Scenario',
         where_in_source="Annex 'Lithium demand', row 'Total demand', High Demand 2030", primary='yes'),
    dict(source_id='RQ_HDS_LI40', url=GCMO, title='Global Critical Minerals Outlook 2026, Annex: Lithium demand (High Demand)', publisher='IEA',
         date_published='2026-05', exact_number='1 227', unit='kt Li lithium demand 2040, High Demand Scenario',
         where_in_source="Annex 'Lithium demand', row 'Total demand', High Demand 2040", primary='yes'),
    dict(source_id='RQ_HDS_RE30', url=GCMO, title='Global Critical Minerals Outlook 2026, Annex: Rare earth elements demand (High Demand)', publisher='IEA',
         date_published='2026-05', exact_number='127', unit='kt magnet REE demand 2030, High Demand Scenario',
         where_in_source="Annex 'Rare earth elements demand', row 'Total demand', High Demand 2030", primary='yes'),
    dict(source_id='RQ_HDS_RE40', url=GCMO, title='Global Critical Minerals Outlook 2026, Annex: Rare earth elements demand (High Demand)', publisher='IEA',
         date_published='2026-05', exact_number='160', unit='kt magnet REE demand 2040, High Demand Scenario',
         where_in_source="Annex 'Rare earth elements demand', row 'Total demand', High Demand 2040", primary='yes'),
    dict(source_id='RQ_FIG_KWH', url='https://figure.ai/news/f-03-battery-development', title='F.03 Battery Development', publisher='Figure AI',
         date_published='2025-07-17', exact_number='2.3', unit='kWh battery energy of the Figure 03 humanoid',
         where_in_source="'2.3 kWh enables 5 hours of run time at peak performance'", primary='yes'),
    dict(source_id='RQ_FIG_H', url='https://figure.ai/news/f-03-battery-development', title='F.03 Battery Development', publisher='Figure AI',
         date_published='2025-07-17', exact_number='5', unit='hours of run time at peak performance on 2.3 kWh',
         where_in_source="'2.3 kWh enables 5 hours of run time at peak performance'", primary='yes'),
    dict(source_id='RQ_UPT_PUE20', url='https://datacenter.uptimeinstitute.com/rs/711-RIA-145/images/2025.Annual.Survey.Report.pdf?version=0',
         title='Uptime Institute Global Data Center Survey 2025', publisher='Uptime Institute', date_published='2025-07', exact_number='1.44',
         unit='average PUE of data centers of 20 MW and above, globally, 2025',
         where_in_source="section 'PUE improves beneath the surface': 'larger data centers (20 MW and above) averaged a PUE of 1.44 globally'; raw/uptime_survey2025", primary='yes'),
    dict(source_id='RQ_FAO_CL', url='https://bulks-faostat.fao.org/production/Inputs_LandUse_E_All_Data.zip', title='FAOSTAT Land Use (RL): Cropland area, World',
         publisher='FAO', date_published='2026-10-02', exact_number='1587711.48', unit='1000 ha world cropland 2024',
         where_in_source="Inputs_LandUse_E_All_Data_NOFLAG.csv, Area 'World', Item 'Cropland', Element 'Area', Y2024; raw/faostat_landuse", primary='yes'),
    dict(source_id='RQ_WDI_CROP', url='https://databankfiles.worldbank.org/public/ddpext_download/WDI_CSV.zip',
         title='WDI AG.PRD.CROP.XD Crop production index (2014-2016 = 100), World (FAO)', publisher='World Bank / FAO', date_published='2026-11-13',
         exact_number='114.438557', unit='index 2014-16 = 100, world, 2024', where_in_source='WDICSV.csv, World, AG.PRD.CROP.XD, 2024 (BL:raw/wdi_bulk, read in place)', primary='no'),
    dict(source_id='RQ_PWT', url='https://dataverse.nl/api/access/datafile/554105', title='Penn World Table 11.0 (cn, cgdpo, rnna, delta)',
         publisher='Groningen Growth and Development Centre', date_published='2025', exact_number='see work/landcap.csv',
         unit='mil. 2021 US$ capital stock and GDP at current PPPs; national-price capital index', where_in_source='pwt110.xlsx sheet Data (BL:raw/pwt110, read in place)', primary='yes'),
    dict(source_id='RQ_OWID_BAUX', url='https://ourworldindata.org/grapher/global-mine-production-minerals.csv?v=1&csvType=full&useColumnShortNames=true',
         title='Global mine production of minerals: Bauxite (USGS via Our World in Data)', publisher='USGS / Our World in Data', date_published='2026',
         exact_number='see work/bauxite.csv', unit='t bauxite mined per year', where_in_source='A4v1:raw/owid_mine_production/global-mine-production-minerals.csv, entity Bauxite', primary='no'),
    dict(source_id='RQ_A01', url='assumption', title='AI accelerator fleet: average electrical draw of a server per W of chip TDP = 1.0 (server overhead ~1.3-1.5x TDP at full load times an average load of ~0.7). Test: metered energy of a GB300 NVL72 rack over a year divided by its 72 x 1400 W chip TDP',
         publisher='requirements assumption', date_published='', exact_number='1.0', unit='W drawn per W of chip TDP', where_in_source='', primary='no'),
    dict(source_id='RQ_A02', url='assumption', title='A humanoid or robot worker-equivalent runs the hours a human worker works (2,128 h a year) at the Figure 03 peak-performance draw (2.3 kWh per 5 h). Test: metered kWh per shift of deployed humanoids (Agility, Figure, Tesla pilots)',
         publisher='requirements assumption', date_published='', exact_number='', unit='', where_in_source='', primary='no'),
    dict(source_id='RQ_A03', url='assumption', title="Share of each layer's output above on course that needs a permission current rules do not give = 1 - (1 - licensed share of work, A4v1 BLS) x (1 - discretionary building-permission share, 0.30 for C13 and C03, A4:A04). Test: legal mapping of which tasks and projects need a licence or a discretionary approval, by country",
         publisher='requirements assumption', date_published='', exact_number='0.30', unit='share', where_in_source='', primary='no'),
    dict(source_id='RQ_A04', url='assumption', title="Capital stock by layer: each layer's capital per unit of output is proportional to its EXIOBASE consumption of fixed capital per unit of output (A4:work/layer_intensity.csv cfc), scaled to the PWT world capital stock. Test: PWT/EU KLEMS capital stocks by industry mapped to the layers",
         publisher='requirements assumption', date_published='', exact_number='', unit='', where_in_source='', primary='no'),
    dict(source_id='RQ_A05', url='assumption', title="Cropland by layer: each layer's cropland per unit of output is proportional to its EXIOBASE primary-crop footprint per unit of output (code/exio.py), and crop tonnes per unit of real output change only with yields. Test: FAO food production index vs real household food consumption 2014-24",
         publisher='requirements assumption', date_published='', exact_number='', unit='', where_in_source='', primary='no'),
]

# ------------------------------------------------------------------ IEA WEO 2025 (STEPS ids from Agent 4 rerun, NZE new)
GEN24, GEN35S, GEN35N = S(31229, 'A4:S01'), S(44274, 'A4:S02'), S(48118, 'RQ_NZE_GEN')
GDP24, GDP35 = S(196346, 'A4:S03'), S(270572, 'A4:S04')
TFC24, ELF24, TFC35S, ELF35S = S(453, 'A4:S05'), S(95, 'A4:S06'), S(504, 'A4:S07'), S(130, 'A4:S08')
TFC35N, ELF35N = S(408, 'RQ_NZE_TFC'), S(133, 'RQ_NZE_ELF')
STL24, STL35S, STL35N = S(1883, 'A4:S09'), S(2090, 'A4:S10'), S(1946, 'RQ_NZE_STL')
CEM24, CEM35S, CEM35N = S(3950, 'RQ_STEPS_CEM24'), S(4200, 'RQ_STEPS_CEM35'), S(3881, 'RQ_NZE_CEM')
AL24, AL35S, AL35N = S(113, 'RQ_STEPS_AL24'), S(137, 'RQ_STEPS_AL35'), S(138, 'RQ_NZE_AL')
# GCMO 2026 (STEPS ids from Agent 4 rerun, High Demand new)
CU25, CU30S, CU40S, CU30H, CU40H = S(27775, 'A4:S20'), S(31370, 'A4:S21'), S(34984, 'A4:S22'), S(31967, 'RQ_HDS_CU30'), S(35719, 'RQ_HDS_CU40')
LI25, LI30S, LI40S, LI30H, LI40H = S(275, 'A4:S29'), S(476, 'A4:S30'), S(932, 'A4:S31'), S(540, 'RQ_HDS_LI30'), S(1227, 'RQ_HDS_LI40')
RE25, RE30S, RE40S, RE30H, RE40H = S(93, 'A4:S32'), S(116, 'A4:S33'), S(140, 'A4:S34'), S(127, 'RQ_HDS_RE30'), S(160, 'RQ_HDS_RE40')
P_CU, P_LCE, P_NDPR = S(14000, 'A4:S37'), S(20000, 'A4:S38'), S(110, 'A4:S39')
CU_EL = S(-0.4, 'A4:S47')
LCE_PER_LI = E(73.891 / (2 * 6.941), CODE)
# machine labour electricity
FIG_KWH, FIG_H = S(2.3, 'RQ_FIG_KWH'), S(5, 'RQ_FIG_H')
PUE = S(1.44, 'RQ_UPT_PUE20')
LOAD = A(1.0, 'RQ_A01')
HPW = C(50507.59 / 23.73, S(50507.59, 'A4v1:S002'), S(23.73, 'A4v1:S001'), code=CODE)       # hours worked per worker-year
WE_HUM = A(1.0, 'A4:A08;RQ_A02')
# licensing and permission (A4v1 BLS shares; A4 assumptions)
LIC = {'C01': (12.0, 'S093'), 'C02': (7.9, 'S090'), 'C03': (21.6, 'S084'), 'C04': (7.9, 'S090'), 'C05': (42.0, 'S080'),
       'C06': (18.3, 'S089'), 'C07': (6.4, 'S091'), 'C08': (8.4, 'S088'), 'C09': (42.0, 'S080'), 'C10': (8.4, 'S088'),
       'C11': (None, 'S081;S087'), 'C12': (27.0, 'S083'), 'C13': (18.4, 'S086'), 'C14': (7.9, 'S090'),
       'C15': (16.7, 'S082'), 'U1': (7.9, 'S090'), 'U2': (21.6, 'S084'), 'U3': (21.6, 'S084'), 'U4': (21.6, 'S084')}


def lic(L):
    v, ids = LIC[L]
    if v is None:
        return C((27.3 + 22.6) / 2 / 100, S(27.3, 'A4v1:S081'), S(22.6, 'A4v1:S087'), code=CODE)
    return S(v / 100, 'A4v1:' + ids)


LIC_REFORM = A(0.5, 'A4:A11')
PERM_SHARE = A(0.30, 'A4:A04;RQ_A03')


# ------------------------------------------------------------------ IEA intensity factors (Agent 4's method)
def gdp_9():
    return C((GDP35.v / GDP24.v) ** (9 / 11), GDP35, GDP24, code=CODE)


def tau24(u24, u35):
    """2035 intensity relative to 2026 from a 2024 and 2035 value: intensity change at the 2024-35 CAAGR over 9 years."""
    return C(((u35.v / u24.v) / (GDP35.v / GDP24.v)) ** (9 / 11), u35, u24, GDP35, GDP24, code=CODE)


def tau_gcmo(v25, v30, v40):
    """GCMO path: 2026 on the 2025-30 path, 2035 the geometric midpoint of 2030 and 2040, relative to IEA GDP over 9 years."""
    u26 = v25.v * (v30.v / v25.v) ** 0.2
    u35 = math.sqrt(v30.v * v40.v)
    return C((u35 / u26) / gdp_9().v, v25, v30, v40, GDP35, GDP24, code=CODE)


F24 = C(TFC24.v - ELF24.v, TFC24, ELF24, code=CODE)
F35S = C(TFC35S.v - ELF35S.v, TFC35S, ELF35S, code=CODE)
F35N = C(TFC35N.v - ELF35N.v, TFC35N, ELF35N, code=CODE)

# ------------------------------------------------------------------ land and capital series (code/landcap.py)
_lc = pd.read_csv(os.path.join(WORK, 'landcap.csv')).set_index('id')


def LCv(i):
    return V(_lc.loc[i, 'value'], _lc.loc[i, 'label'], _lc.loc[i, 'src'])


LCNOTE = {i: _lc.loc[i, 'note'] for i in _lc.index}

# ------------------------------------------------------------------ bauxite precedent (OWID / USGS, A4v1 raw, read in place)
_bx = pd.read_csv(os.path.join(V2, 'v1', 'agent4_bottlenecks', 'raw', 'owid_mine_production', 'global-mine-production-minerals.csv'))
_bx = _bx[_bx.entity == 'Bauxite'].set_index('year').mine_production


def _best(start):
    w = {y: _bx[y + 9] / _bx[y] for y in _bx.index if y >= start and y + 9 in _bx.index}
    y = max(w, key=w.get)
    return y, w[y]


BX_Y, BX_R = _best(1950)
BX_Y85, BX_R85 = _best(1985)
pd.DataFrame([dict(id='BX_MAX9_POST1950', start=BX_Y, end=BX_Y + 9, t_start=_bx[BX_Y], t_end=_bx[BX_Y + 9], ratio=BX_R),
              dict(id='BX_MAX9_POST1985', start=BX_Y85, end=BX_Y85 + 9, t_start=_bx[BX_Y85], t_end=_bx[BX_Y85 + 9], ratio=BX_R85)]
             ).to_csv(os.path.join(WORK, 'bauxite.csv'), index=False)
BX_RATIO = E(BX_R, 'RQ_OWID_BAUX;' + CODE)
BX_RATIO85 = E(BX_R85, 'RQ_OWID_BAUX;' + CODE)
