"""
Supervisor Models for Breakpilot Agents

Data classes and enumerations for agent lifecycle management.
"""

from typing import Dict, Optional, Any, Callable, Awaitable
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum


class AgentStatus(Enum):
    """Agent lifecycle states"""
    INITIALIZING = "initializing"
    STARTING = "starting"
    RUNNING = "running"
    PAUSED = "paused"
    STOPPING = "stopping"
    STOPPED = "stopped"
    ERROR = "error"
    RESTARTING = "restarting"


class RestartPolicy(Enum):
    """Agent restart policies"""
    NEVER = "never"
    ON_FAILURE = "on_failure"
    ALWAYS = "always"


@dataclass
class AgentInfo:
    """Information about a registered agent"""
    agent_id: str
    agent_type: str
    status: AgentStatus = AgentStatus.INITIALIZING
    current_task: Optional[str] = None
    started_at: Optional[datetime] = None
    last_activity: Optional[datetime] = None
    error_count: int = 0
    restart_count: int = 0
    max_restarts: int = 3
    restart_policy: RestartPolicy = RestartPolicy.ON_FAILURE
    metadata: Dict[str, Any] = field(default_factory=dict)
    capacity: int = 10  # Max concurrent tasks
    current_load: int = 0

    def is_healthy(self) -> bool:
        """Check if agent is healthy"""
        return self.status == AgentStatus.RUNNING and self.error_count < 3

    def is_available(self) -> bool:
        """Check if agent can accept new tasks"""
        return (
            self.status == AgentStatus.RUNNING and
            self.current_load < self.capacity
        )

    def utilization(self) -> float:
        """Returns agent utilization (0-1)"""
        return self.current_load / max(self.capacity, 1)


AgentFactory = Callable[[str], Awaitable[Any]]
