"""TabICLv2 actual ordinary fine-tuning and time-ordered MetaBatch adaptation.

Relies on pinned upstream internals; validates imports and signatures, never silently falls
back to ordinary finetuning. The upstream base's epoch loop remains authoritative.
"""
from __future__ import annotations
from contextlib import contextmanager
from pathlib import Path
import inspect, types
import numpy as np
from .core import note, scoring, support_select
from .episodes import build_episodes, chronological_meta_batch


def train_adapted(h,train_mask,val_mask,mode,output,seed,device,epochs=4,patience=2,lr=1e-5,weight_decay=.01,grad_clip=1.,episode_support=1024,episode_query=128,max_episodes=64,validation_support=4096,n_estimators_train=2,n_estimators_val=2,n_estimators_infer=8,model_path=None,release_gap=0):
    """Fit a true pretrained TabICL network, save/reload checkpoint; no target-fold labels.

    The custom chron iterator takes precomputed *historical* indices from h.subset(train_mask)
    and supplies temporally disjoint support/query MetaBatches to TabICL's native loss loop.
    """
    if mode not in ('ordinary','chronological'):raise ValueError(mode)
    try:
        import tabicl._finetune.base as upstream_base
        from tabicl import FinetunedTabICLClassifier
        from tabicl._finetune.base import ValidationMetrics
    except ImportError as e:raise RuntimeError('TabICLv2 not installed; adaptation requires real tabicl package') from e
    src=inspect.getsource(upstream_base.FinetunedTabICLBase._fit_impl)
    for required in ('iter_epoch_meta_batches(', 'count_chunks(', '_validate_current_model'):
        if required not in src:raise RuntimeError(f'Unsupported TabICL internal version: missing {required}')
    if mode=='chronological':
        from tabicl._finetune.data import MetaBatch, _build_ensemble_generator
        if not callable(_build_ensemble_generator):raise RuntimeError('Upstream ensemble preprocessor missing')
    train=np.asarray(train_mask,bool);valid=np.asarray(val_mask,bool)
    assert train.sum()>0 and valid.sum()>0
    assert np.max(h.semester_ordinal[train]) < np.min(h.semester_ordinal[valid])
    ht=h.subset(train);hv=h.subset(valid)
    if len(np.unique(ht.y))<2:raise ValueError('Historical fine-tuning labels are one class')
    episodes=[];skipped=[]
    # Build one common episode-count budget for BOTH methods: ordinary shuffled chunks
    # and genuinely chronological episodes receive the same number of optimizer steps.
    episodes,skipped=build_episodes(ht,np.ones(len(ht),bool),episode_support,episode_query,seed,release_gap)
    if not episodes:raise ValueError('No valid historical episodes; cannot construct a fair adaptation comparison')
    # Apply identical step cap to BOTH training conditions, independent of val/target AP.
    if len(episodes)>max_episodes:
        rng=np.random.default_rng(seed);keep=np.sort(rng.choice(len(episodes),size=max_episodes,replace=False));episodes=[episodes[i] for i in keep]
    if mode=='chronological':
        for ep in episodes:ep.verify(ht,release_gap)
        note('EPISODE',mode=mode,valid_count=len(episodes),skipped_count=len(skipped),skipped_reasons=skipped[:12],support_min=min(len(e.support) for e in episodes),support_max=max(len(e.support) for e in episodes),query_rows=sum(len(e.query) for e in episodes),faculty_count=len(set(e.faculty for e in episodes)),origin_count=len(set(e.origin for e in episodes)))
    steps_per_epoch=len(episodes)
    note('ADAPT MATCHING',mode=mode,optimizer_steps_per_epoch=steps_per_epoch,episode_support=episode_support,episode_query=episode_query,ordinary_query_ratio=episode_query/(episode_support+episode_query))
    out=Path(output);out.mkdir(parents=True,exist_ok=True)
    # The upstream fine-tuning base's internal validation uses its ROC-AUC by default.
    # Override it with OUR chronological macro-AP, with training-origin support only.
    class MacroAPFineTuner(FinetunedTabICLClassifier):
        def _compute_batch_loss(self, batch, model):
            loss=super()._compute_batch_loss(batch,model)
            if not hasattr(self,'_losses_for_epoch'):self._losses_for_epoch=[]
            self._losses_for_epoch.append(float(loss.detach().cpu()))
            return loss
        @property
        def _metric_name(self):return 'macro_ap'
        def _run_validation(self, inner, X_train, y_train, X_val, y_val):
            preds=np.full(len(y_val),np.nan,float);vrows=[]
            for fi in np.unique(hv.faculty_idx):
                m=hv.faculty_idx==fi
                for origin in np.sort(np.unique(hv.semester_ordinal[m])):
                    qmask=m&(hv.semester_ordinal==origin)
                    sp,_=support_select(ht,int(fi),float(origin),validation_support,seed,release_gap)
                    if len(sp)<2 or np.unique(y_train[sp]).size<2:
                        from .models import prior
                        preds[qmask]=prior(y_train[sp]);note('FALLBACK',stage='validation',faculty=fi,origin=origin,reason='single_class_support')
                    else:
                        # Validation is performed using the CURRENT in-memory network weights.
                        inner.fit(np.asarray(X_train[sp]),np.asarray(y_train[sp]))
                        pp=inner.predict_proba(np.asarray(X_val[qmask]));ci=int(np.flatnonzero(inner.classes_==1)[0]);preds[qmask]=pp[:,ci]
                    if np.unique(y_val[qmask]).size==2:vrows.append(scoring(y_val[qmask],preds[qmask])['ap'])
            ap=float(np.mean(vrows)) if vrows else float('nan')
            if not hasattr(self,'_validation_counter'):self._validation_counter=0
            step=self._validation_counter;self._validation_counter+=1
            ls=getattr(self,'_losses_for_epoch',[])
            loss_mean=float(np.mean(ls)) if ls else float('nan')
            row={'evaluation_number':step,'epoch':0 if step==0 else step,'mode':mode,'macro_ap':ap,'brier':scoring(y_val,preds)['brier'],'cells':len(vrows),'query_rows':len(y_val),'train_mean_batch_loss':loss_mean,'optimizer_steps':len(ls)}
            import pandas as pd
            curve=out/'selection_curve.csv';pd.DataFrame([row]).to_csv(curve,mode='a',header=not curve.exists(),index=False)
            self._losses_for_epoch=[]
            note('SELECT',mode=mode,epoch=step,macro_ap=ap,validation_cells=len(vrows),validation_rows=len(y_val),validation_brier=row['brier'],train_loss=loss_mean,optimizer_steps=len(ls))
            return ValidationMetrics(primary=ap,secondary={'brier':scoring(y_val,preds)['brier'],'eligible_cells':float(len(vrows))})
    # sklearn estimator class class definitions inside function can instantiate as usual.
    kw=dict(epochs=int(epochs),learning_rate=float(lr),weight_decay=float(weight_decay),grad_clip=float(grad_clip),amp=device.startswith('cuda'),n_estimators_finetune=int(n_estimators_train),n_estimators_validation=int(n_estimators_val),n_estimators_inference=int(n_estimators_infer),max_data_size=max(50,int(episode_support+episode_query)),finetune_ctx_query_ratio=float(episode_query/(episode_support+episode_query)),patience=int(patience),random_state=int(seed),device=device,verbose=True,save_interval=max(1,int(epochs)))
    if model_path:kw['model_path']=str(model_path)
    estimator=MacroAPFineTuner(**kw)
    note('ADAPT',mode=mode,epochs=epochs,train_rows=len(ht),val_rows=len(hv),actual_ep_per_epoch=len(episodes) if mode=='chronological' else 'random_upstream',checkpoint=out,model_path=model_path or 'TabICL_default')
    # Both modes go through the *same upstream gradient loop*. We patch only the
    # context/query generator and step counter, restoring them even on exceptions.
    # The ordinary control still delegates to the upstream RANDOM split iterator.
    old_iterator=upstream_base.iter_epoch_meta_batches;old_count=upstream_base.count_chunks
    def iterator(X,y,*,epoch_seed,preprocessing_seed,n_estimators,feat_shuffle_method,outlier_threshold,**options):
        if len(X)!=len(ht):raise RuntimeError('Upstream TabICL reordered rows: chronological index mapping unsafe')
        if mode=='ordinary':
            import itertools
            return itertools.islice(old_iterator(X,y,epoch_seed=epoch_seed,preprocessing_seed=preprocessing_seed,n_estimators=n_estimators,feat_shuffle_method=feat_shuffle_method,outlier_threshold=outlier_threshold,**options),steps_per_epoch)
        def time_ordered():
            for k,ep in enumerate(episodes):
                yield chronological_meta_batch(X,y,ep,n_estimators=n_estimators,seed=int(preprocessing_seed)+int(epoch_seed)+k,feature_shuffle=feat_shuffle_method,outlier_threshold=outlier_threshold)
        return time_ordered()
    def count(n,max_chunk_size,min_chunk_size=50,*,rank=0,world_size=1):
        if int(world_size)!=1:raise RuntimeError('Suite31 adaptation supports one GPU/process; do not use DDP')
        return steps_per_epoch
    upstream_base.iter_epoch_meta_batches=iterator;upstream_base.count_chunks=count
    try:estimator.fit(ht.current,ht.y,X_val=hv.current,y_val=hv.y,output_dir=out)
    finally:upstream_base.iter_epoch_meta_batches=old_iterator;upstream_base.count_chunks=old_count
    path=out/'best.ckpt'
    if not path.exists():raise FileNotFoundError('Upstream fine-tuning did not emit compatible best.ckpt')
    note('CHECKPOINT',mode=mode,path=path,bytes=path.stat().st_size,selected_epoch='see_checkpoint_metadata')
    return path
