import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.metrics import (accuracy_score, precision_score, recall_score, f1_score,
                              roc_auc_score, confusion_matrix, roc_curve, classification_report)
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import json

np.random.seed(42)

df = pd.read_csv('/home/claude/credit_project/german_credit_real.csv')
print("Loaded real dataset:", df.shape)

# credit_risk: 1 = good, 0 = bad in this file's coding (per selva86 recoding: 1=good,0=bad? verify)
print(df['credit_risk'].value_counts())

# Target: predict BAD credit risk (event = default/bad). In this file 0 appears to be the minority (~132) -> treat 0 as "bad"
# Recode so 1 = BAD (default) for standard credit-risk modeling convention (predicting the event of interest)
df['target_bad'] = (df['credit_risk'] == 0).astype(int)

y = df['target_bad']
X = df.drop(columns=['credit_risk', 'target_bad'])

cat_cols = X.select_dtypes(include='object').columns.tolist()
num_cols = X.select_dtypes(exclude='object').columns.tolist()
print("Categorical:", cat_cols)
print("Numeric:", num_cols)

X_enc = pd.get_dummies(X, columns=cat_cols, drop_first=True)

X_train, X_test, y_train, y_test = train_test_split(X_enc, y, test_size=0.25, random_state=42, stratify=y)
print("Train/Test:", X_train.shape, X_test.shape, "Bad rate train:", y_train.mean().round(3), "Bad rate test:", y_test.mean().round(3))

scaler = StandardScaler()
X_train_s = scaler.fit_transform(X_train)
X_test_s = scaler.transform(X_test)

results = {}

# 1. Logistic Regression (baseline, interpretable)
lr = LogisticRegression(max_iter=2000, class_weight='balanced', random_state=42)
lr.fit(X_train_s, y_train)
p_lr = lr.predict(X_test_s)
proba_lr = lr.predict_proba(X_test_s)[:,1]

# 2. Random Forest
rf = RandomForestClassifier(n_estimators=300, max_depth=6, class_weight='balanced', random_state=42)
rf.fit(X_train, y_train)
p_rf = rf.predict(X_test)
proba_rf = rf.predict_proba(X_test)[:,1]

# 3. Gradient Boosting (sklearn's native GBM -- xgboost unavailable in this sandboxed, offline environment)
gb = GradientBoostingClassifier(n_estimators=200, max_depth=3, learning_rate=0.05, random_state=42)
gb.fit(X_train, y_train)
p_gb = gb.predict(X_test)
proba_gb = gb.predict_proba(X_test)[:,1]

def metrics(name, y_true, y_pred, proba):
    return {
        'model': name,
        'accuracy': round(accuracy_score(y_true, y_pred),4),
        'precision': round(precision_score(y_true, y_pred),4),
        'recall': round(recall_score(y_true, y_pred),4),
        'f1': round(f1_score(y_true, y_pred),4),
        'roc_auc': round(roc_auc_score(y_true, proba),4),
    }

all_metrics = [
    metrics('Logistic Regression', y_test, p_lr, proba_lr),
    metrics('Random Forest', y_test, p_rf, proba_rf),
    metrics('Gradient Boosting', y_test, p_gb, proba_gb),
]
for m in all_metrics:
    print(m)

with open('/home/claude/credit_project/model_metrics.json','w') as f:
    json.dump(all_metrics, f, indent=2)

# Confusion matrix for best model (by ROC-AUC)
best = max(all_metrics, key=lambda m: m['roc_auc'])
print("BEST MODEL:", best)

proba_map = {'Logistic Regression': proba_lr, 'Random Forest': proba_rf, 'Gradient Boosting': proba_gb}
pred_map = {'Logistic Regression': p_lr, 'Random Forest': p_rf, 'Gradient Boosting': p_gb}
best_proba = proba_map[best['model']]
best_pred = pred_map[best['model']]

cm = confusion_matrix(y_test, best_pred)
print("Confusion matrix (rows=actual, cols=predicted) [Good,Bad]:\n", cm)
np.save('/home/claude/credit_project/best_cm.npy', cm)

# ROC curve plot (all 3 models)
plt.figure(figsize=(6,5))
for name, proba in proba_map.items():
    fpr, tpr, _ = roc_curve(y_test, proba)
    auc = roc_auc_score(y_test, proba)
    plt.plot(fpr, tpr, label=f"{name} (AUC={auc:.3f})")
plt.plot([0,1],[0,1],'k--', alpha=0.4)
plt.xlabel('False Positive Rate'); plt.ylabel('True Positive Rate')
plt.title('ROC Curve — Credit Default Prediction (Real German Credit Data, n=479)')
plt.legend(loc='lower right', fontsize=9)
plt.tight_layout()
plt.savefig('/home/claude/credit_project/roc_curve.png', dpi=150)
plt.close()

# Feature importance (Random Forest, since it's tree-based and robust)
importances = pd.Series(rf.feature_importances_, index=X_train.columns).sort_values(ascending=False).head(12)
plt.figure(figsize=(7,5))
importances.sort_values().plot(kind='barh', color='#1B7A72')
plt.title('Top 12 Feature Importances — Random Forest')
plt.xlabel('Importance')
plt.tight_layout()
plt.savefig('/home/claude/credit_project/feature_importance.png', dpi=150)
plt.close()

importances.to_csv('/home/claude/credit_project/feature_importance.csv')
print("\nTop features:\n", importances)

# Save classification report for best model
report = classification_report(y_test, best_pred, target_names=['Good Credit','Bad Credit'])
with open('/home/claude/credit_project/classification_report.txt','w') as f:
    f.write(f"Best model: {best['model']}\n\n" + report)
print(report)

print("\nDONE — all real, computed results saved.")
