"""Stage 1 v4 model: layers, input needs per unit of output, supplies, and the output each input shortage blocks.

A shortage is computed from its own numbers only: input needed per unit of output x output, against the input's supply
(on course, or at its fastest precedent). When need exceeds supply, the output that cannot be made is taken from the layers
that get the least output per unit of that input first (the scarce input goes to its most productive uses; the
input-intensive output is the output lost). On-course output is never dropped: the on-course economy's own shortfall is
already inside tier 1 in the v3 table. See own_block for the two questions (tier 2 and tier 1).

Labour is one block, rebuilt from v3's own files: work per unit of output at the best achievable productivity
(requirements.csv work_ratio_2035), humans up to their record employment ratio (Agent 4 K18), machines for the rest,
split cognitive / physical by each layer's high-skill share (Agent 4 model_memo 'cog'), on top of the on-course robot and
AI fleets (Agent 4 need_by_layer). Who does which task is the step 3 alternative.
"""
import json
import re

import numpy as np
import pandas as pd

from common import A4, BL, RQ, num, rd, worst

LAYERS = None


def layers():
    """Step 1: the v3 requirements table by layer (target, today, on-course growth, tier 1, tier 2)."""
    L = rd(RQ / "layers_v3.csv").set_index("layer_id")
    B = rd(BL / "layers.csv").set_index("id")
    rows = []
    for j in L.index:
        today = num(B.loc[j, "today_2026_ppp_bn"])
        oc = num(L.loc[j, "on_course_counted"])
        rows.append(dict(layer_id=j, target=num(L.loc[j, "target"]), target_label=L.loc[j, "target_label"],
                         today=today, today_label=B.loc[j, "today_2026_ppp_bn_label"],
                         on_course=oc, on_course_label=L.loc[j, "on_course_counted_label"],
                         growth=oc - today, growth_label=worst(L.loc[j, "on_course_counted_label"], B.loc[j, "today_2026_ppp_bn_label"]),
                         tier1=num(L.loc[j, "tier1"]), tier1_label=L.loc[j, "tier1_label"],
                         tier2=num(L.loc[j, "tier2"]), tier2_label=L.loc[j, "tier2_label"]))
    return pd.DataFrame(rows).set_index("layer_id")


def inputs():
    """Inputs with need, supply on course and at precedent (v3 requirements table; repaired Agent 4 for 2026 supply)."""
    I = rd(RQ / "inputs_v3.csv").set_index("input_id")
    b4 = rd(A4 / "bottlenecks.csv").set_index("id")
    mk = rd(RQ / "model_constants.csv").set_index("id")
    s26 = {k: (num(b4.loc[k, "supply_2026"]), b4.loc[k, "supply_2026_label"], f"stage1-v3/repair/agent4_bottlenecks/bottlenecks.csv ({k} supply_2026)")
           for k in b4.index}
    for k, m in (("K20", "MK17"), ("K21", "MK15"), ("K22", "MK16")):
        s26[k] = (num(mk.loc[m, "value"]), mk.loc[m, "value_label"], f"stage1-v3/requirements/model_constants.csv ({m})")
    out = []
    for k in I.index:
        if k == "K19":          # information row (output needing a permission); its supply sits in K12, K15, K16
            continue
        r = I.loc[k]
        s = s26.get(k, (np.nan, "", ""))
        out.append(dict(input_id=k, input=r.input, unit=r.unit, R=num(r.required_base), R_label=r.required_base_label,
                        O=num(r.supply_on_course_2035), O_label=r.supply_on_course_2035_label,
                        P=num(r.supply_precedent_2035), P_label=r.supply_precedent_2035_label,
                        R_alt=num(r.required_base_at_alt_ratio), fastest_precedent=r.fastest_precedent,
                        method=r.output_depending_method, s2026=s[0], s2026_label=s[1], s2026_src=s[2],
                        test=(b4.loc[k, "test"] if k in b4.index else ""),
                        cov_oc_economy=(num(b4.loc[k, "coverage_on_course_economy"]) if k in b4.index else np.nan)))
    return pd.DataFrame(out).set_index("input_id")


def need_ratios():
    """Input needed per bn of output by input and layer: base (v3 ratio) and alternative (v3's second sourced ratio)."""
    r = rd(RQ / "requirements.csv")
    L = rd(RQ / "layers_v3.csv").set_index("layer_id")
    il = rd(RQ / "input_layer_v3.csv").set_index(["input_id", "layer_id"])
    r["target"] = r.layer_id.map(lambda j: num(L.loc[j, "target"]))
    r["req"] = r.required_base.map(num)
    r["req_alt"] = r.required_base_at_alt_ratio.map(lambda x: num(x, np.nan))
    r["m"] = np.where(r.target.abs() > 0, r.req / r.target, 0.0)
    r["m_alt"] = np.where(r.target.abs() > 0, r.req_alt / r.target, np.nan)
    r["work"] = r.work_ratio_2035.map(lambda x: num(x, np.nan))
    # CES inputs (Agent 4 rule for copper, lithium, rare earths, elasticity 0.4): v3's own conversion of a missing unit into
    # output, missing / output_depending, used as the effective input per bn of output
    r["m_ces"] = np.nan
    for i, x in r.iterrows():
        if (x.input_id, x.layer_id) in il.index:
            q = il.loc[(x.input_id, x.layer_id)]
            mi, od = num(q.missing), num(q.output_depending_ppp_bn)
            r.at[i, "m_ces"] = mi / od if od > 0 else np.nan
    return r


def labour_data():
    """Work per bn of output, cognitive share, on-course fleets, licensed share by layer (all from v3 / Agent 4 files)."""
    r = rd(RQ / "requirements.csv")
    k18 = r[r.input_id == "K18"].set_index("layer_id")
    memo = json.loads((A4 / "work" / "model_memo.json").read_text())
    nb = rd(A4 / "work" / "need_by_layer.csv")
    on = lambda k: {x.layer_id: num(x.need_on_course_2035) for x in nb[nb.input_id == k].itertuples()}
    # licensed share of work by layer: BLS shares via Agent 4 v1, as v3 coded them (requirements/code/inputs.py, LIC)
    src = (RQ / "code" / "inputs.py").read_text()
    block = src[src.index("LIC = {"): src.index("}", src.index("LIC = {")) + 1]
    lic = {}
    for j, v in re.findall(r"'(\w+)': \(([\d.]+|None),", block):
        lic[j] = (27.3 + 22.6) / 2 / 100 if v == "None" else float(v) / 100
    reform = float(re.search(r"LIC_REFORM = A\(([\d.]+)", src).group(1))
    mk = rd(RQ / "model_constants.csv").set_index("id")
    return dict(w={j: num(k18.loc[j, "work_ratio_2035"], 0.0) for j in k18.index},
                cog={j: memo["cog"][j]["v"] if isinstance(memo["cog"][j], dict) else memo["cog"][j] for j in memo["cog"]},
                cog_label="E", on03=on("K03"), on04=on("K04"), lic=lic, lic_reform=reform,
                gw_per_we=memo["gw_per_we"]["v"], e_rob=num(mk.loc["MK13", "value"]), e_ai=num(mk.loc["MK14", "value"]))


def labour(X, D, H, method):
    """Machine work needed to produce output X (bn by layer) with H million humans, under a task split:
    base   v3: humans spread over all tasks; the gap split cognitive (AI) / physical (robots) by the layer's high-skill share
    cogfirst  AI takes every cognitive task first; humans move to physical tasks; robots only for physical work beyond them
    humanscog humans keep the cognitive tasks; AI adds no work; robots fill the whole gap
    Returns robots, AI work, machine work in licensed tasks (current rules and after reform), machine-labour electricity."""
    W = {j: D["w"].get(j, 0.0) * max(0.0, X[j]) for j in X}
    Wt = sum(W.values())
    cog = D["cog"]
    on03, on04 = sum(D["on03"].values()), sum(D["on04"].values())
    if method == "base":
        gap = max(0.0, Wt - H)
        sh = gap / Wt if Wt > 0 else 0.0
        rob_j = {j: W[j] * sh * (1 - cog[j]) for j in W}
        ai_j = {j: W[j] * sh * cog[j] for j in W}
    elif method == "cogfirst":
        phys = {j: W[j] * (1 - cog[j]) for j in W}
        P_ = sum(phys.values())
        rg = max(0.0, P_ - H)
        rob_j = {j: phys[j] * rg / P_ if P_ > 0 else 0.0 for j in W}
        ai_j = {j: W[j] * cog[j] for j in W}
    elif method == "humanscog":
        gap = max(0.0, Wt - H)
        phys = {j: W[j] * (1 - cog[j]) for j in W}
        P_ = sum(phys.values())
        rob_j = {j: phys[j] * gap / P_ if P_ > 0 else 0.0 for j in W}
        ai_j = {j: 0.0 for j in W}
    else:
        raise ValueError(method)
    mw = {j: rob_j[j] + ai_j[j] for j in W}
    lic_on = sum(D["lic"].get(j, 0.0) * mw[j] for j in W)
    return dict(rob=on04 + sum(rob_j.values()), ai=on03 + sum(ai_j.values()), lic_on=lic_on, lic_prec=D["lic_reform"] * lic_on,
                em=sum(rob_j.values()) * D["e_rob"] + sum(ai_j.values()) * D["e_ai"], Wt=Wt, mw=sum(mw.values()))


def _bisect(need_fn, supply, X, j, cap, tol):
    lo, hi = 0.0, cap
    for _ in range(60):
        mid = (lo + hi) / 2
        Xm = dict(X); Xm[j] = X[j] - mid
        if need_fn(Xm) > supply + tol:
            lo = mid
        else:
            hi = mid
    return hi


def drop(need_fn, supply, units, base_X, tol=1e-6):
    """Drop output units (layer, tier, size) in the given order until need_fn(X) <= supply; the last unit partly.
    Returns {(layer, tier): dropped} and the excess left (units of the input) if dropping every unit is not enough."""
    X = dict(base_X)
    d = {}
    if need_fn(X) <= supply + tol:
        return d, 0.0
    for j, t, cap in units:
        if cap <= 0:
            continue
        Xall = dict(X); Xall[j] = X[j] - cap
        if need_fn(Xall) > supply + tol:
            X = Xall
            d[(j, t)] = cap
            continue
        d[(j, t)] = _bisect(need_fn, supply, X, j, cap, tol)
        return d, 0.0
    return d, max(0.0, need_fn(X) - supply)


def own_block(need_prec, need_oc, order, supply_oc, supply_prec, target_X, T2, T1):
    """Output one input blocks on its own (every other input plentiful), in two questions:
    tier 2 (beyond precedent): at the input's fastest-precedent supply with the whole target attempted, the scarce input
      goes where it yields the most output; the output lost is the most input-intensive output, layer by layer (`order`),
      tier 2 before tier 1 inside a layer. The tier-2 part is the input's block of tier-2 rows; the tier-1 part lost on
      that path (t1_competing) is tier-1 output that competes with tier 2 for the input and is reported, not counted.
    tier 1 (closable at precedent): at the input's on-course supply, with on course + tier 1 attempted and no tier 2, the
      tier-1 output it cannot carry (layers in `order`). t1_beyond_precedent: the same at the precedent supply (a conflict
      with the layer view that the row is closable).
    need_prec / need_oc differ only for labour, where the human supply is at its precedent or on course."""
    units_layer = [(j, t, (T2 if t == "2" else T1).get(j, 0.0)) for j in order for t in ("2", "1")]
    a, leftP = drop(need_prec, supply_prec, units_layer, target_X)
    X0 = {j: target_X[j] - T2.get(j, 0.0) for j in target_X}
    units_t1 = [(j, "1", T1.get(j, 0.0)) for j in order]
    b, leftO = drop(need_oc, supply_oc, units_t1, X0)
    c, leftC = drop(need_prec, supply_prec, units_t1, X0)
    keys = list(T2)
    t2 = {j: a.get((j, "2"), 0.0) for j in keys}
    t1c = {j: a.get((j, "1"), 0.0) for j in keys}
    t1 = {j: b.get((j, "1"), 0.0) for j in keys}
    t1p = {j: c.get((j, "1"), 0.0) for j in keys}
    return dict(t2=t2, t1=t1, t1_beyond_precedent=t1p, t1_competing=t1c, left_prec=leftP, left_oc=leftO, left_t1_prec=leftC,
                total=sum(t2.values()) + sum(t1.values()))
