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

COURSE_STATIC = [
    'SoTinChi','SoTietMH','SoTietTuan','SiSoMax','SiSoMin','KhgTinhDTB',
    'IsMHTN','IsMonKT','IsMHKN','IsMHDacThu','IsGDTiengAnh','IsMonNgoaiNgu'
]
COURSE_HIST = ['hist_count','hist_mean','hist_std','hist_fail_rate','hist_pass_rate']
COURSE_FEATURES = COURSE_STATIC + COURSE_HIST
STUDENT_FEATURES = [
    'DTBHK','DTBTLHK','SOTCDKHK','SoTCDat','SoTCNoTL','SoMonNoHB','SoTCKDat',
    'DTBHKDK','SoTCHKDK','SoTCDatHKDK','DTBCNTLHK','SoTCCNTLHK','recency','valid'
]
REL_FEATURES = ['grade10','credits','passed','recency','same_course','target_prereq_of_event','event_prereq_of_target']
COMP_FEATURES = ['grade10','weight','passed','recency','same_course','course_related']


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 Suite22Bundle:
    flat: np.ndarray
    student_seq: np.ndarray
    student_mask: np.ndarray
    course_target: np.ndarray
    course_neighbors: np.ndarray
    course_neighbor_mask: np.ndarray
    course_relation_type: np.ndarray
    relation_events: np.ndarray
    relation_mask: np.ndarray
    relation_type: np.ndarray
    component_events: np.ndarray
    component_mask: np.ndarray
    component_type: np.ndarray
    y: np.ndarray
    faculty_idx: np.ndarray
    blocks: np.ndarray
    semester_ordinal: np.ndarray
    semester_z: np.ndarray
    student_id: np.ndarray
    course_id: np.ndarray
    row_index: np.ndarray
    faculty_names: List[str]
    committee: np.ndarray

    def subset(self, mask) -> "Suite22Bundle":
        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 Suite22Bundle(**kwargs)

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

    @property
    def current_features(self) -> np.ndarray:
        return np.concatenate([self.flat, self.course_target], axis=1).astype(np.float32)


@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)
        if x.shape[:-1] != mask.shape:
            raise ValueError(f"Mask shape mismatch: x={x.shape}, mask={mask.shape}")
        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


def _fit_piecewise_knots(current: np.ndarray, max_knots: int = 5) -> tuple[np.ndarray, np.ndarray]:
    """Training-only quantile knots for the self-contained piecewise-linear tokenizer.

    Binary / effectively constant variables receive no hinge knots. Duplicate
    quantiles are removed feature-wise; unused slots are NaN and masked out.
    """
    x = np.asarray(current, np.float32)
    f = x.shape[1]
    knots = np.full((f, int(max_knots)), np.nan, np.float32)
    mask = np.zeros((f, int(max_knots)), bool)
    q = np.linspace(.1, .9, int(max_knots))
    for j in range(f):
        v = x[:, j]
        v = v[np.isfinite(v)]
        if len(v) < 10:
            continue
        sample = v if len(v) <= 100000 else v[np.linspace(0, len(v)-1, 100000).astype(int)]
        uniq = np.unique(sample)
        if len(uniq) <= 2:
            continue
        ks = np.unique(np.quantile(sample, q).astype(np.float32))
        if len(ks) <= 1:
            continue
        ks = ks[: int(max_knots)]
        knots[j, :len(ks)] = ks
        mask[j, :len(ks)] = True
    return knots, mask


@dataclass
class Suite22Preprocessor:
    flat: DenseStandardizer
    student: MaskedStandardizer
    course: DenseStandardizer
    relation: MaskedStandardizer
    component: MaskedStandardizer
    semester_mean: float
    semester_scale: float
    current_knots: np.ndarray
    current_knot_mask: np.ndarray

    @classmethod
    def fit(cls, bundle: Suite22Bundle, mask: np.ndarray, max_knots: int = 5) -> "Suite22Preprocessor":
        m = np.asarray(mask, bool)
        # Course statistics are standardized in one coordinate frame for the target
        # course and course-neighbor tokens.
        ct = bundle.course_target[m][:, None, :]
        cn = bundle.course_neighbors[m]
        cm = bundle.course_neighbor_mask[m]
        ctm = np.ones((len(ct), 1), bool)
        allc = np.concatenate([ct, cn], axis=1)
        allm = np.concatenate([ctm, cm], axis=1)
        vals = allc[allm]
        cstd = DenseStandardizer.fit(vals if len(vals) else np.zeros((1, allc.shape[-1]), np.float32))

        flat_std = DenseStandardizer.fit(bundle.flat[m])
        student_std = MaskedStandardizer.fit(bundle.student_seq[m], bundle.student_mask[m])
        relation_std = MaskedStandardizer.fit(bundle.relation_events[m], bundle.relation_mask[m])
        component_std = MaskedStandardizer.fit(bundle.component_events[m], bundle.component_mask[m])

        sem = np.asarray(bundle.semester_ordinal[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

        cur = np.concatenate([flat_std.transform(bundle.flat[m]), cstd.transform(bundle.course_target[m])], axis=1)
        knots, knot_mask = _fit_piecewise_knots(cur, max_knots=max_knots)
        return cls(flat_std, student_std, cstd, relation_std, component_std,
                   sem_mean, sem_scale, knots, knot_mask)

    def transform(self, b: Suite22Bundle) -> Suite22Bundle:
        sem_z = ((np.asarray(b.semester_ordinal, np.float32) - self.semester_mean) / self.semester_scale).astype(np.float32)
        sem_z = np.nan_to_num(sem_z, nan=0.0, posinf=0.0, neginf=0.0)
        return Suite22Bundle(
            flat=self.flat.transform(b.flat),
            student_seq=self.student.transform(b.student_seq, b.student_mask),
            student_mask=b.student_mask,
            course_target=self.course.transform(b.course_target),
            course_neighbors=self.course.transform(b.course_neighbors),
            course_neighbor_mask=b.course_neighbor_mask,
            course_relation_type=b.course_relation_type,
            relation_events=self.relation.transform(b.relation_events, b.relation_mask),
            relation_mask=b.relation_mask,
            relation_type=b.relation_type,
            component_events=self.component.transform(b.component_events, b.component_mask),
            component_mask=b.component_mask,
            component_type=b.component_type,
            y=b.y,
            faculty_idx=b.faculty_idx,
            blocks=b.blocks,
            semester_ordinal=b.semester_ordinal,
            semester_z=sem_z,
            student_id=b.student_id,
            course_id=b.course_id,
            row_index=b.row_index,
            faculty_names=b.faculty_names,
            committee=b.committee,
        )


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_set = set(unique[-nv:])
        val[m & np.isin(blocks, list(val_set))] = True
        train[m & ~np.isin(blocks, list(val_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[Suite22Bundle, Suite22Bundle, 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)

        def pack(raw, frame, y, structured, blocks, row_index, committee):
            n = len(y)
            required = [
                "student_seq","student_mask","course_target","course_neighbors","course_neighbor_mask",
                "course_relation_type","relation_events","relation_mask","relation_type",
                "component_events","component_mask","component_type"
            ]
            miss = [k for k in required if k not in structured]
            if miss:
                raise KeyError(f"Structured cache missing keys: {miss}")
            expected_dims = {
                "student_seq": len(STUDENT_FEATURES),
                "course_target": len(COURSE_FEATURES),
                "relation_events": len(REL_FEATURES),
                "component_events": len(COMP_FEATURES),
            }
            for key, dim in expected_dims.items():
                if np.asarray(structured[key]).shape[-1] != dim:
                    raise ValueError(f"Structured schema mismatch for {key}: expected last dim {dim}, got {np.asarray(structured[key]).shape[-1]}")
            sem = semester_ordinal(frame["target_NHHK"].to_numpy())
            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),
                "course_neighbors": np.asarray(structured["course_neighbors"], np.float32),
                "course_neighbor_mask": np.asarray(structured["course_neighbor_mask"], bool),
                "course_relation_type": np.asarray(structured["course_relation_type"], np.int64),
                "relation_events": np.asarray(structured["relation_events"], np.float32),
                "relation_mask": np.asarray(structured["relation_mask"], bool),
                "relation_type": np.asarray(structured["relation_type"], np.int64),
                "component_events": np.asarray(structured["component_events"], np.float32),
                "component_mask": np.asarray(structured["component_mask"], bool),
                "component_type": np.asarray(structured["component_type"], np.int64),
                "y": np.asarray(y, np.int8),
                "faculty_idx": np.full(n, fi, np.int16),
                "blocks": np.asarray(blocks, np.float32),
                "semester_ordinal": sem,
                "semester_z": np.zeros(n, np.float32),
                "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": np.asarray(committee, 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)
        committee_hist = np.asarray(d["cf_probabilities"], float)[idx].mean(1)
        committee_target = np.asarray(d["pte"], float).mean(1)
        hist_parts.append(pack(
            raw_hist, hist_frame, np.asarray(d["ytr"], int)[idx], sc["train"],
            selected_blocks(d, idx), idx, committee_hist,
        ))
        target_parts.append(pack(
            raw_target, target_frame, np.asarray(d["yte"], int), sc["target"],
            semester_ordinal(target_frame["target_NHHK"].to_numpy()),
            np.arange(len(target_frame)), committee_target,
        ))

    def concatenate(parts) -> Suite22Bundle:
        kwargs = {k: np.concatenate([p[k] for p in parts], axis=0) for k in parts[0]}
        return Suite22Bundle(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 evidence_support_table(bundle: Suite22Bundle, fold: int, split: str) -> pd.DataFrame:
    rows = []
    sources = {
        "student_semesters": bundle.student_mask.sum(1),
        "course_neighbors": bundle.course_neighbor_mask.sum(1),
        "relation_events": bundle.relation_mask.sum(1),
        "component_events": bundle.component_mask.sum(1),
    }
    for fi, faculty in enumerate(bundle.faculty_names):
        fm = bundle.faculty_idx == fi
        if not fm.any():
            continue
        base = {
            "fold": int(fold), "split": split, "faculty": faculty,
            "rows": int(fm.sum()), "failures": int(bundle.y[fm].sum()),
            "prevalence": float(bundle.y[fm].mean()),
        }
        for name, count in sources.items():
            c = np.asarray(count[fm], int)
            base[f"{name}_zero_rate"] = float((c == 0).mean())
            base[f"{name}_mean"] = float(c.mean())
            base[f"{name}_median"] = float(np.median(c))
            base[f"{name}_p75"] = float(np.quantile(c, .75))
        rows.append(base)
    return pd.DataFrame(rows)


def current_feature_names(common_names: List[str]) -> List[str]:
    return list(common_names) + [f"course::{x}" for x in COURSE_FEATURES]
