"""Panorama benchmark processor."""

from .base import BenchmarkProcessor


class PanoramaProcessor(BenchmarkProcessor):
    """Panorama: Different sub-datasets use different metrics."""

    CORE_METRICS = {
        "par4pc": "macro_f1",
        "pi4pc": "gold_hit_rate",
        "noc4pc": "macro_f1",
    }

    @classmethod
    def match(cls, benchmark_name: str) -> bool:
        return "panorama" in benchmark_name.lower()

    @classmethod
    def get_core_metric(cls, accuracy_summary: dict) -> tuple[str, float, bool] | None:
        scores = []
        metrics_used = []

        for ds, metrics in accuracy_summary.items():
            if not isinstance(metrics, dict):
                continue
            ds_lower = ds.lower()
            # Find matching core metric
            core_metric = "accuracy"  # fallback
            for pattern, metric in cls.CORE_METRICS.items():
                if pattern in ds_lower:
                    core_metric = metric
                    break

            if core_metric in metrics:
                scores.append(float(metrics[core_metric]))
                metrics_used.append(core_metric)

        if not scores:
            return None

        avg = sum(scores) / len(scores)
        unique = list(set(metrics_used))

        if len(scores) == 1:
            metric_name = unique[0]
        elif len(unique) == 1:
            metric_name = f"{unique[0]} (average)"
        else:
            metric_name = "mixed (average)"

        return (metric_name, avg, cls.is_higher_better(metric_name))
