# app/src/trading/market_intelligence/coordinator/validation.py

from __future__ import annotations

from src.trading.market_intelligence.common.models import RuntimeResult
from src.trading.market_intelligence.coordinator.exceptions import (
    CoordinatorValidationError,
)


class CoordinatorValidation:
    """Проверка входного RuntimeResult для Coordinator."""

    def validate(
        self,
        runtime_result: RuntimeResult,
    ) -> None:
        """Проверить RuntimeResult перед координацией."""
        self._validate_runtime_result_exists(runtime_result)
        self._validate_runtime_has_results(runtime_result)
        self._validate_runtime_successful(runtime_result)

    def _validate_runtime_result_exists(
        self,
        runtime_result: RuntimeResult,
    ) -> None:
        """Проверить, что RuntimeResult передан."""
        if runtime_result is None:
            raise CoordinatorValidationError(
                "RuntimeResult is required for Coordinator."
            )

    def _validate_runtime_has_results(
        self,
        runtime_result: RuntimeResult,
    ) -> None:
        """Проверить наличие результатов Engine."""
        if runtime_result.total_engines <= 0:
            raise CoordinatorValidationError(
                "RuntimeResult does not contain Engine results."
            )

    def _validate_runtime_successful(
        self,
        runtime_result: RuntimeResult,
    ) -> None:
        """Проверить отсутствие критических ошибок Runtime."""
        if not runtime_result.is_successful:
            raise CoordinatorValidationError(
                "RuntimeResult contains failed Engine results."
            )