# -*- coding: utf-8 -*-
from __future__ import annotations

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

import joblib
import numpy as np
import pandas as pd
import torch
from torch.utils.data import Dataset

from suite26_common import semester_ordinal

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
GRADE_COL='target_grade10'


def discover_faculties(cache_dir: str|Path, fold: int=1)->List[str]:
    root=Path(cache_dir); suffix=f'_fold_{int(fold)}.joblib'
    out=[p.name[:-len(suffix)] for p in sorted(root.glob(f'*{suffix}'))]
    if not out: raise FileNotFoundError(f'No canonical cache files under {root}')
    return out


def parse_faculties(value, available):
    if value is None or str(value).strip().lower() in {'','all','auto'}: return list(available)
    wanted=[x.strip() for x in str(value).split(',') if x.strip()]
    missing=[x for x in wanted if x not in available]
    if missing: raise ValueError(f'Unknown faculties: {missing}')
    return wanted


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


def discover_common_feature_names(cache_dir,faculties,fold=1):
    caches={f:joblib.load(Path(cache_dir)/f'{f}_fold_{int(fold)}.joblib') for f in faculties}
    return common_raw_features(caches,faculties)


def align_raw_features(X,feature_names,common_names):
    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.nan_to_num(np.asarray(X,np.float32)[:,[idx[n] for n in common_names]],nan=0.,posinf=0.,neginf=0.)


def selected_history_indices(d):
    covered=np.asarray(d['cf_covered_mask'],bool); p=np.asarray(d['cf_probabilities'],float)
    valid=covered & np.isfinite(p).all(1)
    return np.where(valid)[0]


def selected_blocks(d,idx):
    key='cf_block_ids' if 'cf_block_ids' in d else 'train_groups'
    raw=np.asarray(d.get(key,np.full(len(d['ytr']),np.nan)),dtype=object)
    z=pd.to_numeric(pd.Series(raw[idx]),errors='coerce').to_numpy(float)
    if not np.isfinite(z).all(): raise ValueError(f'Non-finite temporal blocks source={key}')
    return z.astype(np.float32)


def _grade_from_frame(frame:pd.DataFrame, n:int)->np.ndarray:
    # Canonical train_v3 target column. Fail loudly for historical rows because
    # Suite26 explicitly studies the value of the student's observed outcomes.
    if GRADE_COL not in frame.columns:
        raise KeyError(f'Missing required historical grade column {GRADE_COL!r}')
    g=pd.to_numeric(frame[GRADE_COL],errors='coerce').to_numpy(np.float32)
    if len(g)!=n: raise ValueError('grade length mismatch')
    # A few administrative rows may have missing numeric grade despite a binary target.
    # Preserve them with NaN and expose grade_present to the model; never impute using target data.
    return g


@dataclass
class StudentBundle:
    current:np.ndarray
    y:np.ndarray
    committee:np.ndarray
    faculty_idx:np.ndarray
    blocks:np.ndarray
    semester_ordinal:np.ndarray
    semester_z:np.ndarray
    target_semester:np.ndarray
    student_id:np.ndarray
    course_id:np.ndarray
    row_index:np.ndarray
    grade10:np.ndarray
    faculty_names:List[str]

    def __len__(self): return int(len(self.y))
    def subset(self,mask):
        m=np.asarray(mask,bool); kw={}
        for k,v in self.__dict__.items():
            kw[k]=v[m] if isinstance(v,np.ndarray) and len(v)==len(m) else v
        return StudentBundle(**kw)


@dataclass
class Standardizer:
    mean_:np.ndarray
    scale_:np.ndarray
    semester_mean:float
    semester_scale:float

    @classmethod
    def fit(cls,bundle:StudentBundle,mask):
        m=np.asarray(mask,bool)
        x=np.nan_to_num(bundle.current[m].astype(np.float32),nan=0.,posinf=0.,neginf=0.)
        mean=x.mean(0); scale=x.std(0); scale=np.where(scale<1e-6,1.,scale)
        sem=bundle.semester_ordinal[m].astype(np.float32); sm=float(np.mean(sem)); ss=float(np.std(sem)); ss=1. if ss<1e-6 else ss
        return cls(mean.astype(np.float32),scale.astype(np.float32),sm,ss)

    def transform(self,b:StudentBundle)->StudentBundle:
        x=((np.nan_to_num(b.current.astype(np.float32),nan=0.,posinf=0.,neginf=0.)-self.mean_)/self.scale_).astype(np.float32)
        sz=((b.semester_ordinal-self.semester_mean)/self.semester_scale).astype(np.float32)
        return StudentBundle(x,b.y,b.committee,b.faculty_idx,b.blocks,b.semester_ordinal,sz,b.target_semester,b.student_id,b.course_id,b.row_index,b.grade10,b.faculty_names)


def load_fold_bundle(cache_dir,structured_dir,faculties,fold,common_names=None):
    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=[]; target=[]
    for fi,fac in enumerate(faculties):
        d=caches[fac]; sp=structured_dir/f'{fac}_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 mismatch for {fac} F{fold}')
        if np.asarray(sc['train']['course_target']).shape[-1]!=len(COURSE_FEATURES): raise ValueError('course_target schema mismatch')
        raw_h=align_raw_features(d['Xtr'][idx],d['feature_names'],common_names)
        raw_t=align_raw_features(d['Xte'],d['feature_names'],common_names)
        ct_h=np.asarray(sc['train']['course_target'],np.float32); ct_t=np.asarray(sc['target']['course_target'],np.float32)
        cur_h=np.concatenate([raw_h,np.nan_to_num(ct_h)],1).astype(np.float32)
        cur_t=np.concatenate([raw_t,np.nan_to_num(ct_t)],1).astype(np.float32)
        fr=d['train_frame'].iloc[idx].reset_index(drop=True); ft=d['test_frame'].reset_index(drop=True)
        sem_h=pd.to_numeric(fr['target_NHHK'],errors='coerce').to_numpy(float); sem_t=pd.to_numeric(ft['target_NHHK'],errors='coerce').to_numpy(float)
        if not np.isfinite(sem_h).all() or not np.isfinite(sem_t).all(): raise ValueError(f'Non-finite target semester {fac} F{fold}')
        ord_h=np.asarray([semester_ordinal(v) for v in sem_h],np.float32); ord_t=np.asarray([semester_ordinal(v) for v in sem_t],np.float32)
        if not np.isfinite(ord_h).all() or not np.isfinite(ord_t).all(): raise ValueError(f'Unparseable semester {fac} F{fold}')
        gh=_grade_from_frame(fr,len(fr))
        hist.append(dict(current=cur_h,y=np.asarray(d['ytr'],int)[idx].astype(np.int8),committee=np.asarray(d['cf_probabilities'],float)[idx].mean(1).astype(np.float32),faculty_idx=np.full(len(idx),fi,np.int16),blocks=selected_blocks(d,idx),semester_ordinal=ord_h,semester_z=np.zeros(len(idx),np.float32),target_semester=sem_h.astype(np.float32),student_id=fr['IDSV'].astype(str).to_numpy(),course_id=fr['IDMH'].astype(str).to_numpy(),row_index=idx.astype(np.int32),grade10=gh.astype(np.float32)))
        try:
            import train_v3 as core; tb,_=core.extract_temporal_groups(d['test_frame']); tb=np.asarray(tb,np.float32)
        except Exception: tb=np.full(len(ft),float(fold),np.float32)
        if 'qte' in d and np.asarray(d['qte']).ndim==1: committee_t=np.asarray(d['qte'],float)
        else: committee_t=np.asarray(d['pte'],float).mean(1)
        # Target grade is intentionally absent from model input. Keep NaN so accidental use is detectable.
        target.append(dict(current=cur_t,y=np.asarray(d['yte'],int).astype(np.int8),committee=np.asarray(committee_t,np.float32),faculty_idx=np.full(len(ft),fi,np.int16),blocks=tb,semester_ordinal=ord_t,semester_z=np.zeros(len(ft),np.float32),target_semester=sem_t.astype(np.float32),student_id=ft['IDSV'].astype(str).to_numpy(),course_id=ft['IDMH'].astype(str).to_numpy(),row_index=np.arange(len(ft),dtype=np.int32),grade10=np.full(len(ft),np.nan,np.float32)))
    def cat(parts):
        return StudentBundle(**{k:np.concatenate([p[k] for p in parts],0) for k in parts[0]},faculty_names=list(faculties))
    return cat(hist),cat(target),list(common_names)


def current_feature_names(common_names): return list(common_names)+[f'course::{x}' for x in COURSE_FEATURES]


@dataclass
class HistoryStore:
    idx:np.ndarray            # [N,H] local bank positions; -1 padding
    recency:np.ndarray        # [N,H] semester distance
    same_course:np.ndarray    # [N,H]
    support:np.ndarray        # [N]
    failure_support:np.ndarray
    grade_support:np.ndarray
    course_seen_count:np.ndarray
    max_history:int


def build_history_store(bank:StudentBundle, query:StudentBundle, allowed_bank_mask=None, max_history=32, keep_recent=24, progress_every=50000):
    """Chronology-safe same-student episodic memory.

    Every candidate must satisfy candidate_semester < query_semester. If more than
    ``max_history`` events exist, retain the most recent ``keep_recent`` and then
    older failures (the most strongly validated rare-event signal), finally fill by
    recency. Same-semester courses are never used because courses within a semester
    are concurrent/unordered in AQ.
    """
    n=len(query); H=int(max_history); keep_recent=min(int(keep_recent),H)
    idx=np.full((n,H),-1,np.int32); rec=np.zeros((n,H),np.float32); same=np.zeros((n,H),np.float32)
    support=np.zeros(n,np.int16); fs=np.zeros(n,np.int16); gs=np.zeros(n,np.int16); cs=np.zeros(n,np.int32)
    allowed=np.ones(len(bank),bool) if allowed_bank_mask is None else np.asarray(allowed_bank_mask,bool)
    by_student={}; course_counts={}
    for j in np.where(allowed)[0]:
        by_student.setdefault(str(bank.student_id[j]),[]).append(int(j))
        c=str(bank.course_id[j]); course_counts[c]=course_counts.get(c,0)+1
    # Sort once by chronological order and stable row index.
    for sid,v in by_student.items(): v.sort(key=lambda j:(float(bank.semester_ordinal[j]),int(bank.row_index[j])))
    for i in range(n):
        cs[i]=int(course_counts.get(str(query.course_id[i]),0))
        cand=by_student.get(str(query.student_id[i]),[])
        if cand:
            qsem=float(query.semester_ordinal[i]); eligible=[j for j in cand if float(bank.semester_ordinal[j]) < qsem]
            if eligible:
                recent=eligible[-keep_recent:]
                chosen=list(recent); chosen_set=set(chosen)
                # Preserve older failures if recent truncation would otherwise erase them.
                if len(chosen)<H:
                    older=[j for j in eligible[:-keep_recent] if int(bank.y[j])==1 and j not in chosen_set]
                    older=older[-(H-len(chosen)):]; chosen=older+chosen; chosen_set=set(chosen)
                if len(chosen)<H:
                    fill=[j for j in eligible if j not in chosen_set]
                    chosen=fill[-(H-len(chosen)):]+chosen
                # Present in chronological order; attention is set-based but recency is explicit.
                chosen=sorted(chosen,key=lambda j:(float(bank.semester_ordinal[j]),int(bank.row_index[j])))[-H:]
                k=len(chosen); idx[i,:k]=chosen
                rr=qsem-bank.semester_ordinal[np.asarray(chosen,int)]; rec[i,:k]=rr.astype(np.float32)
                same[i,:k]=(bank.course_id[np.asarray(chosen,int)]==str(query.course_id[i])).astype(np.float32)
                support[i]=k; fs[i]=int(bank.y[np.asarray(chosen,int)].sum()); gs[i]=int(np.isfinite(bank.grade10[np.asarray(chosen,int)]).sum())
        if progress_every and (i+1)%int(progress_every)==0: print(f'[HISTORY STORE] {i+1:,}/{n:,}')
    # Strict audit.
    valid=idx>=0
    if valid.any():
        qi=np.repeat(np.arange(n)[:,None],H,axis=1)[valid]; bi=idx[valid]
        bad=np.sum(bank.semester_ordinal[bi] >= query.semester_ordinal[qi])
        if bad: raise RuntimeError(f'Chronology violation in history store: {bad} candidate edges')
    return HistoryStore(idx,rec,same,support,fs,gs,cs,H)


@dataclass
class TreeArrays:
    leaf_ids:np.ndarray   # [N,T] global compact leaf ids
    score:np.ndarray      # [N]


class Suite26Dataset(Dataset):
    def __init__(self,query:StudentBundle,bank:StudentBundle,query_tree:TreeArrays,bank_tree:TreeArrays,store:HistoryStore|None,indices=None,history_grade_override=None,history_y_override=None):
        self.query=query; self.bank=bank; self.query_tree=query_tree; self.bank_tree=bank_tree; self.store=store
        self.indices=np.arange(len(query),dtype=np.int64) if indices is None else np.asarray(indices,np.int64)
        self.grade_override=history_grade_override; self.y_override=history_y_override
    def __len__(self): return len(self.indices)
    def __getitem__(self,j):
        i=int(self.indices[j]); out={
            'current':torch.from_numpy(self.query.current[i]),
            'current_leaf':torch.from_numpy(self.query_tree.leaf_ids[i].astype(np.int64)),
            'tree_score':torch.tensor(float(self.query_tree.score[i]),dtype=torch.float32),
            'faculty_idx':torch.tensor(int(self.query.faculty_idx[i]),dtype=torch.long),
            'semester_z':torch.tensor(float(self.query.semester_z[i]),dtype=torch.float32),
            'y':torch.tensor(float(self.query.y[i]),dtype=torch.float32),
            'committee':torch.tensor(float(self.query.committee[i]),dtype=torch.float32),
            'bundle_index':torch.tensor(i,dtype=torch.long),
        }
        H=self.store.max_history if self.store is not None else 1
        if self.store is None:
            D=self.query.current.shape[1]; T=self.query_tree.leaf_ids.shape[1]
            out.update({'history_x':torch.zeros((H,D),dtype=torch.float32),'history_leaf':torch.zeros((H,T),dtype=torch.long),'history_tree_score':torch.zeros(H),'history_y':torch.zeros(H),'history_grade':torch.zeros(H),'history_grade_present':torch.zeros(H),'history_recency':torch.zeros(H),'history_same_course':torch.zeros(H),'history_mask':torch.zeros(H,dtype=torch.bool)})
            return out
        ids=self.store.idx[i]; mask=ids>=0; safe=np.where(mask,ids,0)
        gy=np.asarray(self.bank.y if self.y_override is None else self.y_override,np.float32)[safe]
        gg=np.asarray(self.bank.grade10 if self.grade_override is None else self.grade_override,np.float32)[safe]
        present=np.isfinite(gg).astype(np.float32); gg=np.nan_to_num(gg,nan=5.0,posinf=10.0,neginf=0.0)/10.0
        out.update({
            'history_x':torch.from_numpy(self.bank.current[safe]),
            'history_leaf':torch.from_numpy(self.bank_tree.leaf_ids[safe].astype(np.int64)),
            'history_tree_score':torch.from_numpy(self.bank_tree.score[safe].astype(np.float32)),
            'history_y':torch.from_numpy((gy*mask).astype(np.float32)),
            'history_grade':torch.from_numpy((gg*mask).astype(np.float32)),
            'history_grade_present':torch.from_numpy((present*mask).astype(np.float32)),
            'history_recency':torch.from_numpy(self.store.recency[i].astype(np.float32)),
            'history_same_course':torch.from_numpy(self.store.same_course[i].astype(np.float32)),
            'history_mask':torch.from_numpy(mask.astype(bool)),
        })
        return out
