"""Pluggable hook for publishing agent run artifacts after each run.

Framework stays free of infrastructure packages. Infra registers a publisher
via ``set_run_artifact_publisher`` (see agent_logging under the infra package).
"""

from __future__ import annotations

from collections.abc import Callable
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from supply_agent.logging.logger import AgentLogger

RunArtifactPublisher = Callable[["AgentLogger"], str | None]

_publisher: RunArtifactPublisher | None = None


def set_run_artifact_publisher(publisher: RunArtifactPublisher | None) -> None:
    """Install or clear the post-run artifact publisher callback."""
    global _publisher
    _publisher = publisher


def get_run_artifact_publisher() -> RunArtifactPublisher | None:
    return _publisher


def publish_run_artifacts(agent_logger: AgentLogger) -> str | None:
    """
    Invoke the registered publisher, if any.

    Returns the public artifact URL, or None when no publisher is registered
    or publishing is skipped / failed.
    """
    if _publisher is None:
        return None
    return _publisher(agent_logger)
