"""Workflow schema - extends Pipeline with workflow-specific fields"""

from __future__ import annotations

from datetime import datetime, timezone
from typing import Any

from pydantic import BaseModel, Field

from agentkit.orchestrator.pipeline_schema import Pipeline, PipelineStage


class WorkflowStage(PipelineStage):
    """A workflow stage extending PipelineStage with type and config."""

    type: str = "skill"  # "skill" | "condition" | "approval" | "parallel"
    config: dict[str, Any] = Field(default_factory=dict)


class WorkflowDefinition(BaseModel):
    """Workflow definition extending Pipeline with workflow-specific fields."""

    workflow_id: str = ""
    name: str
    version: int = 1
    stages: list[WorkflowStage] = Field(default_factory=list)
    triggers: list[dict[str, Any]] = Field(default_factory=list)
    variables_schema: dict[str, Any] = Field(default_factory=dict)
    output_schema: dict[str, Any] = Field(default_factory=dict)
    created_at: str = Field(default_factory=lambda: datetime.now(timezone.utc).isoformat())
    updated_at: str = Field(default_factory=lambda: datetime.now(timezone.utc).isoformat())


class WorkflowExecution(BaseModel):
    """Runtime state of a workflow execution."""

    execution_id: str = ""
    workflow_id: str = ""
    status: str = "pending"  # pending|running|paused|completed|failed|cancelled
    current_stage: str | None = None
    stage_results: dict[str, Any] = Field(default_factory=dict)
    started_at: str | None = None
    completed_at: str | None = None
    error: str | None = None
    variables: dict[str, Any] = Field(default_factory=dict)


class WorkflowSummary(BaseModel):
    """Summary for listing workflows."""

    workflow_id: str
    name: str
    version: int
    stage_count: int
    trigger_count: int
    created_at: str
    updated_at: str


class CreateWorkflowRequest(BaseModel):
    """Request body for creating/updating a workflow."""

    name: str
    stages: list[WorkflowStage] = Field(default_factory=list)
    triggers: list[dict[str, Any]] = Field(default_factory=list)
    variables_schema: dict[str, Any] = Field(default_factory=dict)
    output_schema: dict[str, Any] = Field(default_factory=dict)


class ExecuteWorkflowRequest(BaseModel):
    """Request body for executing a workflow."""

    variables: dict[str, Any] = Field(default_factory=dict)


class ApproveRequest(BaseModel):
    """Request body for approving a paused approval node."""

    approved: bool
    comment: str | None = None
