"""Memory-Interacting Neural Trees: prediction is a mixture of forest leaves.

Memory is read at internal nodes and changes their conditional thresholds. The
main label loss trains routing, feature choice, leaf values and memory together.
There is deliberately no standalone neural prediction head.
"""
from __future__ import annotations

import math
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F


class MemoryIntegratedForest(nn.Module):
    def __init__(self, blueprint, n_course_meta, n_courses, variant='mint',
                 hidden=32, ability_dim=16, dropout=.1):
        super().__init__()
        if variant not in {'tree_only', 'memory', 'mint'}:
            raise ValueError('variant must be tree_only, memory, or mint')
        self.n_features = int(blueprint['n_features'])
        self.n_trees = int(blueprint['n_trees'])
        self.depth = int(blueprint['depth'])
        self.n_nodes = 2 ** self.depth - 1
        self.n_leaves = 2 ** self.depth
        self.variant, self.hidden, self.ability_dim = variant, int(hidden), int(ability_dim)
        self.n_course_meta, self.n_courses = int(n_course_meta), int(n_courses)
        self.dropout_rate = float(dropout)
        # Every variant instantiates every parameter, in the same order. With the
        # same RNG seed, their initial shared parameters are exactly identical.
        selectors = torch.zeros(self.n_trees, self.n_nodes, self.n_features)
        selectors.scatter_(-1, torch.as_tensor(blueprint['feature_index']).unsqueeze(-1), 4.)
        self.selector_logits = nn.Parameter(selectors)
        self.thresholds = nn.Parameter(torch.tensor(blueprint['thresholds'], dtype=torch.float32))
        p = torch.tensor(blueprint['leaf_prob'], dtype=torch.float32).clamp(1e-5, 1 - 1e-5)
        self.leaf_logits = nn.Parameter(torch.logit(p))
        init_tau = (.25 - .03) / (2. - .03)
        self.temperature_unconstrained = nn.Parameter(torch.full_like(self.thresholds, math.log(init_tau / (1 - init_tau))))
        self.course_embedding = nn.Embedding(n_courses, hidden, padding_idx=0)
        self.course_metadata = nn.Linear(n_course_meta, hidden)
        self.course_norm = nn.LayerNorm(hidden)
        self.current_state = nn.Linear(self.n_features, hidden)
        self.history_state = nn.Linear(self.n_features, hidden)
        self.feature_embedding = nn.Parameter(torch.randn(self.n_features, hidden) * .1)
        self.node_embedding = nn.Parameter(torch.randn(self.n_trees, self.n_nodes, hidden) * .1)
        self.margin_query = nn.Linear(1, hidden, bias=False)
        self.path_query = nn.Linear(1, hidden, bias=False)
        self.memory_key = nn.Linear(2 * hidden + 2, hidden)
        self.memory_value = nn.Linear(2 * hidden + 6, hidden)
        self.value_norm = nn.LayerNorm(hidden)
        self.dropout = nn.Dropout(dropout)
        self.node_static_weight = nn.Parameter(torch.zeros(self.n_trees, self.n_nodes, hidden))
        self.node_memory_weight = nn.Parameter(torch.randn(self.n_trees, self.n_nodes, hidden) * .02)
        self.skill_assignment = nn.Linear(hidden, ability_dim)
        self.course_requirement = nn.Linear(hidden, ability_dim)
        self.course_importance = nn.Linear(hidden, ability_dim)
        self.node_gap_weight = nn.Parameter(torch.randn(self.n_trees, self.n_nodes, ability_dim) * .1)
        self.register_buffer('seed_features', torch.as_tensor(blueprint['feature_index'], dtype=torch.long))
        self.register_buffer('seed_thresholds', torch.as_tensor(blueprint['thresholds'], dtype=torch.float32))
        self.register_buffer('seed_active', torch.as_tensor(blueprint['active_nodes'], dtype=torch.bool))

    def config(self):
        return dict(n_course_meta=self.n_course_meta, n_courses=self.n_courses,
                    variant=self.variant, hidden=self.hidden, ability_dim=self.ability_dim,
                    dropout=self.dropout_rate)

    def _course(self, ids, metadata):
        return self.course_norm(self.course_embedding(ids) + self.course_metadata(metadata))

    def forward(self, batch, intervention=None, return_details=False):
        flags = set() if intervention is None else ({intervention} if isinstance(intervention, str) else set(intervention))
        unknown = flags - {'no_memory', 'no_outcomes', 'no_ability', 'uniform_attention', 'hard_routing', 'no_context'}
        if unknown:
            raise ValueError(f'Unknown intervention: {unknown}')
        x = batch['x']
        b, hist_len = x.shape[0], batch['h_mask'].shape[1]
        mask = batch['h_mask'].bool()
        has_history = mask.any(-1).to(x.dtype)
        course = self._course(batch['course_id'], batch['course_meta'])
        state = torch.tanh(self.current_state(x))
        context = torch.tanh(state + course)
        hist_course = self._course(batch['h_course_id'], batch['h_meta'])
        hist_state = torch.tanh(self.history_state(batch['h_x']))
        recency = batch['h_recency'].clamp_min(0)
        age = torch.stack((torch.log1p(recency), torch.exp(-recency / 3.)), -1)
        hist_key_input = torch.cat((hist_state, hist_course, age), -1)
        keys = F.normalize(self.memory_key(hist_key_input), dim=-1)
        outcome_known = mask.to(x.dtype) if 'no_outcomes' not in flags else torch.zeros_like(batch['h_y'])
        grade_known = batch['h_grade_present'] * outcome_known
        fail = batch['h_y'] * outcome_known
        grade = (batch['h_grade'] - .5) * grade_known
        values_in = torch.cat((hist_state, hist_course, age, fail.unsqueeze(-1),
                               grade.unsqueeze(-1), grade_known.unsqueeze(-1), outcome_known.unsqueeze(-1)), -1)
        values = self.dropout(torch.tanh(self.value_norm(self.memory_value(values_in))))
        # History-only course associations make theta independent of the target.
        positive_weights = F.softplus(self.skill_assignment(hist_course)) * outcome_known.unsqueeze(-1)
        performance = torch.where(batch['h_grade_present'].bool(), batch['h_grade'], 1 - batch['h_y'])
        performance = performance * outcome_known
        prior_strength = 2.
        readiness = (prior_strength * .5 + (positive_weights * performance.unsqueeze(-1)).sum(1)) / (prior_strength + positive_weights.sum(1))
        requirement = torch.sigmoid(self.course_requirement(course))
        importance = torch.softmax(self.course_importance(course), dim=-1)
        gap = importance * (requirement - readiness)
        gap_score = gap.sum(-1)
        use_history = self.variant in {'memory', 'mint'} and not flags.intersection({'no_memory', 'no_context'})
        use_gap = self.variant == 'mint' and not flags.intersection({'no_memory', 'no_ability', 'no_context', 'no_outcomes'})
        # Hard forward feature choice; softmax is a surrogate gradient, not an
        # exact derivative of discrete argmax. Normal inference also uses argmax.
        soft = torch.softmax(self.selector_logits, -1)
        hard = F.one_hot(soft.argmax(-1), self.n_features).to(soft.dtype)
        selectors = hard + (soft - soft.detach())
        selected = torch.einsum('bf,tnf->btn', x, selectors)
        feature_id = torch.einsum('tnf,fh->tnh', selectors, self.feature_embedding)
        temperatures = .03 + 1.97 * torch.sigmoid(self.temperature_unconstrained)
        reach = x.new_ones(b, self.n_trees, 1)
        agg_attention = x.new_zeros(b, hist_len)
        entropy = x.new_zeros(b)
        static_abs, memory_abs, gap_abs = (x.new_zeros(b) for _ in range(3))
        threshold_penalty = x.new_zeros(b)
        routes, effective_thresholds = [], []
        for depth in range(self.depth):
            start, stop = 2 ** depth - 1, 2 ** (depth + 1) - 1
            margin = selected[:, :, start:stop] - self.thresholds[None, :, start:stop]
            query = (context[:, None, None, :] + self.node_embedding[None, :, start:stop, :]
                     + feature_id[None, :, start:stop, :] + self.margin_query(margin.unsqueeze(-1))
                     + self.path_query(torch.log(reach.clamp_min(1e-8)).unsqueeze(-1)))
            static_shift = 2 * torch.tanh(torch.einsum('bh,tmh->btm', context, self.node_static_weight[:, start:stop]))
            if 'no_context' in flags:
                static_shift = torch.zeros_like(static_shift)
            memory_shift = torch.zeros_like(static_shift)
            gap_shift = torch.zeros_like(static_shift)
            if use_history:
                similarity = 8 * torch.einsum('btmh,bsh->btms', F.normalize(query, dim=-1), keys)
                if 'uniform_attention' in flags:
                    similarity = torch.zeros_like(similarity)
                similarity = similarity.masked_fill(~mask[:, None, None, :], -1e4)
                attention = torch.softmax(similarity, dim=-1) * mask[:, None, None, :].to(x.dtype)
                attention = attention / attention.sum(-1, keepdim=True).clamp_min(1e-12)
                retrieved = torch.einsum('btms,bsh->btmh', attention, values)
                memory_shift = 2 * torch.tanh(torch.einsum('btmh,tmh->btm', retrieved, self.node_memory_weight[:, start:stop]))
                memory_shift = memory_shift * has_history[:, None, None]
                agg_attention = agg_attention + (attention * reach.unsqueeze(-1)).sum((1, 2)) / (self.n_trees * self.depth)
            if use_gap:
                gap_shift = 2 * torch.tanh(torch.einsum('bk,tmk->btm', gap, self.node_gap_weight[:, start:stop]))
                gap_shift = gap_shift * has_history[:, None, None]
            threshold = self.thresholds[None, :, start:stop] + static_shift + memory_shift + gap_shift
            logits = (selected[:, :, start:stop] - threshold) / temperatures[None, :, start:stop]
            right = (logits > 0).to(x.dtype) if 'hard_routing' in flags else torch.sigmoid(logits)
            safe = right.clamp(1e-7, 1 - 1e-7)
            routing_entropy = -(safe * safe.log() + (1 - safe) * torch.log1p(-safe))
            denom = self.n_trees * self.depth
            entropy = entropy + (reach * routing_entropy).sum((1, 2)) / denom
            static_abs = static_abs + (reach * static_shift.abs()).sum((1, 2)) / denom
            memory_abs = memory_abs + (reach * memory_shift.abs()).sum((1, 2)) / denom
            gap_abs = gap_abs + (reach * gap_shift.abs()).sum((1, 2)) / denom
            threshold_penalty = threshold_penalty + (reach * (memory_shift.square() + gap_shift.square())).sum((1, 2)) / denom
            if return_details:
                routes.append(right)
                effective_thresholds.append(threshold)
            reach = torch.stack((reach * (1 - right), reach * right), -1).flatten(-2)
        tree_prob = (reach * torch.sigmoid(self.leaf_logits)[None]).sum(-1)
        prob = tree_prob.mean(-1).clamp(1e-7, 1 - 1e-7)
        result = dict(prob=prob, tree_prob=tree_prob, attention=agg_attention,
                      route_entropy=entropy, memory_shift_abs=memory_abs,
                      static_shift_abs=static_abs, gap_shift_abs=gap_abs,
                      readiness=readiness, requirement=requirement, gap_score=gap_score,
                      threshold_penalty=threshold_penalty)
        if return_details:
            result.update(routes=torch.cat(routes, -1), leaf_mass=reach,
                          effective_thresholds=torch.cat(effective_thresholds, -1))
        return result

    @torch.no_grad()
    def structural_diagnostics(self):
        active = self.seed_active
        changed = self.selector_logits.argmax(-1) != self.seed_features
        delta = (self.thresholds - self.seed_thresholds).abs()
        return dict(feature_changed_fraction=float(changed.float().mean()),
                    seed_active_feature_changed_fraction=float(changed[active].float().mean()) if active.any() else 0.,
                    mean_base_threshold_change=float(delta.mean()),
                    max_base_threshold_change=float(delta.max()),
                    mean_temperature=float((.03 + 1.97 * self.temperature_unconstrained.sigmoid()).mean()),
                    parameter_count=sum(p.numel() for p in self.parameters()))
