"""Shared ActionTracker status helpers for prediction flows."""

from __future__ import annotations

from contextlib import suppress
from dataclasses import dataclass
from typing import Any


def safe_update_status(action_tracker: Any, step_code: str, status: str, description: str) -> None:
    """Best-effort status update that never raises to caller."""
    with suppress(Exception):
        if hasattr(action_tracker, "update_status"):
            action_tracker.update_status(step_code, status, description)


def safe_log_error(action_tracker: Any, file_path: str, location: str, message: str) -> None:
    """Best-effort ActionTracker error logging."""
    with suppress(Exception):
        if hasattr(action_tracker, "log_error"):
            action_tracker.log_error(file_path, location, message)


@dataclass(frozen=True)
class PredictionStatusCodes:
    """Canonical prediction-related status step codes."""

    LOAD_ERROR: str = "MDL_PRED_ERR"
    PREDICT_ERROR: str = "MDL_PREDICT_ERR"


class PredictionStatusReporter:
    """Semantic status reporter for prediction error transitions."""

    def __init__(self, action_tracker: Any, codes: PredictionStatusCodes | None = None) -> None:
        """Create a reporter backed by an ActionTracker-like object."""
        self.action_tracker = action_tracker
        self.codes = codes or PredictionStatusCodes()

    def model_load_failed(self, msg: str = "Error in loading model") -> None:
        """Emit model-load failure status."""
        safe_update_status(self.action_tracker, self.codes.LOAD_ERROR, "ERROR", msg)

    def prediction_failed(self, msg: str = "Error during prediction") -> None:
        """Emit prediction failure status."""
        safe_update_status(self.action_tracker, self.codes.PREDICT_ERROR, "ERROR", msg)
