import pandas as pd, numpy as np, pyreadr, statsmodels.api as sm
PROJ="/sessions/serene-beautiful-maxwell/mnt/Shackles of Democracy/Regression - Evidence of Decline"
WDI="/tmp/wdi/indicators"
def wb(ind,name,y0=2010,y1=2022):
    d=pd.read_csv(f"{WDI}/{ind}/data.csv").rename(columns={"Country Code":"iso3","Year":"year","Value":name})
    d=d[(d.year>=y0)&(d.year<=y1)].groupby("iso3",as_index=False)[name].mean()
    return d
cpi=pd.read_csv(f"{PROJ}/cpi_merged.csv")[["iso3","cpi","FreeScore","log_gdppc"]]
wvs=pd.read_csv(f"{PROJ}/wvs_merged.csv")[["iso3","t1"]].rename(columns={"t1":"trust"})
pol=pd.read_csv(f"{PROJ}/polar_merged.csv")[["iso3","pol24"]].rename(columns={"pol24":"polar"})
tax=wb("gc.tax.totl.gd.zs","tax"); fdi=wb("bx.klt.dinv.cd.wd","fdi_usd")
hom=wb("vc.ihr.psrc.p5","homicide",2005,2020); gini=wb("si.pov.gini","gini",2005,2020)
gdp=pd.read_csv(f"{PROJ}/panel_v3.csv"); gdp=gdp[(gdp.year>=2010)&(gdp.year<=2022)].groupby("iso3",as_index=False)["gdp_usd"].mean()

def ols(df,y,xs,label):
    d=df.dropna(subset=[y]+xs).copy()
    X=sm.add_constant(d[xs]); r=sm.OLS(d[y],X).fit(cov_type="HC1")
    print(f"\n[{label}]  N={int(r.nobs)}  R2={r.rsquared:.2f}")
    for x in xs: print(f"   {x:12s} b={r.params[x]:+.3f}  se={r.bse[x]:.3f}  p={r.pvalues[x]:.4f}")

print("=== MODEL 1: government quality ~ trust + polarization + income ===")
m1=cpi.merge(wvs,on="iso3",how="inner").merge(pol,on="iso3",how="left").merge(tax,on="iso3",how="left")
ols(m1,"cpi",["trust","polar","log_gdppc"],"1a  outcome = corruption-cleanliness (CPI)")
ols(m1,"tax",["trust","polar","log_gdppc"],"1b  outcome = fiscal capacity (tax % GDP)")

print("\n=== MODEL 2: FDI inflows ~ corruption + democracy + income ===")
m2=cpi.merge(fdi,on="iso3",how="inner").merge(gdp,on="iso3",how="inner")
m2["fdi_gdp"]=100*m2["fdi_usd"]/m2["gdp_usd"]
m2=m2[(m2.fdi_gdp>-50)&(m2.fdi_gdp<50)]  # drop financial-hub outliers
ols(m2,"fdi_gdp",["cpi","FreeScore","log_gdppc"],"2  outcome = FDI inflows (% GDP)")

print("\n=== MODEL 3: homicide ~ inequality + income ===")
m3=hom.merge(gini,on="iso3",how="inner").merge(cpi[["iso3","log_gdppc"]],on="iso3",how="left")
ols(m3,"homicide",["gini","log_gdppc"],"3  outcome = homicide rate per 100k")
