"""Shared readers and helpers for Stage 1 v4. Every input file is read from disk; nothing is typed by hand except the
labels' meaning and plain-English names. Paths are relative to the repository root (two levels up from this file)."""
from pathlib import Path

import pandas as pd

V4 = Path(__file__).resolve().parents[1]
ROOT = V4.parent
V3 = ROOT / "stage1-v3"
V2 = ROOT / "stage1-v2"
RQ = V3 / "requirements"
A4 = V3 / "repair" / "agent4_bottlenecks"
BL = V3 / "repair" / "baseline"
A6 = V3 / "repair" / "agent6_cheap_inputs"

rd = lambda p: pd.read_csv(p, dtype=str).fillna("")


def num(x, default=float("nan")):
    try:
        return float(str(x).replace(",", ""))
    except ValueError:
        return default


def worst(*labs):
    labs = [l for l in labs if l]
    return "A" if "A" in labs else ("E" if "E" in labs else ("S" if labs else "A"))


def rel(p):
    return str(Path(p).resolve().relative_to(ROOT))


LAYER_NAME = {"C01": "Food at home", "C02": "Clothing", "C03": "Shelter and utilities", "C04": "Household equipment",
              "C05": "Health care", "C06": "Mobility", "C07": "Communication", "C08": "Recreation", "C09": "Education",
              "C10": "Food service, lodging", "C11": "Personal and financial services", "C12": "Public services",
              "C13": "Built structures", "C14": "Productive equipment", "C15": "Knowledge and software",
              "U1": "Alcohol, tobacco, narcotics", "U2": "Net purchases abroad", "U3": "Inventories", "U4": "Net exports"}

# plain-English input names and the category the brief asks for (workers by skill, machines, energy, materials, land,
# capital, permission, buyers, know-how that does not exist yet)
INPUT_NAME = {"K01": ("Electricity", "energy"), "K02": ("Fuels", "energy"), "K03": ("Machine cognition (AI agents)", "machines"),
              "K04": ("Physical machine labour (robots)", "machines"), "K05": ("AI compute", "machines"),
              "K06": ("Copper", "materials"), "K07": ("Lithium", "materials"), "K08": ("Rare earths", "materials"),
              "K09": ("Steel", "materials"), "K10": ("Cement", "materials"), "K11": ("Saving for investment", "capital"),
              "K12": ("Building permission", "permission"), "K13": ("Grid connection approval", "permission"),
              "K14": ("Transformers and grid equipment", "machines"), "K15": ("Clinical approval", "permission"),
              "K16": ("Professional licensing", "permission"), "K17": ("Construction trades", "workers by skill"),
              "K18": ("Human workforce", "workers by skill"), "K20": ("Aluminium", "materials"),
              "K21": ("Capital stock", "capital"), "K22": ("Cropland", "land")}
