"""Objective for the image-decoding track (track 1).

Decode which image a subject viewed from a single EEG epoch, evaluated by
**retrieval**: a submission's model maps torch batches ``(B, C, T)`` to
predicted image *embeddings* (``predict(X) -> (B, D)``, matching the target
embedding space, e.g. DINOv2). Each test window's prediction is ranked
against the candidate pool (the unique target embeddings of the test split)
by cosine similarity.

The objective exposes two phase-specific Top-5 metrics (with Top-1 reported
alongside): warm-up uses retrieval pooled over all test windows, while sealed
evaluation uses the ``*_subject_agg`` variant, which averages predictions of
repeated presentations of each image *within a subject* before retrieval and
then pools the resulting (subject, image) queries (NeuralBench's
``top-k_subject-agg``). The active Codabench phase selects the ranking key.
Data flows as lazy dataloaders — see ``benchmark_utils/data.py``; targets ``y``
are the float embeddings ``(B, D)`` of the viewed images.
"""

import numpy as np
from benchopt import BaseObjective

from benchmark_utils.data import subject_ids, to_numpy
from benchmark_utils.metrics import group_means, topk_accuracy


def _normalize(v):
    return v / np.maximum(np.linalg.norm(v, axis=-1, keepdims=True), 1e-12)


class Objective(BaseObjective):

    name = "Image-decoding"
    url = "https://github.com/neural-interfaces26/2026-competition"

    # CPU/GPU variants resolved by ``benchopt install [--gpu]``; the conda
    # metapackages pin the matching torch build (CI installs the cpu one).
    # Install the full trio from one channel: mixing a conda torch with pip
    # torchvision/torchaudio breaks operators such as torchvision::nms.
    requirements = {
        "cpu": ["scikit-learn", "pytorch-cpu", "torchvision", "torchaudio"],
        "gpu": ["scikit-learn", "pytorch-gpu", "torchvision", "torchaudio"],
    }

    min_benchopt_version = "1.10.0"

    # Each solver runs once to completion (no convergence curve).
    sampling_strategy = "run_once"

    # training=True lets the solvers' optional ``fit`` run before evaluation
    # (how baselines and participants train); default runs are
    # inference-only, like the platform. Select it with
    #     benchopt run ... -o "<objective>[training=True]"
    parameters = {"training": [False]}

    # ``benchopt test`` exercises the full contract, training included.
    test_config = {"training": True}

    def set_data(self, train_loader, test_loader, n_outputs, **meta):
        self.train_loader = train_loader
        self.test_loader = test_loader
        self.n_outputs = n_outputs  # embedding dimension D
        self.meta = meta  # sfreq, ch_names, chs_info, n_chans, n_times, ...

    def skip(self, train_loader=None, **data):
        if self.training and train_loader is None:
            return True, "training=True needs a dataset with a train split"
        return False, None

    def get_objective(self):
        # Train loader goes to the solver; the test loader stays here so the
        # objective owns evaluation.
        return dict(
            # Inference-only unless training is selected: no loader, no fit.
            train_loader=self.train_loader if self.training else None,
            n_outputs=self.n_outputs,
            **self.meta,
        )

    def evaluate_result(self, model):
        y_true, y_pred, subject = [], [], []
        for X, y, info in self.test_loader:
            pred = to_numpy(model.predict(X))
            y_pred.append(pred)
            y_true.append(to_numpy(y))
            subject.append(subject_ids(info, len(pred)))
        y_true = np.concatenate(y_true)          # (N, D)
        y_pred = np.concatenate(y_pred)          # (N, D)
        subject = np.concatenate(subject)        # (N,)

        # Candidate gallery: the unique target embeddings of the test split
        # (repeated presentations of one image share its embedding).
        candidates, target_idx = np.unique(
            y_true, axis=0, return_inverse=True
        )
        scores = _normalize(y_pred) @ _normalize(candidates).T  # (N, M)

        # Subject-aggregated retrieval (neuralbench top-k_subject-agg): average
        # the predictions of the repeated presentations of each image *within a
        # subject*, then pool those (subject, image) queries and rank them
        # against the gallery.
        keys, pred_sa = group_means(
            y_pred, np.stack([subject, target_idx.reshape(-1)], axis=1)
        )
        sa_idx = keys[:, 1]  # gallery (image) index of each query
        scores_sa = _normalize(pred_sa) @ _normalize(candidates).T

        return dict(
            top5_acc=topk_accuracy(scores, target_idx, k=5),
            top1_acc=topk_accuracy(scores, target_idx, k=1),
            top5_acc_subject_agg=topk_accuracy(scores_sa, sa_idx, k=5),
            top1_acc_subject_agg=topk_accuracy(scores_sa, sa_idx, k=1),
            n_candidates=int(len(candidates)),
        )

    def get_one_result(self):
        # A trivial constant model, used by ``benchopt test`` to validate the
        # metric computation.
        from benchmark_utils.baselines import ConstantEmbedder
        return dict(model=ConstantEmbedder(self.n_outputs))
