import sys
from pathlib import Path
import numpy as np
import pandas as pd
import joblib
import torch

ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT))
from suite25_data import RetrievalBundle,RetrievalPreprocessor,RetrievalDataset
from suite25_retrieval import CourseRelationIndex,materialize_retrieval_store,shuffled_bank_labels
from suite25_model import StructureAwareChronologicalRetrieval


def bundle(n=8,d=10):
    rng=np.random.default_rng(3);sem=np.array([1,1,2,2,3,3,4,4],np.float32)[:n]
    return RetrievalBundle(rng.normal(size=(n,d)).astype(np.float32),np.array([0,1,0,0,1,0,1,0],np.int8)[:n],np.full(n,.1,np.float32),np.array([0,0,0,1,0,1,0,1],np.int16)[:n],sem.copy(),sem.copy(),np.zeros(n,np.float32),np.array([20241,20241,20242,20242,20243,20243,20251,20251],np.float32)[:n],np.array(['s1','s2','s1','s3','s1','s4','s5','s1'],object)[:n],np.array(['c1','c1','c2','c3','c1','c4','c2','c5'],object)[:n],np.arange(n,dtype=np.int32),['f0','f1'])

def relation_index(tmp_path):
    edges=pd.DataFrame({'src':['c2','c3'],'dst':['c5','c5'],'family':[1,2],'kind':[1,2],'available_nhhk':[20240,20240]});joblib.dump(edges,tmp_path/'course_edges.joblib');return CourseRelationIndex(tmp_path)

def test_preprocessor_shapes():
    b=bundle();p=RetrievalPreprocessor.fit(b,np.ones(len(b),bool),1,pca_dim=4,pca_rows=100);z=p.transform(b);assert z.current.shape==b.current.shape;assert p.coarse(z).shape==(len(b),4)

def test_chronology_and_structured_pools(tmp_path):
    b=bundle();p=RetrievalPreprocessor.fit(b,np.ones(len(b),bool),1,pca_dim=4);z=p.transform(b);ri=relation_index(tmp_path);c=p.coarse(z);st=materialize_retrieval_store(ri,z,z,c,c,student_pool=4,course_pool=4,struct_pool=4,state_pool=3,max_candidates=12,progress_every=0)
    assert not st.chronology_violation.any()
    for i in range(len(z)):
        ids=st.candidate_idx[i][st.candidate_idx[i]>=0]
        if len(ids):assert np.all(z.semester_ordinal[ids] < z.semester_ordinal[i])
    assert st.pool_flags[4,:,0].sum()>=1 # s1 has prior history
    assert st.pool_flags[4,:,1].sum()>=1 # c1 has prior attempts

def test_model_forward_and_gates(tmp_path):
    b=bundle();p=RetrievalPreprocessor.fit(b,np.ones(len(b),bool),1,pca_dim=4);z=p.transform(b);ri=relation_index(tmp_path);c=p.coarse(z);st=materialize_retrieval_store(ri,z,z,c,c,student_pool=4,course_pool=4,struct_pool=4,state_pool=3,max_candidates=12,progress_every=0);ds=RetrievalDataset(z,z,st,indices=[4,6,7]);batch={k:torch.stack([ds[i][k] for i in range(len(ds))]) for k in ds[0]}
    m=StructureAwareChronologicalRetrieval(z.current.shape[1],2,'sacr',d_main=32,key_dim=16,faculty_dim=4,encoder_blocks=1,predictor_blocks=1,context_size=4,pool_context=2);out,a=m(batch,return_aux=True);assert out.shape==(3,);assert a['candidate_attention'].shape==(3,12);s=a['pool_gate'].sum(1);assert torch.all((s==0)|torch.isclose(s,torch.ones_like(s),atol=1e-5))

def test_label_ablation_changes_output(tmp_path):
    b=bundle();p=RetrievalPreprocessor.fit(b,np.ones(len(b),bool),1,pca_dim=4);z=p.transform(b);ri=relation_index(tmp_path);c=p.coarse(z);st=materialize_retrieval_store(ri,z,z,c,c,student_pool=4,course_pool=4,struct_pool=4,state_pool=3,max_candidates=12,progress_every=0);ds=RetrievalDataset(z,z,st,indices=[4,6,7]);batch={k:torch.stack([ds[i][k] for i in range(len(ds))]) for k in ds[0]};m=StructureAwareChronologicalRetrieval(z.current.shape[1],2,'sacr',d_main=32,key_dim=16,faculty_dim=4,encoder_blocks=1,predictor_blocks=1,context_size=4,pool_context=2);a=m(batch);b2=m(batch,ablate=['no_labels']);assert not torch.allclose(a,b2)

def test_no_retrieval_is_finite(tmp_path):
    b=bundle();p=RetrievalPreprocessor.fit(b,np.ones(len(b),bool),1,pca_dim=4);z=p.transform(b);ri=relation_index(tmp_path);c=p.coarse(z);st=materialize_retrieval_store(ri,z,z,c,c,state_pool=3,max_candidates=12,progress_every=0);ds=RetrievalDataset(z,z,st,indices=[4,6]);batch={k:torch.stack([ds[i][k] for i in range(len(ds))]) for k in ds[0]};m=StructureAwareChronologicalRetrieval(z.current.shape[1],2,'sacr',d_main=32,key_dim=16,faculty_dim=4,encoder_blocks=1,predictor_blocks=1);o=m(batch,ablate=['no_retrieval']);assert torch.isfinite(o).all()

def test_shuffle_preserves_faculty_prevalence():
    b=bundle();y=shuffled_bank_labels(b,7)
    for f in np.unique(b.faculty_idx):
        m=b.faculty_idx==f;assert y[m].sum()==b.y[m].sum()
