"""Fixed-learner selective borrowing with explicit, normalized donor mass."""
from __future__ import annotations
import time
import numpy as np
from sklearn.ensemble import HistGradientBoostingClassifier
from s32_data import stable_seed

class PriorModel:
    def __init__(self,p):self.p=float(p)
    def predict_proba(self,X):return np.repeat([[1-self.p,self.p]],len(X),axis=0)

def sampled(part,limit,seed):
    if len(part)<=limit or limit<=0:return part
    rng=np.random.default_rng(seed)
    # Uniform sampling in each source is deliberate: do not resample against its true prevalence.
    return part.sub(np.sort(rng.choice(len(part),int(limit),replace=False)))

def fit_weighted(data,target,origin,donors=(),rho=0.,representation='common',
                 mask_mode=None,max_donor_rows=4096,seed=42,max_iter=100,min_samples_leaf=20,
                 donor_weight_mode='equal',release_gap=0,cap_local=0):
    """Only rows strictly earlier than origin are used. Validated, faculty-normalized loss masses.

    donors is a list of (faculty, positive_weight). The mass rho is split by explicit weights.
    Model is trained FROM SCRATCH for this policy. Committee is never a model input.
    """
    if target not in data.history:raise KeyError(target)
    matrix,features=data_matrix_factory(data,target,representation,mask_mode)
    local=data.history[target]
    local=local.sub(local.semester < origin-release_gap)
    if cap_local and len(local)>cap_local:local=sampled(local,cap_local,stable_seed(seed,target,origin,'local'))
    if len(local)==0:raise ValueError(f'No local history for {target} origin {origin}')
    xs=[matrix(local)];ys=[local.y];faculty_sizes={target:len(local)}
    positives={target:int(local.y.sum())};ages={target:float(np.mean(origin-local.semester))}
    dw=[(s,float(w)) for s,w in donors if s!=target and s in data.history and float(w)>0]
    parts=[]
    available_source_rows={}
    for s,w in dw:
        ps=data.history[s];ps=ps.sub(ps.semester<origin-release_gap)
        available_source_rows[s]=len(ps)
        if max_donor_rows>0:ps=sampled(ps,max_donor_rows,stable_seed(seed,target,origin,s))
        if len(ps):parts.append((s,w,ps))
    if not parts:rho=0.
    masses=np.array([x[1] for x in parts],float)
    if len(masses):
        if donor_weight_mode=='size':masses=np.array([available_source_rows[x[0]] for x in parts],float)
        if not np.isfinite(masses).all() or masses.sum()<=0:raise ValueError('Invalid donor weights')
        masses=masses/masses.sum()
    # Multiplicative weight constant cancels in normalized loss; preserve local total mass.
    total_local=len(local)
    ws=[np.full(len(local),1-float(rho),float)]
    for (s,_,p),frac in zip(parts,masses):
        xs.append(matrix(p));ys.append(p.y)
        ws.append(np.full(len(p),float(rho)*total_local*frac/len(p),float))
        faculty_sizes[s]=len(p);positives[s]=int(p.y.sum());ages[s]=float(np.mean(origin-p.semester))
    X=np.vstack(xs);Y=np.concatenate(ys);W=np.concatenate(ws)
    if not np.isfinite(W).all() or (W<0).any():raise ValueError('Invalid weights')
    assert all(np.max(data.history[s].semester[data.history[s].semester<origin-release_gap])<origin-release_gap for s in faculty_sizes)
    model=(PriorModel(np.average(Y,weights=W)) if len(np.unique(Y[W>0]))<2 else
            HistGradientBoostingClassifier(loss='log_loss',learning_rate=.06,max_iter=max_iter,
              max_leaf_nodes=15,min_samples_leaf=min_samples_leaf,l2_regularization=1.,
              early_stopping=False,random_state=seed))
    start=time.monotonic()
    if not isinstance(model,PriorModel):model.fit(X,Y,sample_weight=W)
    elapsed=time.monotonic()-start
    train_p=np.asarray(model.predict_proba(X)[:,1],float)
    train_p=np.clip(train_p,1e-8,1-1e-8)
    losses=-(Y*np.log(train_p)+(1-Y)*np.log1p(-train_p))
    train_loss=float(np.average(losses,weights=W))
    train_local_loss=float(losses[:len(local)].mean())
    train_donor_loss=float(losses[len(local):].mean()) if len(Y)>len(local) else float('nan')
    from sklearn.metrics import average_precision_score
    train_local_ap=(float(average_precision_score(local.y,train_p[:len(local)])) if len(np.unique(local.y))==2 else float('nan'))
    mass_by_source={target:float(W[:len(local)].sum()/max(W.sum(),1e-12))}
    for (s,_,p),frac in zip(parts,masses):mass_by_source[s]=float(rho*frac)
    diag={'target':target,'origin':int(origin),'features':len(features),'representation':representation,
          'mask_mode':mask_mode or 'none','donor_names':[x[0] for x in parts],
          'rho':float(rho),'n_fit':len(Y),'n_local':len(local),'n_donor':len(Y)-len(local),
          'n_positive':int(Y.sum()),'weighted_positive_rate':float(np.average(Y,weights=W)),
          'source_mass':mass_by_source,'source_rows':faculty_sizes,'source_failures':positives,
          'source_mean_age':ages,'source_eligible_rows_before_cap':available_source_rows,
          'training_weighted_bce':train_loss,'training_local_bce':train_local_loss,
          'training_donor_bce':train_donor_loss,'training_local_ap_IN_SAMPLE':train_local_ap,
          'fitted_iterations':int(getattr(model,'n_iter_',0)),'weight_ess':float(W.sum()**2/max(np.sum(W**2),1e-12)),
          'positive_weight_ess':float((W[Y==1].sum()**2)/max(np.sum(W[Y==1]**2),1e-12)),
          'seconds':elapsed,'max_iter':max_iter,'learner':'HGB' if not isinstance(model,PriorModel) else 'prior_fallback'}
    return model,matrix,diag

def data_matrix_factory(data,target,representation,mask_mode):
    from s32_data import feature_schema
    if representation not in ('common','native'):raise ValueError(representation)
    if mask_mode not in (None,'observed','naive_zero','placebo'):raise ValueError(mask_mode)
    return feature_schema(data,target,representation,include_mask=mask_mode is not None,
                          mask_type=mask_mode or 'observed')

def predict(model,matrix,part):
    p=np.asarray(model.predict_proba(matrix(part))[:,1],float)
    if not np.isfinite(p).all():raise ValueError('Nonfinite prediction')
    return np.clip(p,1e-8,1-1e-8)
