"""Read-only adapter for the inspected AQ Suite32 cache contract."""
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
import hashlib
import json
import re
import joblib
import numpy as np
import pandas as pd

COURSE_FEATURES = [
    "SoTinChi", "SoTietMH", "SoTietTuan", "SiSoMax", "SiSoMin", "KhgTinhDTB",
    "IsMHTN", "IsMonKT", "IsMHKN", "IsMHDacThu", "IsGDTiengAnh", "IsMonNgoaiNgu",
    "hist_count", "hist_mean", "hist_std", "hist_fail_rate", "hist_pass_rate",
]


def json_default(v):
    if isinstance(v, np.ndarray):
        return v.tolist()
    if isinstance(v, np.generic):
        return v.item()
    if isinstance(v, Path):
        return str(v)
    raise TypeError(type(v).__name__)


def json_hash(v):
    return hashlib.sha256(json.dumps(v, sort_keys=True, default=json_default,
                                    separators=(",", ":")).encode()).hexdigest()


def sha256_file(path):
    h = hashlib.sha256()
    with open(path, "rb") as f:
        for b in iter(lambda: f.read(2**20), b""):
            h.update(b)
    return h.hexdigest()


def semester_ordinal(v):
    try:
        value = float(v)
        z = int(value)
        if not np.isfinite(value) or value != z or z % 10 not in (1, 2, 3):
            raise ValueError()
        return (z // 10) * 3 + z % 10 - 1
    except (ValueError, TypeError, OverflowError) as e:
        raise ValueError(f"Invalid target_NHHK semester {v!r}") from e


@dataclass
class Part:
    X: np.ndarray
    names: list
    y: np.ndarray
    frame: pd.DataFrame
    row_index: np.ndarray
    reference: np.ndarray

    def __len__(self):
        return len(self.y)

    @property
    def semester(self):
        return self.frame["_ordinal"].to_numpy(np.int64)

    def sub(self, index):
        index = np.asarray(index)
        if index.dtype == bool:
            index = np.flatnonzero(index)
        return Part(self.X[index], self.names, self.y[index],
                    self.frame.iloc[index].reset_index(drop=True),
                    self.row_index[index], self.reference[index])


@dataclass
class Cell:
    history: Part
    target: Part
    audit: dict


def discover_population(cache_dir, structured_dir, expected_faculties=14):
    population = {}
    for cp in sorted(Path(cache_dir).glob("*.joblib")):
        match = re.fullmatch(r"(.+)_fold_([1-7])\.joblib", cp.name)
        if not match:
            raise ValueError(f"Unknown faculty/fold cache filename: {cp.name}")
        faculty, fold = match[1], int(match[2])
        if "/" in faculty or faculty in (".", "..") or not faculty.strip():
            raise ValueError("Invalid faculty filename")
        sp = Path(structured_dir) / cp.name
        if not sp.is_file():
            raise FileNotFoundError(f"Missing structured sidecar: {sp}")
        population.setdefault(faculty, {})[fold] = (cp, sp)
    if not population:
        raise FileNotFoundError(f"No canonical joblib files in {cache_dir}")
    if len(population) != expected_faculties:
        raise ValueError(f"Expected {expected_faculties} faculties; discovered {len(population)}")
    for faculty, folds in population.items():
        if set(folds) != set(range(1, 8)):
            raise ValueError(f"{faculty}: expected all seven folds, found {sorted(folds)}")
    return population


def _frame(frame, n, label):
    if not isinstance(frame, pd.DataFrame) or len(frame) != n:
        raise ValueError(f"{label}: frame row count mismatch")
    frame = frame.copy().reset_index(drop=True)
    for key in ("IDSV", "IDMH", "target_NHHK"):
        if key not in frame or frame[key].isna().any():
            raise ValueError(f"{label}: missing identity field {key}")
        if frame[key].astype(str).str.strip().eq("").any():
            raise ValueError(f"{label}: empty identity field {key}")
    frame["_ordinal"] = [semester_ordinal(v) for v in frame.target_NHHK]
    return frame


def load_cell(cache_path, structured_path, faculty, fold, common_names=None, strict=False):
    d = joblib.load(cache_path)
    sc = joblib.load(structured_path)
    required = ("Xtr", "Xte", "ytr", "yte", "train_frame", "test_frame",
                "feature_names", "cf_covered_mask", "cf_probabilities")
    if not isinstance(d, dict) or any(k not in d for k in required):
        raise ValueError(f"{faculty} F{fold}: missing canonical cache keys")
    names = list(d["feature_names"])
    if not names or len(set(names)) != len(names) or not all(isinstance(n, str) for n in names):
        raise ValueError("Missing or duplicate feature names")
    forbidden = {"target_grade10", "target_grade4", "y", "label", "target_status",
                 "target_IDCongDiem", "IDSV", "IDMH", "IDKQDK"}
    if forbidden.intersection(names):
        raise ValueError(f"Forbidden target/identity input fields: {sorted(forbidden.intersection(names))}")
    xtr, xte = np.asarray(d["Xtr"]), np.asarray(d["Xte"])
    ytr, yte = np.asarray(d["ytr"]), np.asarray(d["yte"])
    for X, y, label in ((xtr, ytr, "history"), (xte, yte, "target")):
        if X.ndim != 2 or X.shape[1] != len(names) or y.ndim != 1 or len(X) != len(y):
            raise ValueError(f"{label}: invalid feature/label shapes")
        if not len(y) or not np.isin(y, [0, 1]).all():
            raise ValueError(f"{label}: empty or nonbinary labels")
    train_frame = _frame(d["train_frame"], len(ytr), "history")
    test_frame = _frame(d["test_frame"], len(yte), "target")
    covered = np.asarray(d["cf_covered_mask"])
    cf = np.asarray(d["cf_probabilities"], float)
    if covered.shape != (len(ytr),) or not np.isin(covered, [0, 1]).all():
        raise ValueError("Invalid cross-fit coverage mask")
    if cf.ndim != 2 or cf.shape[0] != len(ytr) or cf.shape[1] < 1:
        raise ValueError("Invalid cross-fit probabilities shape")
    valid = covered.astype(bool) & np.isfinite(cf).all(axis=1)
    ix = np.flatnonzero(valid)
    if not len(ix) or ((cf[ix] < 0) | (cf[ix] > 1)).any():
        raise ValueError("Empty or invalid finite cross-fit coverage")
    if not isinstance(sc, dict) or any(k not in sc for k in ("train_row_index", "train", "target")):
        raise ValueError("Missing structured sidecar keys")
    source_index = np.asarray(sc["train_row_index"])
    if source_index.dtype.kind not in "iu" or not np.array_equal(source_index, ix):
        raise ValueError(f"{faculty} F{fold}: historical sidecar row indices do not align")
    if "target_row_index" in sc and not np.array_equal(sc["target_row_index"], np.arange(len(yte))):
        raise ValueError("Target sidecar row indices do not align")
    ctr = np.asarray(sc["train"]["course_target"], np.float32)
    cte = np.asarray(sc["target"]["course_target"], np.float32)
    if ctr.shape != (len(ix), 17) or cte.shape != (len(yte), 17):
        raise ValueError("Invalid structured course_target shapes; expected 17 inspected channels")
    raw_names = names if common_names is None else list(common_names)
    if any(n not in names for n in raw_names):
        raise ValueError("Common schema not contained in faculty feature schema")
    indices = [names.index(n) for n in raw_names]
    final_names = raw_names + ["course::" + n for n in COURSE_FEATURES]
    trX = np.concatenate((np.asarray(xtr[ix][:, indices], np.float32), ctr), axis=1)
    teX = np.concatenate((np.asarray(xte[:, indices], np.float32), cte), axis=1)
    tf = train_frame.iloc[ix].reset_index(drop=True)
    ref_source = "unavailable"
    ref = np.full(len(yte), np.nan, float)
    if "qte" in d or "pte" in d:
        ref_source = "qte" if "qte" in d else "pte_member_mean"
        ref = np.asarray(d.get("qte", d.get("pte")), float)
        if ref.ndim == 2:
            ref = ref.mean(axis=1)
        if ref.shape != (len(yte),):
            raise ValueError("Reference prediction shape mismatch")
        finite = np.isfinite(ref)
        if ((ref[finite] < 0) | (ref[finite] > 1)).any():
            raise ValueError("Reference probability outside [0,1]")
    h = Part(trX, final_names, ytr[ix].astype(np.int8), tf, ix, cf[ix].mean(axis=1))
    t = Part(teX, final_names, yte.astype(np.int8), test_frame, np.arange(len(yte)), ref)
    if h.semester.max() >= t.semester.min():
        raise ValueError(f"{faculty} F{fold}: chronology failed; future history")
    if len(np.unique(t.semester)) != 1:
        raise ValueError("Canonical faculty-fold target must be one semester")
    duplicates = int(test_frame.duplicated(["IDSV", "IDMH", "target_NHHK"]).sum())
    if strict and duplicates:
        raise ValueError("Duplicate target identities under strict data gate")
    audit = dict(faculty=faculty, fold=int(fold), raw_features=len(names),
                 history_original=len(ytr), history_rows=len(h), target_rows=len(t),
                 excluded_history=len(ytr)-len(h), excluded_failures=int(ytr[~valid].sum()),
                 history_failures=int(h.y.sum()), target_failures=int(t.y.sum()),
                 target_prevalence=float(t.y.mean()), coverage=float(valid.mean()),
                 history_first=int(h.semester.min()), history_last=int(h.semester.max()),
                 target_semester=str(test_frame.target_NHHK.iloc[0]),
                 target_ordinal=int(t.semester[0]), structured_features=17,
                 sidecar_alignment="HISTORY_EXACT_CANONICAL_POSITION",
                 target_sidecar_alignment=("EXPLICIT_CANONICAL_POSITION" if "target_row_index" in sc
                                           else "INHERITED_INSPECTED_SOURCE_ORDER_SHAPE_ONLY"),
                 duplicate_targets=duplicates,
                 duplicate_policy="retain_canonical_row_index",
                 cache_nonfinite_rate=float((~np.isfinite(trX)).mean()),
                 reference_source=ref_source, reference_coverage=float(np.isfinite(ref).mean()),
                 provenance="SEMESTER_PROXY_ONLY_NOT_RELEASE_CERTIFIED",
                 raw_names=names)
    return Cell(h, t, audit)


def inspect_population(population, strict=False, progress=None):
    raw_schemas, audits, calendar, hashes, target_origins = {}, [], {}, {}, {}
    for faculty, folds in population.items():
        for fold, (cp, sp) in sorted(folds.items()):
            if progress:
                progress(dict(faculty=faculty, fold=fold, source=cp.name))
            cell = load_cell(cp, sp, faculty, fold, strict=strict)
            schema = cell.audit["raw_names"]
            if faculty in raw_schemas and schema != raw_schemas[faculty]:
                raise ValueError(f"{faculty}: feature schema/order drift across folds")
            raw_schemas[faculty] = schema
            audits.append({k: v for k, v in cell.audit.items() if k != "raw_names"})
            calendar.setdefault(fold, set()).update(map(int, cell.history.semester))
            target_origins.setdefault(fold, set()).add(cell.audit["target_ordinal"])
            hashes[str(cp.resolve())] = sha256_file(cp)
            hashes[str(sp.resolve())] = sha256_file(sp)
    common = set.intersection(*(set(v) for v in raw_schemas.values()))
    first = raw_schemas[sorted(raw_schemas)[0]]
    common = [n for n in first if n in common]
    if not common:
        raise ValueError("No common raw feature schema")
    for fold in target_origins:
        if len(target_origins[fold]) != 1 or max(calendar[fold]) >= min(target_origins[fold]):
            raise ValueError("Global faculty/fold chronology inconsistency")
    origins = [next(iter(target_origins[f])) for f in sorted(target_origins)]
    if np.any(np.diff(origins) <= 0):
        raise ValueError("Fold target origins are not strictly increasing")
    return common, audits, {f: sorted(v) for f, v in calendar.items()}, hashes


def chronological_split(part, validation_blocks=3, calendar=None):
    semesters = np.asarray(part.semester)
    calendar = sorted(set(semesters) if calendar is None else set(calendar))
    candidates = list(dict.fromkeys([validation_blocks, 2, 1] + list(range(4, len(calendar)))))
    for blocks in candidates:
        if blocks < 1 or blocks >= len(calendar):
            continue
        cutoff = calendar[-blocks]
        fit, val = np.flatnonzero(semesters < cutoff), np.flatnonzero(semesters >= cutoff)
        if len(fit) and len(val) and len(np.unique(part.y[fit])) == 2 and len(np.unique(part.y[val])) == 2:
            if semesters[fit].max() >= semesters[val].min():
                raise ValueError("Historical chronological split is not strict")
            return fit, val, dict(cutoff=int(cutoff), validation_blocks=blocks,
                                 requested_blocks=validation_blocks,
                                 fallback=blocks != validation_blocks,
                                 fitting_rows=len(fit), validation_rows=len(val))
    raise ValueError("Insufficient historical class support for chronological validation")
