"""Bounded differentiable reader proposal, followed by mandatory full regrowth.

This is a partial soft-subforest surrogate, not differentiation through greedy
hard split induction. Main-loss retrieval gradients are audited separately from
the training-only auxiliary heads.
"""
import math
import time
import numpy as np
import torch
from torch.nn import functional as F
from memory import summarize_torch,auxiliary_loss,retrieval_parameters,grad_norm,reader_fingerprint

ATTENTION_FEATURES={0,1,2,3,4,7,8,9,10}


def _has_attention(node,n_raw):
    if 'leaf' in node: return False
    return node['feature']-n_raw in ATTENTION_FEATURES or _has_attention(node['left'],n_raw) or _has_attention(node['right'],n_raw)


def soft_tree(tree,reader,provider,indices,batch=None,temperature=1.,cache=None):
    if temperature<=0: raise ValueError('Positive routing temperature required')
    indices=np.asarray(indices,dtype=np.int64); batch=provider.batch(indices) if batch is None else batch
    cache={} if cache is None else cache
    logits=cache.get('root_logits')
    if logits is None: logits=reader.root_logits(batch); cache['root_logits']=logits
    raw=torch.as_tensor(provider.raw[indices],device=provider.device)
    def visit(node):
        if 'leaf' in node: return torch.full((len(indices),),node['leaf'],device=provider.device)
        feature=node['feature']
        if feature<raw.shape[1]: value=raw[:,feature]
        else:
            path=tuple(tuple(p) for p in node['path'])
            if path not in cache:
                bias=torch.as_tensor(provider.path_bias(indices,path),device=provider.device)
                cache[path]=summarize_torch(batch,logits+bias)[0]
            value=cache[path][:,feature-raw.shape[1]]
        left=torch.sigmoid((node['threshold']-value)/max(temperature*node.get('soft_scale',1.),1e-3))
        return left*visit(node['left'])+(1-left)*visit(node['right'])
    return visit(tree)


def propose(reader,train_provider,forest,weights,epochs=1,max_rows=10000,batch_size=128,
            lr=.0002,max_trees=8,temperature=1.,seed=42,context=None):
    if epochs<0 or max_rows<1 or batch_size<1 or max_trees<1: raise ValueError('Invalid coadapt budget')
    forest._check(train_provider)
    selected=[t for t in forest.trees if _has_attention(t,len(forest.raw_names))][:max_trees]
    if not selected or epochs==0:
        record={**(context or {}),'status':'skipped_no_attention_splits' if not selected else 'skipped_zero_epochs',
                'selected_trees':len(selected),'reference_trees':len(forest.trees),'main_retrieval_grad_norm':0.}
        print('Reader proposal:',record,flush=True); return [record]
    rng=np.random.default_rng(seed); pool=rng.permutation(len(train_provider.raw))[:max_rows]
    reference=np.full(len(pool),forest.intercept,dtype=np.float64)
    chosen=np.zeros(len(pool),dtype=np.float64)
    for tree in forest.trees: reference+=forest.config.learning_rate*forest.tree_values(tree,train_provider,pool)
    for tree in selected: chosen+=forest.config.learning_rate*forest.tree_values(tree,train_provider,pool)
    fixed=torch.as_tensor(reference-chosen,dtype=torch.float32,device=train_provider.device)
    y=torch.as_tensor(train_provider.query.table.y[pool],dtype=torch.float32,device=train_provider.device)
    w=torch.as_tensor(np.asarray(weights,np.float32)[pool],device=train_provider.device)
    reader.to(train_provider.device); reader.train()
    optimizer=torch.optim.AdamW(reader.parameters(),lr=lr,weight_decay=1e-4)
    initial_hash=reader_fingerprint(reader); initial_retrieval=reader_fingerprint(reader,False)
    original=[p.detach().clone() for p in retrieval_parameters(reader)]; logs=[]
    for epoch in range(epochs):
        order=rng.permutation(len(pool)); sums={'main_loss':0.,'aux_loss':0.,'main_retrieval_grad_norm':0.,'total_retrieval_grad_norm':0.,'auxiliary_head_grad_norm':0.}; batches=0; start=time.perf_counter()
        for lo in range(0,len(order),batch_size):
            pos=order[lo:lo+batch_size]; idx=pool[pos]; batch=train_provider.batch(idx); cache={}
            soft=torch.zeros(len(pos),device=train_provider.device)
            for tree in selected: soft=soft+forest.config.learning_rate*soft_tree(tree,reader,train_provider,idx,batch,temperature,cache)
            main=(F.binary_cross_entropy_with_logits(fixed[pos]+soft,y[pos],reduction='none')*w[pos]).mean()
            gradients=torch.autograd.grad(main,retrieval_parameters(reader),retain_graph=True,allow_unused=True)
            main_norm=math.sqrt(sum(float(g.detach().square().sum()) for g in gradients if g is not None))
            evidence,attention=reader.evidence(batch)
            auxiliary,_=auxiliary_loss(reader,batch,w[pos],evidence,attention)
            loss=main+.2*auxiliary; optimizer.zero_grad(); loss.backward()
            sums['main_loss']+=float(main.detach()); sums['aux_loss']+=float(auxiliary.detach())
            sums['main_retrieval_grad_norm']+=main_norm; sums['total_retrieval_grad_norm']+=grad_norm(retrieval_parameters(reader))
            sums['auxiliary_head_grad_norm']+=grad_norm([p for n,p in reader.named_parameters() if 'head' in n])
            torch.nn.utils.clip_grad_norm_(reader.parameters(),2.); optimizer.step(); batches+=1
        record={**(context or {}),'status':'proposed_requires_complete_regrowth','epoch':epoch+1,
                'rows':len(pool),'selected_trees':len(selected),'reference_trees':len(forest.trees),
                **{k:v/max(batches,1) for k,v in sums.items()},'seconds':time.perf_counter()-start,
                'initial_reader_hash':initial_hash,'reader_hash':reader_fingerprint(reader),
                'initial_retrieval_hash':initial_retrieval,'retrieval_hash':reader_fingerprint(reader,False),
                'retrieval_parameter_delta':math.sqrt(sum(float((p.detach()-old).square().sum()) for p,old in zip(retrieval_parameters(reader),original)))}
        print('Reader proposal:',record,flush=True); logs.append(record)
    reader.eval(); return logs
