"""Pipeline 数据模型"""

from enum import Enum
from typing import Any

from pydantic import BaseModel

from agentkit.orchestrator.retry import StepRetryPolicy


class StageStatus(str, Enum):
    PENDING = "pending"
    RUNNING = "running"
    COMPLETED = "completed"
    FAILED = "failed"
    SKIPPED = "skipped"


class PipelineStage(BaseModel):
    name: str
    agent: str
    action: str
    depends_on: list[str] = []
    inputs: dict[str, Any] = {}
    outputs: list[str] = []
    timeout_seconds: int = 300
    retry_count: int = 0
    continue_on_failure: bool = False
    condition: str | None = None
    retry_policy: StepRetryPolicy | None = None
    compensate: str | None = None

    model_config = {"arbitrary_types_allowed": True}


class Pipeline(BaseModel):
    name: str
    version: str
    description: str
    stages: list[PipelineStage]
    variables: dict[str, Any] = {}


class StageResult(BaseModel):
    stage_name: str
    status: StageStatus = StageStatus.PENDING
    output_data: dict[str, Any] | None = None
    error_message: str | None = None
    started_at: str | None = None
    completed_at: str | None = None


class PipelineResult(BaseModel):
    pipeline_name: str
    status: StageStatus = StageStatus.PENDING
    stage_results: dict[str, StageResult] = {}
    variables: dict[str, Any] = {}
    error_message: str | None = None
    metadata: dict[str, Any] = {}


class AdaptiveConfig(BaseModel):
    """Configuration for adaptive pipeline execution with reflection-replanning."""

    enabled: bool = False
    max_reflections: int = 3
    reflection_model: str = "default"
    skip_stages: list[str] = []

    model_config = {"arbitrary_types_allowed": True}


class ReflectionReport(BaseModel):
    """Structured report from pipeline reflection analysis."""

    failure_type: str  # input_error, resource_error, logic_error, timeout
    root_cause: str
    suggested_fix: str
    failed_stage: str
    reflection_number: int = 1
