#!/usr/bin/env python3
from pathlib import Path
import sys,joblib,numpy as np

ROOT=Path('/root/disk_3tb/earlywarning/30072026')
CACHE=ROOT/'results/cross_faculty_residual_moe/cache'
STRUCT=ROOT/'results/suite12_structured_cache'

def main():
    print('='*110);print('SUITE25 FINAL ENVIRONMENT CHECK');print('='*110)
    print('python=',sys.version);print('executable=',sys.executable)
    mods=['numpy','pandas','sklearn','scipy','joblib','torch']
    for name in mods:
        m=__import__(name);print(f'[OK] {name:12s} {getattr(m,"__version__","unknown")}')
    import torch
    print(f'[CUDA] available={torch.cuda.is_available()} device={torch.cuda.get_device_name(0) if torch.cuda.is_available() else "cpu"}')
    suffix='_fold_1.joblib';fac=[p.name[:-len(suffix)] for p in sorted(CACHE.glob(f'*{suffix}'))]
    if not fac:raise RuntimeError(f'No canonical cache at {CACHE}')
    required=['Xtr','Xte','ytr','yte','feature_names','cf_covered_mask','cf_probabilities','train_frame','test_frame']
    for f in fac:
        for fold in range(1,8):
            p=CACHE/f'{f}_fold_{fold}.joblib'
            if not p.exists():raise RuntimeError(f'Missing {p}')
            d=joblib.load(p,mmap_mode='r');miss=[k for k in required if k not in d]
            if miss:raise RuntimeError(f'{p} missing {miss}')
            sp=STRUCT/f'{f}_fold_{fold}.joblib'
            if not sp.exists():raise RuntimeError(f'Missing structured cache {sp}')
            s=joblib.load(sp,mmap_mode='r')
            if 'train' not in s or 'target' not in s or 'train_row_index' not in s:raise RuntimeError(f'Invalid structured cache {sp}')
            if 'course_target' not in s['train'] or 'course_target' not in s['target']:raise RuntimeError(f'Missing course_target in {sp}')
    edge=STRUCT/'raw_index/course_edges.joblib'
    if not edge.exists():raise RuntimeError(f'Missing course relation source {edge}')
    first=joblib.load(CACHE/f'{fac[0]}_fold_1.joblib',mmap_mode='r');common=set(first['feature_names'])
    for f in fac[1:]:common&=set(joblib.load(CACHE/f'{f}_fold_1.joblib',mmap_mode='r')['feature_names'])
    print(f'[OK] canonical cache complete for {len(fac)} faculties x 7 folds')
    print(f'[DATA] common_raw_features={len(common)} + course_target=17 => current_features={len(common)+17}')
    print(f'[OK] course relation source {edge}')
    refs={'Suite22':ROOT/'results/suite22_sftr_tabm','Suite23':ROOT/'results/suite23_cs_hagt','Suite24':ROOT/'results/suite24_target_conditioned_structural_residual','Council':ROOT/'results/suite4/2_competence_council','V2':ROOT/'results/final_train_v2'}
    for k,p in refs.items():print(f'[REF] {k:8s} {"FOUND" if p.exists() else "MISSING"} {p}')
    print('\n[PASS] Suite25 environment is ready.')
if __name__=='__main__':main()
