# pylint:disable=unnecessary-lambda
from typing import Dict, List, Any
from pydantic import BaseModel, Field
from mini_videorag.core.processor import ProcessorResult


class WorkflowProgress(BaseModel):
    """Progress information from a running workflow query."""

    total_processors: int = Field(..., ge=0, description="Total number of processors to execute")
    completed_processors: int = Field(..., ge=0, description="Number of completed processors")
    current_processor: str = Field(default="", description="Name of currently executing processor")
    progress_pct: float = Field(..., ge=0.0, le=100.0, description="Progress percentage (0-100)")
    completed_processor_names: List[str] = Field(
        default_factory=list, description="List of completed processor names"
    )


class WorkflowResult(BaseModel):
    """Final result from completed video pipeline workflow."""

    video_id: str = Field(..., description="Unique identifier for the processed video")
    results: Dict[str, ProcessorResult] = Field(
        default_factory=dict, description="Map of processor names to their results"
    )
    total_processors: int = Field(..., ge=0, description="Total number of processors executed")
    completed_processors: int = Field(
        ..., ge=0, description="Number of processors that completed (success or failure)"
    )

    class Config:
        arbitrary_types_allowed = True  # Allow ProcessorResult


class TemporalActivityConfig(BaseModel):
    """Configuration for Temporal activity execution.

    Public API - intended for config loader integration.
    """

    start_to_close_timeout_minutes: int = Field(
        default=30, gt=0, description="Maximum time for activity to complete (minutes)"
    )
    heartbeat_timeout_minutes: int = Field(
        default=15, gt=0, description="Maximum time between heartbeats (minutes)"
    )
    retry: "RetryConfig" = Field(
        default_factory=lambda: RetryConfig(), description="Retry policy configuration"
    )
    processor_overrides: Dict[str, Dict[str, Any]] = Field(
        default_factory=dict,
        description="Per-processor timeout/retry overrides (processor_name -> config)",
    )


class RetryConfig(BaseModel):
    """Retry policy configuration for Temporal activities.

    Public API - intended for config loader integration.
    """

    initial_interval_seconds: int = Field(
        default=10, gt=0, description="Initial retry interval in seconds"
    )
    maximum_interval_minutes: int = Field(
        default=5, gt=0, description="Maximum retry interval in minutes"
    )
    maximum_attempts: int = Field(default=3, gt=0, description="Maximum number of retry attempts")
    non_retryable_errors: List[str] = Field(
        default_factory=lambda: ["ModelInitError", "VideoLoadError"],
        description="Error types that should not be retried",
    )


class TemporalConfig(BaseModel):
    """Complete Temporal workflow configuration.

    Public API - intended for config loader integration.
    """

    activity: TemporalActivityConfig = Field(
        default_factory=TemporalActivityConfig, description="Activity execution settings"
    )
    workflow: Dict[str, Any] = Field(
        default_factory=dict, description="Workflow-level settings (execution timeout, etc.)"
    )

    @classmethod
    def from_dict(cls, config: Dict[str, Any]) -> "TemporalConfig":
        """
        Create config from dict (e.g., from temporal section in YAML).

        Args:
            config: Config dict with temporal settings

        Returns:
            TemporalConfig instance
        """
        return cls.model_validate(config)
