# -*- coding: utf-8 -*-
from __future__ import annotations
from collections import defaultdict
from pathlib import Path
import math
import numpy as np
import pandas as pd
import joblib
from suite24_common import semester_distance, stable_hash

MAX_HISTORY_COURSES=16
MAX_STRUCT_COURSES=10
STUDENT_RAW_DIM=16
COURSE_STATIC=['SoTinChi','SoTietMH','SoTietTuan','SiSoMax','SiSoMin','KhgTinhDTB','IsMHTN','IsMonKT','IsMHKN','IsMHDacThu','IsGDTiengAnh','IsMonNgoaiNgu']
COURSE_PROFILE_NAMES=COURSE_STATIC+['hist_count','hist_mean','hist_std','hist_fail_rate','hist_pass_rate']
COURSE_RAW_DIM=len(COURSE_PROFILE_NAMES)
EDGE_RAW_DIM=8
SUPPORT_DIM=6

STUDENT_PROFILE_NAMES=['hist_count','grade_mean','grade_std','fail_rate','pass_rate','recent3_mean','recent3_fail_rate','last_grade','grade_slope','credits_sum','unique_courses','semester_span','latest_recency','max_grade','min_grade','passed_credit_ratio']


def _norm_id(x):
    if pd.isna(x): return None
    s=str(x)
    try:
        f=float(s)
        if math.isfinite(f) and abs(f-round(f))<1e-9:return str(int(round(f)))
    except Exception: pass
    return s

def _coalesce_grade(df):
    a=pd.to_numeric(df.get('DiemHP',pd.Series(np.nan,index=df.index)),errors='coerce')
    if 'DiemHPCalc' in df:
        b=pd.to_numeric(df['DiemHPCalc'],errors='coerce');a=a.where(a.notna(),b)
    return a

def _safe_int(x,default=0):
    try:
        if x is None or pd.isna(x):return int(default)
        return int(float(x))
    except Exception:return int(default)

def encode_relation(family, forward, kind):
    family=1 if _safe_int(family,2)==1 else 2
    kind=max(1,min(3,_safe_int(kind,2)))
    if family==1:return kind if forward else 3+kind
    return 6+kind if forward else 9+kind

def relation_available(av,target):
    if av is None or pd.isna(av):return True
    try:return int(float(av))<int(float(target))
    except Exception:return False

def _edge_attr(grade=0,credits=0,passed=0,recency=0,rel_code=0,direct=0,structural=0,support=1):
    rec=max(float(recency),0.) if np.isfinite(recency) else 0.
    return np.array([float(grade)/10.,float(credits)/10.,float(passed),math.log1p(rec)/3.,math.exp(-rec/3.),float(rel_code)/13.,float(direct),float(structural)*float(support)],np.float32)

class EvidenceIndex:
    def __init__(self,raw_index_dir):
        root=Path(raw_index_dir);self.root=root
        needed=['course_history.joblib','course_static.joblib','course_edges.joblib']
        missing=[x for x in needed if not (root/x).exists()]
        if missing:raise FileNotFoundError(f'Missing Suite12 raw-index files: {missing} under {root}')
        self.hp=joblib.load(root/'course_history.joblib').copy();self.static_df=joblib.load(root/'course_static.joblib').copy();self.edges=joblib.load(root/'course_edges.joblib').copy()
        self.hp['IDSV']=self.hp.IDSV.map(_norm_id);self.hp['IDMH']=self.hp.IDMH.map(_norm_id);self.hp['NHHK']=pd.to_numeric(self.hp.NHHK,errors='coerce');self.hp['grade_value']=_coalesce_grade(self.hp)
        credit_series=self.hp['SoTinChi'] if 'SoTinChi' in self.hp else pd.Series(0,index=self.hp.index);pass_series=self.hp['DatHP'] if 'DatHP' in self.hp else pd.Series(np.nan,index=self.hp.index)
        self.hp['credits_value']=pd.to_numeric(credit_series,errors='coerce').fillna(0);self.hp['passed_value']=pd.to_numeric(pass_series,errors='coerce');self.hp['passed_value']=self.hp['passed_value'].where(self.hp['passed_value'].notna(),(self.hp['grade_value']>=5).astype(float))
        self.hp=self.hp[self.hp.IDSV.notna()&self.hp.IDMH.notna()&self.hp.NHHK.notna()&self.hp.grade_value.notna()].copy()
        self.by_student={k:g.sort_values('NHHK') for k,g in self.hp.groupby('IDSV',sort=False)};self.by_course={k:g.sort_values('NHHK') for k,g in self.hp.groupby('IDMH',sort=False)}
        self.static_df['IDMH']=self.static_df.IDMH.map(_norm_id);self.static={}
        for r in self.static_df.itertuples(index=False):self.static[_norm_id(getattr(r,'IDMH'))]=np.nan_to_num(np.array([pd.to_numeric(getattr(r,c,np.nan),errors='coerce') for c in COURSE_STATIC],np.float32))
        self.edge_map=defaultdict(list);self.edge_pair={}
        for r in self.edges.itertuples(index=False):
            a=_norm_id(getattr(r,'src'));b=_norm_id(getattr(r,'dst'));av=getattr(r,'available_nhhk',np.nan);fam=_safe_int(getattr(r,'family',0),0);kind=_safe_int(getattr(r,'kind',0),0)
            fwd=encode_relation(fam,True,kind);rev=encode_relation(fam,False,kind)
            self.edge_map[a].append((b,fwd,fam,kind,av));self.edge_map[b].append((a,rev,fam,kind,av));self.edge_pair[(a,b)]=(fwd,fam,kind,av);self.edge_pair[(b,a)]=(rev,fam,kind,av)
        self._student_cache={};self._course_cache={};self._history_cache={};self._struct_cache={}

    def student_profile(self,s,target):
        s=_norm_id(s);key=(s,int(float(target)))
        if key in self._student_cache:return self._student_cache[key]
        g=self.by_student.get(s);z=g[g.NHHK<float(target)] if g is not None else None;out=np.zeros(STUDENT_RAW_DIM,np.float32)
        if z is not None and len(z):
            grades=z.grade_value.to_numpy(float);credits=z.credits_value.to_numpy(float);passed=z.passed_value.to_numpy(float);sem=z.NHHK.to_numpy(float);recent=grades[-3:];x=np.arange(len(grades),dtype=float);slope=float(np.polyfit(x,grades,1)[0]) if len(grades)>1 else 0.;rec=semester_distance(target,sem[-1]);den=max(float(np.sum(credits)),1e-6)
            try:
                from suite24_common import semester_ordinal
                ords=[semester_ordinal(v) for v in sem];ords=[o for o in ords if o is not None];span=float(max(ords)-min(ords)) if len(ords)>1 else 0.
            except Exception:span=0.
            out=np.array([len(grades),grades.mean(),grades.std() if len(grades)>1 else 0.,np.mean(grades<5),np.mean(grades>=5),recent.mean(),np.mean(recent<5),grades[-1],slope,np.sum(credits),z.IDMH.nunique(),span,0. if not np.isfinite(rec) else rec,np.max(grades),np.min(grades),float(np.sum(credits*passed)/den)],np.float32)
        self._student_cache[key]=out;return out

    def course_profile(self,c,target):
        c=_norm_id(c);key=(c,int(float(target)))
        if key in self._course_cache:return self._course_cache[key]
        st=self.static.get(c,np.zeros(len(COURSE_STATIC),np.float32));g=self.by_course.get(c);hist=np.zeros(5,np.float32)
        if g is not None:
            z=g[g.NHHK<float(target)];grades=z.grade_value.to_numpy(float)
            if len(grades):hist=np.array([len(grades),grades.mean(),grades.std() if len(grades)>1 else 0.,np.mean(grades<5),np.mean(grades>=5)],np.float32)
        out=np.nan_to_num(np.concatenate([st,hist])).astype(np.float32);self._course_cache[key]=out;return out

    def history_courses(self,s,target_course,target,max_n=MAX_HISTORY_COURSES):
        s=_norm_id(s);target_course=_norm_id(target_course);key=(s,target_course,int(float(target)),int(max_n))
        if key in self._history_cache:return self._history_cache[key]
        g=self.by_student.get(s);z=g[g.NHHK<float(target)].copy() if g is not None else pd.DataFrame();rows=[]
        if len(z):
            z=z.sort_values('NHHK').drop_duplicates('IDMH',keep='last')
            for r in z.itertuples(index=False):
                c=_norm_id(getattr(r,'IDMH'));pair=self.edge_pair.get((c,target_course));rel=1 if pair and relation_available(pair[3],target) else 0
                # Prioritize exact target-course attempts, then structurally linked history, then recency.
                rows.append((0 if c==target_course else 1 if rel else 2,-float(getattr(r,'NHHK')),r,pair))
            rows.sort(key=lambda x:(x[0],x[1]));rows=rows[:max_n]
        self._history_cache[key]=rows;return rows

    def structural_courses(self,c,target,max_n=MAX_STRUCT_COURSES):
        c=_norm_id(c);key=(c,int(float(target)),int(max_n))
        if key in self._struct_cache:return self._struct_cache[key]
        vals=[]
        for nb,code,fam,kind,av in self.edge_map.get(c,[]):
            if relation_available(av,target):vals.append((0 if fam==1 else 1,code,nb,fam,kind,av))
        vals=sorted(vals,key=lambda x:(x[0],x[1],x[2]))[:max_n];self._struct_cache[key]=vals;return vals


def build_query_evidence(index:EvidenceIndex,student,course,target):
    student=_norm_id(student);course=_norm_id(course);target=float(target)
    student_x=index.student_profile(student,target);target_x=index.course_profile(course,target)
    hist_x=np.zeros((MAX_HISTORY_COURSES,COURSE_RAW_DIM),np.float16);hist_edge=np.zeros((MAX_HISTORY_COURSES,EDGE_RAW_DIM),np.float16);hist_rel=np.zeros(MAX_HISTORY_COURSES,np.uint8);hist_mask=np.zeros(MAX_HISTORY_COURSES,bool)
    struct_x=np.zeros((MAX_STRUCT_COURSES,COURSE_RAW_DIM),np.float16);struct_edge=np.zeros((MAX_STRUCT_COURSES,EDGE_RAW_DIM),np.float16);struct_rel=np.zeros(MAX_STRUCT_COURSES,np.uint8);struct_mask=np.zeros(MAX_STRUCT_COURSES,bool)
    max_edge_sem=-np.inf;related_hist=0;same_hist=0;failed_hist=0;direct_struct=0
    hist=index.history_courses(student,course,target)
    for j,(_pri,_neg,r,pair) in enumerate(hist):
        c=_norm_id(getattr(r,'IDMH'));hist_x[j]=index.course_profile(c,target);grade=float(getattr(r,'grade_value'));credits=float(getattr(r,'credits_value'));passed=float(getattr(r,'passed_value'));sem=float(getattr(r,'NHHK'));max_edge_sem=max(max_edge_sem,sem);rec=semester_distance(target,sem);same=(c==course);rel_code=13 if same else (pair[0] if pair and relation_available(pair[3],target) else 0);direct=1 if pair and int(pair[1])==1 else 0
        hist_edge[j]=_edge_attr(grade,credits,passed,rec,rel_code,direct,1 if rel_code else 0);hist_rel[j]=rel_code;hist_mask[j]=1;related_hist+=int(rel_code>0 and not same);same_hist+=int(same);failed_hist+=int(grade<5)
    struct=index.structural_courses(course,target)
    for j,(_pri,code,nb,fam,kind,av) in enumerate(struct):
        struct_x[j]=index.course_profile(nb,target);struct_edge[j]=_edge_attr(0,0,0,0,code,1 if fam==1 else 0,1);struct_rel[j]=code;struct_mask[j]=1;direct_struct+=int(fam==1)
    violation=bool(np.isfinite(max_edge_sem) and max_edge_sem>=target)
    support=np.array([len(hist),len(struct),related_hist,same_hist,failed_hist,direct_struct],np.int16)
    return {'student_x':student_x.astype(np.float16),'target_course_x':target_x.astype(np.float16),'history_x':hist_x,'history_edge':hist_edge,'history_rel':hist_rel,'history_mask':hist_mask,'struct_x':struct_x,'struct_edge':struct_edge,'struct_rel':struct_rel,'struct_mask':struct_mask,'support':support,'max_edge_semester':np.float32(max_edge_sem if np.isfinite(max_edge_sem) else -1),'chronology_violation':violation}

class EvidenceTensorStore:
    KEYS=('student_x','target_course_x','history_x','history_edge','history_rel','history_mask','struct_x','struct_edge','struct_rel','struct_mask','support','max_edge_semester','chronology_violation')
    def __init__(self,data):self.data=data;self.n=len(data['student_x'])
    def __len__(self):return self.n
    def __getitem__(self,i):return {k:self.data[k][i] for k in self.KEYS}

def materialize_evidence(index,bundle,progress_every=25000):
    n=len(bundle.y);data={
      'student_x':np.zeros((n,STUDENT_RAW_DIM),np.float16),'target_course_x':np.zeros((n,COURSE_RAW_DIM),np.float16),'history_x':np.zeros((n,MAX_HISTORY_COURSES,COURSE_RAW_DIM),np.float16),'history_edge':np.zeros((n,MAX_HISTORY_COURSES,EDGE_RAW_DIM),np.float16),'history_rel':np.zeros((n,MAX_HISTORY_COURSES),np.uint8),'history_mask':np.zeros((n,MAX_HISTORY_COURSES),bool),'struct_x':np.zeros((n,MAX_STRUCT_COURSES,COURSE_RAW_DIM),np.float16),'struct_edge':np.zeros((n,MAX_STRUCT_COURSES,EDGE_RAW_DIM),np.float16),'struct_rel':np.zeros((n,MAX_STRUCT_COURSES),np.uint8),'struct_mask':np.zeros((n,MAX_STRUCT_COURSES),bool),'support':np.zeros((n,SUPPORT_DIM),np.int16),'max_edge_semester':np.full(n,-1,np.float32),'chronology_violation':np.zeros(n,bool)}
    for i,(s,c,t) in enumerate(zip(bundle.student_id,bundle.course_id,bundle.target_semester)):
        g=build_query_evidence(index,s,c,t)
        for k in data:data[k][i]=g[k]
        if progress_every and (i+1)%progress_every==0:print(f'[EVIDENCE CACHE] {i+1:,}/{n:,}')
    if data['chronology_violation'].any():raise RuntimeError(f'CHRONOLOGY VIOLATION rows={np.where(data["chronology_violation"])[0][:10].tolist()}')
    return EvidenceTensorStore(data)
