from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path
from typing import Dict, List, Tuple

import joblib
import numpy as np
import pandas as pd

EPS = 1e-6


def discover_faculties(cache_dir: str | Path, fold: int = 1) -> List[str]:
    root = Path(cache_dir)
    suffix = f"_fold_{int(fold)}.joblib"
    return [p.name[:-len(suffix)] for p in sorted(root.glob(f"*{suffix}"))]


def parse_faculties(value: str, available: List[str]) -> List[str]:
    if not value or str(value).lower() in {"all", "auto"}:
        return list(available)
    requested = [x.strip() for x in str(value).split(",") if x.strip()]
    missing = [x for x in requested if x not in available]
    if missing:
        raise ValueError(f"Unknown faculties: {missing}")
    return requested


def semester_ordinal(values) -> np.ndarray:
    out = []
    for v in values:
        try:
            x = int(float(v))
            year, term = x // 10, x % 10
            out.append(float(year * 3 + term - 1) if term in (1, 2, 3) else np.nan)
        except Exception:
            out.append(np.nan)
    return np.asarray(out, np.float32)


def common_raw_features(caches: Dict[str, dict], faculties: List[str]) -> List[str]:
    if not faculties:
        raise ValueError("No faculties")
    first = list(caches[faculties[0]]["feature_names"])
    common = set(first)
    for f in faculties[1:]:
        common &= set(caches[f]["feature_names"])
    result = [n for n in first if n in common]
    if not result:
        raise RuntimeError("No common raw features across faculties")
    return result


def align_raw_features(X, feature_names, common_names) -> np.ndarray:
    idx = {n: i for i, n in enumerate(feature_names)}
    missing = [n for n in common_names if n not in idx]
    if missing:
        raise KeyError(f"Missing common features: {missing[:10]}")
    return np.asarray(X, np.float32)[:, [idx[n] for n in common_names]]


def selected_history_indices(cache_obj: dict) -> np.ndarray:
    covered = np.asarray(cache_obj["cf_covered_mask"], bool)
    probs = np.asarray(cache_obj["cf_probabilities"], float)
    if probs.ndim != 2 or probs.shape[1] < 2:
        raise ValueError("cf_probabilities must be [N,K] with K>=2")
    valid = covered & np.isfinite(probs).all(1)
    return np.where(valid)[0]


def selected_blocks(cache_obj: dict, idx: np.ndarray) -> np.ndarray:
    key = "cf_block_ids" if "cf_block_ids" in cache_obj else "train_groups"
    raw = np.asarray(cache_obj.get(key, np.full(len(cache_obj["ytr"]), np.nan)), dtype=object)
    if len(raw) != len(cache_obj["ytr"]):
        raise ValueError(f"{key} length mismatch")
    block = pd.to_numeric(pd.Series(raw[idx]), errors="coerce").to_numpy(float)
    if not np.isfinite(block).all():
        raise ValueError(f"History rows contain non-numeric temporal block ids; source={key}")
    return block.astype(np.float32)


@dataclass
class Suite21Bundle:
    flat: np.ndarray
    student_seq: np.ndarray
    student_mask: np.ndarray
    course_target: np.ndarray
    y: np.ndarray
    faculty_idx: np.ndarray
    blocks: np.ndarray
    target_semester: np.ndarray
    student_id: np.ndarray
    course_id: np.ndarray
    row_index: np.ndarray
    faculty_names: List[str]
    committee_probs: np.ndarray

    @property
    def committee(self) -> np.ndarray:
        return np.asarray(self.committee_probs, np.float32).mean(1)

    def subset(self, mask) -> "Suite21Bundle":
        m = np.asarray(mask, bool)
        kwargs = {}
        for key, value in self.__dict__.items():
            if isinstance(value, np.ndarray) and len(value) == len(m):
                kwargs[key] = value[m]
            else:
                kwargs[key] = value
        return Suite21Bundle(**kwargs)

    def __len__(self) -> int:
        return int(len(self.y))


@dataclass
class DenseStandardizer:
    mean_: np.ndarray
    scale_: np.ndarray

    @classmethod
    def fit(cls, x: np.ndarray) -> "DenseStandardizer":
        z = np.nan_to_num(np.asarray(x, np.float32), nan=0.0, posinf=0.0, neginf=0.0)
        mean = z.mean(0)
        scale = z.std(0)
        scale = np.where(scale < EPS, 1.0, scale)
        return cls(mean.astype(np.float32), scale.astype(np.float32))

    def transform(self, x: np.ndarray) -> np.ndarray:
        z = np.nan_to_num(np.asarray(x, np.float32), nan=0.0, posinf=0.0, neginf=0.0)
        return ((z - self.mean_) / self.scale_).astype(np.float32)


@dataclass
class MaskedStandardizer:
    mean_: np.ndarray
    scale_: np.ndarray

    @classmethod
    def fit(cls, x: np.ndarray, mask: np.ndarray) -> "MaskedStandardizer":
        x = np.asarray(x, np.float32)
        mask = np.asarray(mask, bool)
        values = np.nan_to_num(x[mask], nan=0.0, posinf=0.0, neginf=0.0)
        if len(values) == 0:
            return cls(np.zeros(x.shape[-1], np.float32), np.ones(x.shape[-1], np.float32))
        mean = values.mean(0)
        scale = values.std(0)
        scale = np.where(scale < EPS, 1.0, scale)
        return cls(mean.astype(np.float32), scale.astype(np.float32))

    def transform(self, x: np.ndarray, mask: np.ndarray) -> np.ndarray:
        z = np.nan_to_num(np.asarray(x, np.float32), nan=0.0, posinf=0.0, neginf=0.0)
        out = ((z - self.mean_) / self.scale_).astype(np.float32)
        out[~np.asarray(mask, bool)] = 0.0
        return out


@dataclass
class Suite21Standardizers:
    flat: DenseStandardizer
    student: MaskedStandardizer
    course: DenseStandardizer
    semester_mean: float
    semester_scale: float

    @classmethod
    def fit(cls, bundle: Suite21Bundle, mask: np.ndarray) -> "Suite21Standardizers":
        m = np.asarray(mask, bool)
        sem = np.asarray(bundle.target_semester[m], np.float32)
        finite = np.isfinite(sem)
        sem_mean = float(np.mean(sem[finite])) if finite.any() else 0.0
        sem_scale = float(np.std(sem[finite])) if finite.any() else 1.0
        if sem_scale < EPS:
            sem_scale = 1.0
        return cls(
            flat=DenseStandardizer.fit(bundle.flat[m]),
            student=MaskedStandardizer.fit(bundle.student_seq[m], bundle.student_mask[m]),
            course=DenseStandardizer.fit(bundle.course_target[m]),
            semester_mean=sem_mean,
            semester_scale=sem_scale,
        )

    def transform(self, bundle: Suite21Bundle) -> Suite21Bundle:
        sem = ((np.asarray(bundle.target_semester, np.float32) - self.semester_mean) / self.semester_scale).astype(np.float32)
        return Suite21Bundle(
            flat=self.flat.transform(bundle.flat),
            student_seq=self.student.transform(bundle.student_seq, bundle.student_mask),
            student_mask=bundle.student_mask,
            course_target=self.course.transform(bundle.course_target),
            y=bundle.y,
            faculty_idx=bundle.faculty_idx,
            blocks=bundle.blocks,
            target_semester=sem,
            student_id=bundle.student_id,
            course_id=bundle.course_id,
            row_index=bundle.row_index,
            faculty_names=bundle.faculty_names,
            committee_probs=bundle.committee_probs,
        )


def latest_block_split(faculty_idx: np.ndarray, blocks: np.ndarray, val_blocks: int = 3) -> Tuple[np.ndarray, np.ndarray]:
    faculty_idx = np.asarray(faculty_idx, int)
    blocks = np.asarray(blocks, float)
    train = np.zeros(len(blocks), bool)
    val = np.zeros(len(blocks), bool)
    for fi in np.unique(faculty_idx):
        m = (faculty_idx == fi) & np.isfinite(blocks)
        unique = sorted(pd.unique(blocks[m]))
        if not unique:
            continue
        nv = min(int(val_blocks), max(1, len(unique) - 1)) if len(unique) > 1 else 0
        if nv == 0:
            train[m] = True
            continue
        val_blocks_set = set(unique[-nv:])
        val[m & np.isin(blocks, list(val_blocks_set))] = True
        train[m & ~np.isin(blocks, list(val_blocks_set))] = True
    return train, val


def load_fold_bundle(
    cache_dir: str | Path,
    structured_dir: str | Path,
    faculties: List[str],
    fold: int,
    common_names: List[str] | None = None,
) -> Tuple[Suite21Bundle, Suite21Bundle, List[str]]:
    cache_dir = Path(cache_dir)
    structured_dir = Path(structured_dir)
    caches = {f: joblib.load(cache_dir / f"{f}_fold_{int(fold)}.joblib") for f in faculties}
    if common_names is None:
        common_names = common_raw_features(caches, faculties)

    hist_parts, target_parts = [], []
    for fi, faculty in enumerate(faculties):
        d = caches[faculty]
        sp = structured_dir / f"{faculty}_fold_{int(fold)}.joblib"
        if not sp.exists():
            raise FileNotFoundError(f"Missing structured cache: {sp}")
        sc = joblib.load(sp)
        idx = np.asarray(sc["train_row_index"], int)
        expected = selected_history_indices(d)
        if not np.array_equal(idx, expected):
            raise ValueError(f"Structured/canonical history row mismatch for {faculty} F{fold}")

        hist_frame = d["train_frame"].iloc[idx].reset_index(drop=True)
        target_frame = d["test_frame"].reset_index(drop=True)
        hist_probs = np.asarray(d["cf_probabilities"], np.float32)[idx]
        target_probs = np.asarray(d["pte"], np.float32)
        if hist_probs.ndim != 2 or target_probs.ndim != 2 or hist_probs.shape[1] != target_probs.shape[1]:
            raise ValueError(f"Committee component mismatch for {faculty} F{fold}")

        def pack(raw, frame, y, structured, blocks, row_index, committee_probs):
            semester = pd.to_numeric(frame["target_NHHK"], errors="coerce").to_numpy(np.float32)
            return {
                "flat": np.nan_to_num(raw, nan=0.0, posinf=0.0, neginf=0.0).astype(np.float32),
                "student_seq": np.asarray(structured["student_seq"], np.float32),
                "student_mask": np.asarray(structured["student_mask"], bool),
                "course_target": np.asarray(structured["course_target"], np.float32),
                "y": np.asarray(y, np.int8),
                "faculty_idx": np.full(len(y), fi, np.int16),
                "blocks": np.asarray(blocks, np.float32),
                "target_semester": semester,
                "student_id": frame["IDSV"].astype(str).to_numpy(),
                "course_id": frame["IDMH"].astype(str).to_numpy(),
                "row_index": np.asarray(row_index, np.int32),
                "committee_probs": np.asarray(committee_probs, np.float32),
            }

        raw_hist = align_raw_features(d["Xtr"][idx], d["feature_names"], common_names)
        raw_target = align_raw_features(d["Xte"], d["feature_names"], common_names)
        hist_parts.append(pack(
            raw_hist, hist_frame, np.asarray(d["ytr"], int)[idx], sc["train"],
            selected_blocks(d, idx), idx, hist_probs,
        ))
        target_blocks = semester_ordinal(target_frame["target_NHHK"].to_numpy())
        target_parts.append(pack(
            raw_target, target_frame, np.asarray(d["yte"], int), sc["target"],
            target_blocks, np.arange(len(target_frame)), target_probs,
        ))

    def concatenate(parts) -> Suite21Bundle:
        kwargs = {k: np.concatenate([p[k] for p in parts], axis=0) for k in parts[0]}
        return Suite21Bundle(faculty_names=list(faculties), **kwargs)

    return concatenate(hist_parts), concatenate(target_parts), list(common_names)


def faculty_sampling_weights(faculty_idx, faculty_names):
    faculty_idx = np.asarray(faculty_idx, int)
    weights = np.zeros(len(faculty_idx), float)
    diagnostics = {}
    for fi, name in enumerate(faculty_names):
        m = faculty_idx == fi
        n = int(m.sum())
        if n == 0:
            diagnostics[name] = {"rows": 0}
            continue
        weights[m] = 1.0 / n
        diagnostics[name] = {"rows": n}
    weights *= len(weights) / max(weights.sum(), EPS)
    return weights.astype(np.float64), diagnostics


def history_coverage_table(bundle: Suite21Bundle, fold: int, split: str) -> pd.DataFrame:
    rows = []
    lengths = np.asarray(bundle.student_mask.sum(1), int)
    for fi, faculty in enumerate(bundle.faculty_names):
        m = bundle.faculty_idx == fi
        if not m.any():
            continue
        l = lengths[m]
        yy = bundle.y[m]
        rows.append({
            "fold": int(fold), "split": split, "faculty": faculty,
            "rows": int(m.sum()), "failures": int(yy.sum()), "prevalence": float(yy.mean()),
            "history_zero_rate": float((l == 0).mean()),
            "history_mean_semesters": float(l.mean()),
            "history_median_semesters": float(np.median(l)),
            "history_p25_semesters": float(np.quantile(l, .25)),
            "history_p75_semesters": float(np.quantile(l, .75)),
            "history_max_semesters": int(l.max()) if len(l) else 0,
        })
    return pd.DataFrame(rows)
