import pandas as pd, numpy as np, re
from scipy import stats
import statsmodels.api as sm
from coding import code

df = pd.read_csv("/mnt/user-data/uploads/1788949022214_draghi-measures.csv")
df["class"] = df["ID"].map(code)
df["impl"] = (df["Jul-26"] == "Implemented").astype(int)
df["fp"] = df["Jul-26"].isin(["Implemented", "Partially Implemented"]).astype(int)


def clean(s, n=95):
    return re.sub(r"\s+", " ", str(s)).replace("?", "")[:n]


print("=" * 78)
print("A. WHICH K-MEASURES ARE FULLY IMPLEMENTED? (sanity check on coding)")
print("=" * 78)
for _, r in df[(df["class"] == "K") & (df["impl"] == 1)].iterrows():
    txt = r["Measure"] if str(r["Measure"]).strip() not in ("-", "") else r["Policy"]
    print(f"{r['ID']:>4} {r['Sector'][:12]:<13} {clean(txt)}")

print()
print("=" * 78)
print("B. ALTERNATIVE CODING 1 — binary 'transactable' vs 'not transactable'")
print("   (K and I merged: the object cannot be obtained by transaction)")
print("=" * 78)
df["transactable"] = (df["class"] == "C").astype(int)
for dep, lab in [("impl", "strict"), ("fp", "full+partial")]:
    ct = pd.crosstab(df["transactable"], df[dep])
    odds, p = stats.fisher_exact(ct.values)
    rate_nt = df[df["transactable"] == 0][dep].mean()
    rate_t = df[df["transactable"] == 1][dep].mean()
    print(f"  {lab}: transactable {rate_t:.3f} (n=274) vs not {rate_nt:.3f} (n=109) "
          f"| OR={odds:.2f} p={p:.5f}")

X = sm.add_constant(pd.get_dummies(df[["Sector"]], drop_first=True).astype(float))
X["transactable"] = df["transactable"].astype(float)
for dep, lab in [("impl", "strict"), ("fp", "full+partial")]:
    m = sm.Logit(df[dep], X).fit(disp=0, method="bfgs", maxiter=500)
    print(f"  {lab} with sector FE: coef={m.params['transactable']:+.3f} "
          f"OR={np.exp(m.params['transactable']):.2f} p={m.pvalues['transactable']:.4f}")

print()
print("=" * 78)
print("C. ALTERNATIVE CODING 2 — institutional folded into capacity")
print("   (both are things a legislator can enact)")
print("=" * 78)
df["cap2"] = np.where(df["class"] == "K", 0, 1)
for dep, lab in [("impl", "strict"), ("fp", "full+partial")]:
    ct = pd.crosstab(df["cap2"], df[dep])
    odds, p = stats.fisher_exact(ct.values)
    print(f"  {lab}: capacity+inst {df[df.cap2==1][dep].mean():.3f} (n=335) vs "
          f"capability {df[df.cap2==0][dep].mean():.3f} (n=48) | OR={odds:.2f} p={p:.4f}")

print()
print("=" * 78)
print("D. HOW MUCH RECODING WOULD IT TAKE? (fragility of the null)")
print("=" * 78)
# how many K measures would have to be implemented for C-vs-K to reach p<0.05?
nC, iC = 274, 50
nK = 48
for iK in range(0, 13):
    ct = np.array([[nC - iC, iC], [nK - iK, iK]])
    _, p = stats.fisher_exact(ct)
    flag = "  <-- p<0.05" if p < 0.05 else ""
    if iK <= 2 or p < 0.10:
        print(f"  if {iK:>2} of 48 capability measures were implemented "
              f"(actual: 8): rate {iK/nK:.3f} vs {iC/nC:.3f}, p={p:.4f}{flag}")

print()
print("=" * 78)
print("E. THE PARTIAL-STALL CLAIM, BY CLASS")
print("=" * 78)
o = {"Not Implemented": 0, "In Progress": 1, "Partially Implemented": 2, "Implemented": 3}
df["s0"], df["s2"] = df["Sep-25"].map(o), df["Jul-26"].map(o)
for c in ["C", "I", "K"]:
    g = df[df["class"] == c]
    reached_partial = (g["s2"] >= 2).mean()
    of_which_full = (g["s2"] == 3).sum() / max((g["s2"] >= 2).sum(), 1)
    print(f"  {c}: reached partial-or-better {reached_partial:.3f}; "
          f"of those, share fully implemented {of_which_full:.3f}; "
          f"upgraded since Sep-25 {(g['s2'] > g['s0']).mean():.3f}")

print()
print("=" * 78)
print("F. WHAT ACTUALLY SEPARATES THE HIGH AND LOW SECTORS")
print("=" * 78)
sec = df.groupby("Sector").agg(N=("ID", "size"), strict=("impl", "mean"),
                               fp=("fp", "mean"),
                               shK=("class", lambda s: (s == "K").mean()),
                               shI=("class", lambda s: (s == "I").mean()))
sec = sec.sort_values("strict", ascending=False)
sec["shKI"] = sec["shK"] + sec["shI"]
print(sec.round(3).to_string())
r, p = stats.pearsonr(sec["shKI"], sec["strict"])
rs, ps = stats.spearmanr(sec["shKI"], sec["strict"])
print(f"\n  corr(share not-transactable, sector strict rate): r={r:+.3f} p={p:.4f} | rho={rs:+.3f} p={ps:.4f}")
r, p = stats.pearsonr(sec["shI"], sec["fp"])
print(f"  corr(share institutional, sector full+partial):    r={r:+.3f} p={p:.4f}")

print()
print("=" * 78)
print("G. ENERGY vs CRITICAL RAW MATERIALS, decomposed")
print("=" * 78)
for s in ["Energy", "Critical raw materials", "Digitalisation and advanced technologies", "Defence"]:
    g = df[df["Sector"] == s]
    print(f"\n{s} (N={len(g)}, strict={g['impl'].mean():.3f})")
    print(g.groupby("class").agg(n=("ID", "size"), strict=("impl", "mean"),
                                 fp=("fp", "mean")).round(3).to_string())
