"""Pipeline plugin registrations for model-independent detection nodes."""

from __future__ import annotations

from collections.abc import Mapping
from typing import Any

from cmvr_edge_ai.plugins import PluginKind, PluginRegistry, PluginSpec

from .operator import DetectionOperator
from .registry import DetectionModelRegistry
from .rules import DetectionTriggerOperator
from .video import VideoDecoderOperator


def register_detection_plugins(
    registry: PluginRegistry,
    model_registry: DetectionModelRegistry,
) -> None:
    def detector_factory(node_id: str, params: Mapping[str, Any]) -> DetectionOperator:
        return DetectionOperator(
            node_id,
            params,
            model_registry=model_registry,
        )

    registry.register(
        PluginSpec(
            plugin_id="media.video_decoder.pyav@1",
            kind=PluginKind.OPERATOR,
            factory=VideoDecoderOperator,
            inputs={"frames": "ImageFrame/v1"},
            outputs={"frames": "ImageFrame/v1"},
            description="Stateful H264/H265 decoder backed by optional PyAV",
            tags=frozenset({"media", "video", "decoder"}),
        )
    )
    registry.register(
        PluginSpec(
            plugin_id="detection.model@1",
            kind=PluginKind.OPERATOR,
            factory=detector_factory,
            inputs={"frames": "ImageFrame/v1"},
            outputs={"detections": "DetectionResult/v1"},
            description="Run a registered object-detection model",
            tags=frozenset({"ai", "detection", "model"}),
        )
    )
    registry.register(
        PluginSpec(
            plugin_id="detection.repeat_gate@1",
            kind=PluginKind.OPERATOR,
            factory=DetectionTriggerOperator,
            inputs={"detections": "DetectionResult/v1"},
            outputs={"alerts": "DetectionAlert/v1"},
            description="Emit alerts after configurable repeated frame hits",
            tags=frozenset({"detection", "rule", "temporal"}),
        )
    )


__all__ = ["register_detection_plugins"]
