from __future__ import annotations

from typing import Dict, Tuple

import numpy as np
import pandas as pd

from .config import SuiteConfig
from .experiments import ExperimentSpec


def _num(frame: pd.DataFrame, name: str, default: float = np.nan) -> pd.Series:
    if name in frame.columns:
        return pd.to_numeric(frame[name], errors="coerce")
    return pd.Series(default, index=frame.index, dtype=float)


def _safe_ratio(num: pd.Series, den: pd.Series) -> pd.Series:
    return num / den.where(den.abs() > 1e-12, np.nan)


def _history_reliability(frame: pd.DataFrame, config: SuiteConfig) -> pd.DataFrame:
    out = pd.DataFrame(index=frame.index)
    semesters = _num(frame, "hist_semester_count", 0).fillna(0).clip(lower=0)
    courses = _num(frame, "hist_course_grade10_count", 0).fillna(0).clip(lower=0)
    components = _num(frame, "hist_component_grade_count", 0).fillna(0).clip(lower=0)
    same = _num(frame, "hist_same_course_attempts", 0).fillna(0).clip(lower=0)
    target_course = _num(frame, "hist_target_course_count", 0).fillna(0).clip(lower=0)

    out["ta_history_semester_reliability"] = semesters / (semesters + config.reliability_semester_k)
    out["ta_history_course_reliability"] = courses / (courses + config.reliability_course_k)
    out["ta_history_component_reliability"] = components / (components + config.reliability_component_k)
    out["ta_same_course_reliability"] = same / (same + config.reliability_same_course_k)
    out["ta_target_course_reliability"] = target_course / (
        target_course + config.reliability_target_course_k
    )

    out["ta_hist_semesters_bin_0"] = (semesters == 0).astype(float)
    out["ta_hist_semesters_bin_1"] = (semesters == 1).astype(float)
    out["ta_hist_semesters_bin_2"] = (semesters == 2).astype(float)
    out["ta_hist_semesters_bin_3_5"] = semesters.between(3, 5).astype(float)
    out["ta_hist_semesters_bin_6plus"] = (semesters >= 6).astype(float)
    out["ta_hist_courses_bin_0_3"] = (courses <= 3).astype(float)
    out["ta_hist_courses_bin_4_8"] = courses.between(4, 8).astype(float)
    out["ta_hist_courses_bin_9_20"] = courses.between(9, 20).astype(float)
    out["ta_hist_courses_bin_21plus"] = (courses >= 21).astype(float)
    return out


SHRINKAGE_SPECS = {
    "hist_course_pass_rate": "hist_course_grade10_count",
    "hist_target_course_pass_rate": "hist_target_course_count",
    "hist_same_course_pass_rate": "hist_same_course_attempts",
    "hist_warning_rate": "hist_main_semester_count",
    "hist_component_low_rate": "hist_component_grade_count",
    "hist_component_high_rate": "hist_component_grade_count",
}


def _fit_prior(train: pd.DataFrame, rate_col: str, support_col: str) -> float:
    rate = _num(train, rate_col)
    support = _num(train, support_col, 0).fillna(0).clip(lower=0)
    valid = rate.notna() & (support > 0)
    if valid.any() and float(support[valid].sum()) > 0:
        return float((rate[valid] * support[valid]).sum() / support[valid].sum())
    if rate.notna().any():
        return float(rate.mean())
    return 0.5


def _shrunk_rates(
    frame: pd.DataFrame,
    priors: Dict[str, float],
    config: SuiteConfig,
) -> pd.DataFrame:
    out = pd.DataFrame(index=frame.index)
    alpha = float(config.shrinkage_alpha)
    for rate_col, support_col in SHRINKAGE_SPECS.items():
        rate = _num(frame, rate_col)
        support = _num(frame, support_col, 0).fillna(0).clip(lower=0)
        prior = float(priors[rate_col])
        observed = rate.fillna(prior)
        out[f"ta_shrunk_{rate_col}"] = (
            observed * support + alpha * prior
        ) / (support + alpha)
    return out


def _trend_features(frame: pd.DataFrame) -> pd.DataFrame:
    out = pd.DataFrame(index=frame.index)
    out["ta_course_grade_recent_minus_long"] = (
        _num(frame, "recent3_course_grade10_mean") - _num(frame, "hist_course_grade10_mean")
    )
    out["ta_component_grade_recent_minus_long"] = (
        _num(frame, "recent3_component_grade_mean") - _num(frame, "hist_component_grade_mean")
    )
    out["ta_semester_gpa_recent_minus_long"] = (
        _num(frame, "recent3_semester_gpa10_mean") - _num(frame, "hist_semester_gpa10_mean")
    )
    out["ta_prev_gpa_minus_cumulative"] = (
        _num(frame, "prev_semester_gpa10") - _num(frame, "prev_cumulative_gpa10")
    )
    out["ta_recent_warning_rate"] = _safe_ratio(
        _num(frame, "recent3_warning_count", 0).fillna(0),
        _num(frame, "recent3_semester_count", 0).fillna(0),
    )
    out["ta_recent_minus_long_warning_rate"] = (
        out["ta_recent_warning_rate"] - _num(frame, "hist_warning_rate")
    )
    return out


def _risk_transform_features(frame: pd.DataFrame) -> pd.DataFrame:
    out = pd.DataFrame(index=frame.index)
    prev_fail = _num(frame, "prev_failed_credits", 0).fillna(0).clip(lower=0)
    prev_reg = _num(frame, "prev_registered_credits", 0).fillna(0).clip(lower=0)
    hist_fail = _num(frame, "hist_failed_credits_sum", 0).fillna(0).clip(lower=0)
    hist_pass = _num(frame, "hist_passed_credits_sum", 0).fillna(0).clip(lower=0)
    prev_gpa = _num(frame, "prev_semester_gpa10")
    cum_gpa = _num(frame, "prev_cumulative_gpa10")
    workload = _num(frame, "target_semester_registered_credits")
    hist_workload = _num(frame, "hist_registered_credits_mean")

    out["ta_prev_failed_credit_rate"] = prev_fail / prev_reg.clip(lower=1.0)
    out["ta_hist_failed_credit_rate"] = hist_fail / (hist_fail + hist_pass).clip(lower=1.0)
    out["ta_prev_failed_credits_gt3"] = (prev_fail > 3).astype(float)
    out["ta_prev_failed_credits_gt6"] = (prev_fail > 6).astype(float)
    out["ta_hist_failed_credits_gt9"] = (hist_fail > 9).astype(float)
    out["ta_hist_failed_credits_gt18"] = (hist_fail > 18).astype(float)
    out["ta_prev_gpa_below5"] = (prev_gpa < 5).astype(float)
    out["ta_prev_gpa_below6"] = (prev_gpa < 6).astype(float)
    out["ta_prev_gpa_shortfall_from5"] = (5.0 - prev_gpa).clip(lower=0)
    out["ta_prev_gpa_shortfall_from6"] = (6.0 - prev_gpa).clip(lower=0)
    out["ta_recent_gpa_minus_cumulative"] = prev_gpa - cum_gpa
    out["ta_workload_delta"] = workload - hist_workload
    out["ta_workload_ratio"] = workload / hist_workload.clip(lower=1.0)
    warning_last = _num(frame, "hist_warning_in_last_semester", 0).fillna(0)
    warning_consecutive = _num(frame, "hist_warning_in_last2_consecutive", 0).fillna(0)
    out["ta_warning_x_low_gpa"] = warning_last * out["ta_prev_gpa_shortfall_from6"]
    out["ta_warning_x_failed_credit_rate"] = warning_last * out["ta_prev_failed_credit_rate"]
    out["ta_consecutive_warning_x_gpa_decline"] = warning_consecutive * (
        (cum_gpa - prev_gpa).clip(lower=0)
    )
    return out


def _ability_course_features(frame: pd.DataFrame) -> pd.DataFrame:
    out = pd.DataFrame(index=frame.index)
    recent_ability = _num(frame, "recent3_course_grade10_mean")
    cumulative = _num(frame, "prev_cumulative_gpa10")
    course_mean = _num(frame, "hist_target_course_mean_grade10")
    course_fail = _num(frame, "course_fail_rate_clipped")
    course_difficulty = _num(frame, "target_semester_average_course_difficulty")
    out["ta_recent_ability_minus_course_mean"] = recent_ability - course_mean
    out["ta_cumulative_gpa_minus_course_mean"] = cumulative - course_mean
    out["ta_low_ability_x_course_fail_rate"] = (6.0 - recent_ability).clip(lower=0) * course_fail
    out["ta_low_gpa_x_course_difficulty"] = (6.0 - cumulative).clip(lower=0) * course_difficulty
    out["ta_course_fail_x_target_workload"] = course_fail * _num(
        frame, "target_semester_registered_credits"
    )
    return out


def _missingness_features(frame: pd.DataFrame) -> pd.DataFrame:
    out = pd.DataFrame(index=frame.index)
    out["ta_missing_student_grade_history"] = (
        _num(frame, "hist_course_grade10_count", 0).fillna(0) <= 0
    ).astype(float)
    out["ta_missing_recent_grade_history"] = (
        _num(frame, "recent3_course_grade10_count", 0).fillna(0) <= 0
    ).astype(float)
    out["ta_missing_same_course_history"] = (
        _num(frame, "hist_same_course_attempts", 0).fillna(0) <= 0
    ).astype(float)
    out["ta_missing_target_course_history"] = (
        _num(frame, "hist_target_course_count", 0).fillna(0) <= 0
    ).astype(float)
    out["ta_missing_component_history"] = (
        _num(frame, "hist_component_grade_count", 0).fillna(0) <= 0
    ).astype(float)
    out["ta_missing_conduct_history"] = _num(frame, "prev_conduct_score").isna().astype(float)
    out["ta_missing_registration_timing"] = pd.concat(
        [
            _num(frame, "reg_first_days_from_open"),
            _num(frame, "reg_last_days_from_open"),
            _num(frame, "reg_span_days"),
            _num(frame, "reg_after_deadline_count"),
        ],
        axis=1,
    ).isna().all(axis=1).astype(float)
    out["ta_upstream_student_gpa_missing"] = _num(
        frame, "student_gpa_history_missing_flag", 0
    ).fillna(0)
    out["ta_upstream_course_history_missing"] = _num(
        frame, "course_history_missing_flag", 0
    ).fillna(0)
    return out


def build_feature_frames(
    train_base: pd.DataFrame,
    test_base: pd.DataFrame,
    spec: ExperimentSpec,
    config: SuiteConfig,
) -> Tuple[pd.DataFrame, pd.DataFrame, Dict[str, object]]:
    train = train_base.apply(pd.to_numeric, errors="coerce").replace([np.inf, -np.inf], np.nan)
    test = test_base.reindex(columns=train.columns).apply(pd.to_numeric, errors="coerce").replace(
        [np.inf, -np.inf], np.nan
    )
    baseline_columns = list(train.columns)
    train_parts = [train.reset_index(drop=True)]
    test_parts = [test.reset_index(drop=True)]
    learned: Dict[str, float] = {}

    if spec.add_history_reliability:
        train_parts.append(_history_reliability(train, config).reset_index(drop=True))
        test_parts.append(_history_reliability(test, config).reset_index(drop=True))
    if spec.add_shrinkage:
        priors = {
            rate_col: _fit_prior(train, rate_col, support_col)
            for rate_col, support_col in SHRINKAGE_SPECS.items()
        }
        learned.update({f"{k}__prior": float(v) for k, v in priors.items()})
        train_parts.append(_shrunk_rates(train, priors, config).reset_index(drop=True))
        test_parts.append(_shrunk_rates(test, priors, config).reset_index(drop=True))
    if spec.add_trends:
        train_parts.append(_trend_features(train).reset_index(drop=True))
        test_parts.append(_trend_features(test).reset_index(drop=True))
    if spec.add_risk_transforms:
        train_parts.append(_risk_transform_features(train).reset_index(drop=True))
        test_parts.append(_risk_transform_features(test).reset_index(drop=True))
    if spec.add_ability_course:
        train_parts.append(_ability_course_features(train).reset_index(drop=True))
        test_parts.append(_ability_course_features(test).reset_index(drop=True))
    if spec.add_missingness:
        train_parts.append(_missingness_features(train).reset_index(drop=True))
        test_parts.append(_missingness_features(test).reset_index(drop=True))

    X_train = pd.concat(train_parts, axis=1)
    X_test = pd.concat(test_parts, axis=1)
    X_train = X_train.loc[:, ~X_train.columns.duplicated(keep="first")]
    X_test = X_test.loc[:, ~X_test.columns.duplicated(keep="first")].reindex(
        columns=X_train.columns
    )
    manifest = {
        "baseline_feature_count": len(baseline_columns),
        "baseline_features": baseline_columns,
        "added_features": [c for c in X_train.columns if c not in baseline_columns],
        "learned_parameters": learned,
    }
    return X_train, X_test, manifest
