"""Small structural fixtures. Synthetic scores have no AQ scientific meaning."""
from __future__ import annotations
from pathlib import Path
import json
import joblib
import numpy as np
import pandas as pd
from s34_data import COURSE_FEATURES, semester_ordinal


def make_synthetic(root, rows=48, faculties=2):
    root = Path(root)
    cache = root/"results/cross_faculty_residual_moe/cache"
    structured = root/"results/suite12_structured_cache"
    marker = root/"SYNTHETIC_DATA.json"
    expected = {f"synthetic_faculty_{f}_fold_{fold}.joblib"
                for f in range(faculties) for fold in range(1, 8)}
    marker_data = json.loads(marker.read_text()) if marker.exists() else None
    if marker_data is not None and (not marker_data.get("synthetic") or
            marker_data.get("faculties") != faculties or marker_data.get("rows_per_semester") != rows):
        raise ValueError("Synthetic fixture configuration differs; use a new dedicated root")
    # Validate BOTH actual write destinations before creating either directory/file.
    for destination in (cache, structured):
        files = list(destination.iterdir()) if destination.exists() else []
        if files and (marker_data is None or any(p.name not in expected for p in files)):
            raise ValueError(f"Refusing synthetic generation in existing source tree: {destination}")
    cache.mkdir(parents=True, exist_ok=True)
    structured.mkdir(parents=True, exist_ok=True)
    names = ["prev_cumulative_gpa10", "hist_failed_credits_sum",
             "prev_semester_gpa10", "recent3_semester_gpa10_mean",
             "hist_semester_count", "student_gpa_history_missing_flag",
             "hist_same_course_attempts", "course_fail_rate_clipped",
             "course_history_missing_flag", "course_cluster_id"]
    for f in range(faculties):
        faculty = f"synthetic_faculty_{f}"
        for fold, target in enumerate([20221, 20222, 20231, 20232, 20241, 20242, 20251], 1):
            cp, sp = cache/f"{faculty}_fold_{fold}.joblib", structured/f"{faculty}_fold_{fold}.joblib"
            if cp.exists() and sp.exists():
                continue
            rng = np.random.default_rng(1000+f*100+fold)
            target_ord = semester_ordinal(target)
            sem = np.repeat(np.arange(semester_ordinal(20191), target_ord), rows)
            semester_raw = (sem//3)*10+(sem%3)+1
            n = len(sem)

            def data(nrows):
                X = rng.normal(size=(nrows, len(names)+1)).astype(np.float32)
                X[:, 4] = rng.integers(0, 8, nrows)
                X[:, 5] = rng.random(nrows) < .1
                X[:, 6] = rng.integers(0, 3, nrows)
                X[:, 7] = rng.random(nrows)
                X[:, 8] = rng.random(nrows) < .15
                X[:, 9] = rng.integers(0, 4, nrows)
                C = rng.normal(size=(nrows, 17)).astype(np.float32)
                y = (X[:, 0]*C[:, 0]+.4*X[:, 2]*C[:, 1]+rng.normal(size=nrows) > 1).astype(np.int8)
                for i in range(0, nrows, rows):
                    y[i] = 1
                    if i+1 < nrows:
                        y[i+1] = 0
                X[::17, 2] = np.nan
                return X, C, y
            Xtr, Ctr, ytr = data(n)
            Xte, Cte, yte = data(rows)
            train_frame = pd.DataFrame(dict(IDSV=[f"s{i%rows}" for i in range(n)],
                                           IDMH=[f"c{i%9}" for i in range(n)],
                                           target_NHHK=semester_raw))
            test_frame = pd.DataFrame(dict(IDSV=[f"s{i}" for i in range(rows)],
                                          IDMH=[f"c{i%9}" for i in range(rows)],
                                          target_NHHK=target))
            covered = np.ones(n, bool)
            covered[:rows] = False
            cf = rng.uniform(.01, .5, (n, 3))
            cf[~covered] = np.nan
            d = dict(Xtr=Xtr, Xte=Xte, ytr=ytr, yte=yte,
                     train_frame=train_frame, test_frame=test_frame,
                     feature_names=names+[f"native_{f}"], cf_covered_mask=covered,
                     cf_probabilities=cf, qte=rng.uniform(.01, .5, rows))
            sc = dict(train_row_index=np.flatnonzero(covered),
                      train=dict(course_target=Ctr[covered]),
                      target=dict(course_target=Cte))
            joblib.dump(d, cp, compress=3)
            joblib.dump(sc, sp, compress=3)
    marker.write_text(json.dumps(dict(
        synthetic=True, rows_per_semester=rows, faculties=faculties,
        purpose="STRUCTURAL_VALIDATION_ONLY")))
    return cache, structured
