from enum import Enum, auto
from typing import Dict, Any, Optional, List

class TaskStatus(str, Enum):
    """Enum for task status values"""
    PENDING = "pending"
    RUNNING = "running"
    COMPLETED = "completed"
    FAILED = "failed"
    TIMEOUT = "timeout"
    CANCELLED = "cancelled"
    NOT_FOUND = "not_found"

class AgentStatus:
    """Class to track agent execution status"""
    
    def __init__(self, name: str, total_agents: int = 3):
        """
        Initialize agent status
        
        Args:
            name: Name of the agent
            total_agents: Total number of agents in the pipeline
        """
        self.name = name
        self.status = TaskStatus.PENDING
        self.start_time = None
        self.end_time = None
        self.execution_time = None
        self.error = None
        self.total_agents = total_agents
        
    def to_dict(self) -> Dict[str, Any]:
        """Convert to dictionary"""
        return {
            "name": self.name,
            "status": self.status,
            "start_time": self.start_time,
            "end_time": self.end_time,
            "execution_time": self.execution_time,
            "error": self.error
        }

def calculate_progress(agent_statuses: List[AgentStatus], total_agents: int = 3) -> int:
    """
    Calculate progress percentage based on agent statuses
    
    Args:
        agent_statuses: List of agent statuses
        total_agents: Total number of agents in the pipeline
        
    Returns:
        int: Progress percentage (0-100)
    """
    if not agent_statuses:
        return 0
        
    # Define weights for different statuses
    status_weights = {
        TaskStatus.PENDING: 0,
        TaskStatus.RUNNING: 0.5,
        TaskStatus.COMPLETED: 1,
        TaskStatus.FAILED: 1,  # Consider failed as "done" for progress calculation
        TaskStatus.TIMEOUT: 1,
        TaskStatus.CANCELLED: 1
    }
    
    # Calculate weighted progress
    progress = 0
    for agent in agent_statuses:
        weight = status_weights.get(agent.status, 0)
        progress += weight * (100 / total_agents)
    
    return min(100, max(0, int(progress)))
