from __future__ import annotations

from dataclasses import dataclass
from typing import Dict, Iterable, List, Sequence, Tuple

import numpy as np
import pandas as pd

from .config import SuiteConfig
from .data import baseline_feature_columns


BASIC_DROP_COLUMNS = {
    "reg_first_days_from_open",
    "reg_last_days_from_open",
    "reg_span_days",
    "reg_after_deadline_count",
    "prev_semester_NHHK",
    "prev_semester_ordinal",
    "prev_ssvhk_NHHK",
    "prev_ssvhk_semester_ordinal",
    "hist_target_course_pass_rate",  # near-complement of course_fail_rate_clipped
}

REDUNDANT_DROP_COLUMNS = {
    "hist_ssvhk_semester_count",
    "hist_main_semester_count",
    "hist_course_grade10_median",
    "recent3_course_grade10_median",
    "hist_component_grade_median",
    "recent3_component_grade_median",
    "hist_same_course_pass_rate",
    "hist_warning_credit_debt_count",
    "hist_component_courses",
}

AVAILABILITY_COLUMNS = {
    "eng_has_student_history",
    "eng_has_previous_semester_history",
    "eng_has_same_course_history",
    "eng_has_course_history",
    "eng_has_component_history",
    "eng_student_seen_before",
    "eng_course_seen_before",
    "eng_repeat_course_flag",
}

STUDENT_HISTORICAL_COLUMNS = {
    "eng_student_seen_before",
    "eng_student_prior_registration_count",
    "eng_student_prior_failure_count",
    "eng_student_prior_failure_rate",
    "eng_student_prior_mean_grade",
    "eng_student_prior_semester_count",
    "eng_student_recent_registration_count",
    "eng_student_recent_failure_count",
    "eng_student_recent_failure_rate",
    "eng_student_recent_mean_grade",
    "eng_student_grade_trend",
    "eng_student_failure_rate_volatility",
    "eng_student_semesters_since_last_seen",
    "eng_student_semesters_since_last_failure",
    "eng_student_consecutive_failure_semesters",
    "eng_student_long_duration_flag",
}

COURSE_HISTORICAL_COLUMNS = {
    "eng_course_seen_before",
    "eng_course_prior_registration_count",
    "eng_course_prior_failure_count",
    "eng_course_prior_failure_rate",
    "eng_course_prior_mean_grade",
    "eng_course_prior_semester_count",
    "eng_course_recent_registration_count",
    "eng_course_recent_failure_count",
    "eng_course_recent_failure_rate",
    "eng_course_recent_mean_grade",
    "eng_course_grade_trend",
    "eng_course_failure_rate_volatility",
    "eng_course_semesters_since_last_seen",
    "eng_course_semesters_since_last_failure",
    "eng_course_consecutive_failure_semesters",
    "eng_course_smoothed_failure_rate",
    "eng_global_prior_failure_rate",
}

COMPATIBILITY_COLUMNS = {
    "eng_repeat_course_flag",
    "eng_same_course_prior_attempts",
    "eng_same_course_prior_failure_count",
    "eng_same_course_prior_failure_rate",
    "eng_same_course_last_grade",
    "eng_same_course_mean_grade",
    "eng_same_course_best_grade",
    "eng_same_course_grade_trend",
    "eng_same_course_semesters_since_last_attempt",
    "eng_ability_difficulty_gap",
    "eng_recent_ability_difficulty_gap",
    "eng_student_risk_x_course_risk",
    "eng_low_gpa_x_difficult_course",
    "eng_new_student_x_difficult_course",
    "eng_repeat_course_x_previous_failure",
}

TERM_COLUMNS = {
    "eng_target_term_number",
    "eng_is_first_main_semester",
    "eng_is_second_main_semester",
    "eng_is_special_term",
    "eng_student_program_stage",
}

TRANSFORM_COLUMNS = {
    "eng_prev_failed_credit_ratio",
    "eng_hist_failed_credits_per_semester",
    "eng_hist_passed_credits_per_semester",
    "eng_warning_per_semester",
    "eng_recent_minus_cumulative_gpa",
    "eng_workload_delta",
    "eng_prev_failed_credits_gt3",
    "eng_prev_failed_credits_gt6",
    "eng_hist_failed_credits_gt9",
    "eng_hist_failed_credits_gt18",
    "eng_prev_gpa_shortfall_below6",
    "eng_prev_gpa_shortfall_below5",
    "eng_log1p_prev_failed_credits",
    "eng_log1p_hist_failed_credits",
    "eng_log1p_student_prior_registrations",
    "eng_log1p_course_prior_registrations",
    "eng_course_status_total_completed",
    "eng_course_status_fit_completed",
}


@dataclass(frozen=True)
class ExperimentSpec:
    code: str
    slug: str
    description: str
    clean_basic: bool = False
    drop_course_status: bool = False
    reduce_redundancy: bool = False
    add_availability: bool = False
    add_student: bool = False
    add_course: bool = False
    add_compatibility: bool = False
    add_term: bool = False
    add_transformations: bool = False


EXPERIMENTS: Dict[str, ExperimentSpec] = {
    "E0": ExperimentSpec("E0", "baseline", "Original effective-number baseline."),
    "E1": ExperimentSpec("E1", "basic_clean", "Remove constants, unstable timing, absolute time, and one course-risk complement.", clean_basic=True),
    "E2": ExperimentSpec("E2", "course_status_ablation", "E1 plus remove all sparse course_status columns.", clean_basic=True, drop_course_status=True),
    "E3": ExperimentSpec("E3", "redundancy_reduction", "E2 plus conservative redundant-feature pruning.", clean_basic=True, drop_course_status=True, reduce_redundancy=True),
    "E4": ExperimentSpec("E4", "availability", "E3 plus missingness/history availability indicators.", clean_basic=True, drop_course_status=True, reduce_redundancy=True, add_availability=True),
    "E5": ExperimentSpec("E5", "student_history", "E4 plus leak-safe student historical risk.", clean_basic=True, drop_course_status=True, reduce_redundancy=True, add_availability=True, add_student=True),
    "E6": ExperimentSpec("E6", "dynamic_course_risk", "E5 plus leak-safe dynamic course risk.", clean_basic=True, drop_course_status=True, reduce_redundancy=True, add_availability=True, add_student=True, add_course=True),
    "E7": ExperimentSpec("E7", "compatibility", "E6 plus student-course history and compatibility interactions.", clean_basic=True, drop_course_status=True, reduce_redundancy=True, add_availability=True, add_student=True, add_course=True, add_compatibility=True),
    "E8": ExperimentSpec("E8", "term_context", "E7 plus academic-term and cohort context.", clean_basic=True, drop_course_status=True, reduce_redundancy=True, add_availability=True, add_student=True, add_course=True, add_compatibility=True, add_term=True),
    "E9": ExperimentSpec("E9", "full_engineered", "E8 plus exposure-normalized, threshold, log, workload, and compressed curriculum features.", clean_basic=True, drop_course_status=True, reduce_redundancy=True, add_availability=True, add_student=True, add_course=True, add_compatibility=True, add_term=True, add_transformations=True),
}


@dataclass
class PreparedMatrices:
    X_train: pd.DataFrame
    X_test: pd.DataFrame
    y_train: np.ndarray
    y_test: np.ndarray
    feature_names: List[str]
    dropped_columns: List[str]
    metadata_train: pd.DataFrame
    metadata_test: pd.DataFrame


def _numeric(frame: pd.DataFrame) -> pd.DataFrame:
    out = frame.apply(pd.to_numeric, errors="coerce")
    return out.replace([np.inf, -np.inf], np.nan)


def _series(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 _availability_features(frame: pd.DataFrame, hist: pd.DataFrame) -> pd.DataFrame:
    out = pd.DataFrame(index=frame.index)
    out["eng_has_student_history"] = (_series(frame, "hist_semester_count", 0).fillna(0) > 0).astype(float)
    out["eng_has_previous_semester_history"] = _series(frame, "prev_semester_gpa10").notna().astype(float)
    out["eng_has_same_course_history"] = (_series(frame, "hist_same_course_attempts", 0).fillna(0) > 0).astype(float)
    out["eng_has_course_history"] = (_series(frame, "hist_target_course_count", 0).fillna(0) > 0).astype(float)
    out["eng_has_component_history"] = (_series(frame, "hist_component_count", 0).fillna(0) > 0).astype(float)
    for c in ["eng_student_seen_before", "eng_course_seen_before", "eng_repeat_course_flag"]:
        out[c] = hist[c] if c in hist.columns else 0.0
    return out


def _transformation_features(
    frame: pd.DataFrame,
    hist: pd.DataFrame,
    course_status_cols: Sequence[str],
) -> pd.DataFrame:
    out = pd.DataFrame(index=frame.index)
    prev_fail = _series(frame, "prev_failed_credits", 0).fillna(0).clip(lower=0)
    prev_reg = _series(frame, "prev_registered_credits", 0).fillna(0).clip(lower=0)
    hist_fail = _series(frame, "hist_failed_credits_sum", 0).fillna(0).clip(lower=0)
    hist_pass = _series(frame, "hist_passed_credits_sum", 0).fillna(0).clip(lower=0)
    hist_sem = _series(frame, "hist_semester_count", 0).fillna(0).clip(lower=0)
    warning_count = _series(frame, "hist_warning_count", 0).fillna(0).clip(lower=0)
    prev_gpa = _series(frame, "prev_semester_gpa10")
    cum_gpa = _series(frame, "prev_cumulative_gpa10")
    workload = _series(frame, "target_semester_registered_credits")
    historical_workload = _series(frame, "hist_registered_credits_mean")

    out["eng_prev_failed_credit_ratio"] = prev_fail / prev_reg.clip(lower=1.0)
    out["eng_hist_failed_credits_per_semester"] = hist_fail / hist_sem.clip(lower=1.0)
    out["eng_hist_passed_credits_per_semester"] = hist_pass / hist_sem.clip(lower=1.0)
    out["eng_warning_per_semester"] = warning_count / hist_sem.clip(lower=1.0)
    out["eng_recent_minus_cumulative_gpa"] = prev_gpa - cum_gpa
    out["eng_workload_delta"] = workload - historical_workload
    out["eng_prev_failed_credits_gt3"] = (prev_fail > 3).astype(float)
    out["eng_prev_failed_credits_gt6"] = (prev_fail > 6).astype(float)
    out["eng_hist_failed_credits_gt9"] = (hist_fail > 9).astype(float)
    out["eng_hist_failed_credits_gt18"] = (hist_fail > 18).astype(float)
    out["eng_prev_gpa_shortfall_below6"] = (6.0 - prev_gpa).clip(lower=0)
    out["eng_prev_gpa_shortfall_below5"] = (5.0 - prev_gpa).clip(lower=0)
    out["eng_log1p_prev_failed_credits"] = np.log1p(prev_fail)
    out["eng_log1p_hist_failed_credits"] = np.log1p(hist_fail)
    out["eng_log1p_student_prior_registrations"] = np.log1p(
        _series(hist, "eng_student_prior_registration_count", 0).fillna(0).clip(lower=0)
    )
    out["eng_log1p_course_prior_registrations"] = np.log1p(
        _series(hist, "eng_course_prior_registration_count", 0).fillna(0).clip(lower=0)
    )
    if course_status_cols:
        status = _numeric(frame[list(course_status_cols)]).fillna(0.0)
        out["eng_course_status_total_completed"] = status.sum(axis=1)
        fit_cols = [c for c in course_status_cols if c.startswith("course_status_FIT")]
        out["eng_course_status_fit_completed"] = (
            status[fit_cols].sum(axis=1) if fit_cols else 0.0
        )
    else:
        out["eng_course_status_total_completed"] = 0.0
        out["eng_course_status_fit_completed"] = 0.0
    return out


def prepare_experiment_matrices(
    train_df: pd.DataFrame,
    test_df: pd.DataFrame,
    hist_train: pd.DataFrame,
    hist_test: pd.DataFrame,
    spec: ExperimentSpec,
    config: SuiteConfig,
) -> PreparedMatrices:
    base_cols = baseline_feature_columns(train_df, config)
    base_cols = [c for c in base_cols if c in test_df.columns]
    original_course_status = [c for c in base_cols if c.startswith("course_status_")]
    dropped: List[str] = []

    if spec.clean_basic:
        keep = [c for c in base_cols if c not in BASIC_DROP_COLUMNS]
        dropped.extend(c for c in base_cols if c in BASIC_DROP_COLUMNS)
        base_cols = keep
        numeric_train = _numeric(train_df[base_cols])
        constants = [
            c
            for c in base_cols
            if numeric_train[c].dropna().nunique() <= 1
        ]
        base_cols = [c for c in base_cols if c not in constants]
        dropped.extend(constants)
    if spec.drop_course_status:
        course_status = [c for c in base_cols if c.startswith("course_status_")]
        base_cols = [c for c in base_cols if c not in course_status]
        dropped.extend(course_status)
    if spec.reduce_redundancy:
        redundant = [c for c in base_cols if c in REDUNDANT_DROP_COLUMNS]
        base_cols = [c for c in base_cols if c not in redundant]
        dropped.extend(redundant)

    train_parts = [_numeric(train_df[base_cols]).reset_index(drop=True)]
    test_parts = [_numeric(test_df[base_cols]).reset_index(drop=True)]

    if spec.add_availability:
        train_parts.append(_availability_features(train_df, hist_train).reset_index(drop=True))
        test_parts.append(_availability_features(test_df, hist_test).reset_index(drop=True))
    if spec.add_student:
        cols = sorted(c for c in STUDENT_HISTORICAL_COLUMNS if c in hist_train.columns)
        train_parts.append(hist_train[cols].reset_index(drop=True))
        test_parts.append(hist_test[cols].reset_index(drop=True))
    if spec.add_course:
        cols = sorted(c for c in COURSE_HISTORICAL_COLUMNS if c in hist_train.columns)
        train_parts.append(hist_train[cols].reset_index(drop=True))
        test_parts.append(hist_test[cols].reset_index(drop=True))
    if spec.add_compatibility:
        cols = sorted(c for c in COMPATIBILITY_COLUMNS if c in hist_train.columns)
        train_parts.append(hist_train[cols].reset_index(drop=True))
        test_parts.append(hist_test[cols].reset_index(drop=True))
    if spec.add_term:
        cols = sorted(c for c in TERM_COLUMNS if c in hist_train.columns)
        train_parts.append(hist_train[cols].reset_index(drop=True))
        test_parts.append(hist_test[cols].reset_index(drop=True))
    if spec.add_transformations:
        train_parts.append(
            _transformation_features(train_df, hist_train, original_course_status).reset_index(drop=True)
        )
        test_parts.append(
            _transformation_features(test_df, hist_test, original_course_status).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")]
    X_test = X_test.reindex(columns=X_train.columns)

    means = X_train.mean(axis=0)
    X_train = X_train.fillna(means).fillna(0.0)
    X_test = X_test.fillna(means).fillna(0.0)
    X_train = X_train.replace([np.inf, -np.inf], 0.0).astype(np.float32)
    X_test = X_test.replace([np.inf, -np.inf], 0.0).astype(np.float32)

    metadata_cols = [
        c
        for c in [
            config.student_col,
            config.course_col,
            config.registration_col,
            config.semester_code_col,
            config.semester_col,
            "target_status",
        ]
        if c in train_df.columns and c in test_df.columns
    ]
    metadata_train = train_df[metadata_cols].reset_index(drop=True).copy()
    metadata_test = test_df[metadata_cols].reset_index(drop=True).copy()
    for c in hist_train.columns:
        if c.startswith("eng_"):
            metadata_train[c] = hist_train[c].to_numpy()
            metadata_test[c] = hist_test[c].to_numpy()

    return PreparedMatrices(
        X_train=X_train,
        X_test=X_test,
        y_train=pd.to_numeric(train_df[config.target_col], errors="raise").to_numpy(dtype=float),
        y_test=pd.to_numeric(test_df[config.target_col], errors="raise").to_numpy(dtype=float),
        feature_names=list(X_train.columns),
        dropped_columns=sorted(set(dropped)),
        metadata_train=metadata_train,
        metadata_test=metadata_test,
    )
