from __future__ import annotations

from pathlib import Path
from typing import Iterable, List

import numpy as np
import pandas as pd


def assign_error_group(true_failure: int | bool, warned: int | bool) -> str:
    if bool(true_failure) and bool(warned):
        return "TP"
    if bool(true_failure) and not bool(warned):
        return "FN"
    if not bool(true_failure) and bool(warned):
        return "FP"
    return "TN"


def subgroup_error_table(
    predictions: pd.DataFrame,
    subgroup_col: str,
    warning_col: str = "warn_fixed",
) -> pd.DataFrame:
    if subgroup_col not in predictions.columns:
        raise KeyError(f"Missing subgroup column: {subgroup_col}")
    required = {"true_failure", warning_col}
    missing = required - set(predictions.columns)
    if missing:
        raise KeyError(f"Missing prediction columns: {sorted(missing)}")
    frame = predictions.copy()
    frame["error_group"] = [
        assign_error_group(y, w)
        for y, w in zip(frame["true_failure"], frame[warning_col])
    ]
    grouped = (
        frame.groupby([subgroup_col, "error_group"], dropna=False)
        .size()
        .rename("count")
        .reset_index()
    )
    totals = frame.groupby(subgroup_col, dropna=False).size().rename("subgroup_total")
    grouped = grouped.merge(totals.reset_index(), on=subgroup_col, how="left")
    grouped["share_within_subgroup"] = grouped["count"] / grouped["subgroup_total"]
    return grouped


def default_error_analysis(predictions: pd.DataFrame, warning_col: str = "warn_fixed") -> pd.DataFrame:
    candidate_columns = [
        "grade_band",
        "eng_student_seen_before",
        "eng_course_seen_before",
        "eng_repeat_course_flag",
        "eng_student_long_duration_flag",
        "eng_is_first_main_semester",
        "eng_is_second_main_semester",
    ]
    tables = []
    for column in candidate_columns:
        if column not in predictions.columns:
            continue
        table = subgroup_error_table(predictions, column, warning_col)
        table.insert(0, "subgroup_feature", column)
        table = table.rename(columns={column: "subgroup_value"})
        tables.append(table)
    if not tables:
        return pd.DataFrame(
            columns=[
                "subgroup_feature",
                "subgroup_value",
                "error_group",
                "count",
                "subgroup_total",
                "share_within_subgroup",
            ]
        )
    return pd.concat(tables, ignore_index=True)


def load_prediction_directory(experiment_dir: Path) -> pd.DataFrame:
    files = sorted(Path(experiment_dir).glob("predictions_fold_*.csv"))
    if not files:
        raise FileNotFoundError(f"No predictions_fold_*.csv files in {experiment_dir}")
    frames = [pd.read_csv(path, low_memory=False) for path in files]
    return pd.concat(frames, ignore_index=True)
