from datetime import datetime, timezone
from enum import Enum

from pydantic import BaseModel, Field

from harbor.models.trial.config import TrialConfig
from harbor.models.trial.result import TrialResult


class TrialEvent(Enum):
    """Events in a trial's lifecycle."""

    START = "start"
    ENVIRONMENT_START = "environment_start"
    AGENT_START = "agent_start"
    VERIFICATION_START = "verification_start"
    END = "end"
    CANCEL = "cancel"
    # Backward compatibility alias (OrchestratorEvent.TRIAL_COMPLETED)
    TRIAL_COMPLETED = "end"


class TrialHookEvent(BaseModel):
    """
    Unified event object passed to all trial lifecycle hooks.

    Provides context about the trial at the time of the event.
    The `result` field is only populated for END events.
    """

    model_config = {"arbitrary_types_allowed": True}

    event: TrialEvent
    trial_id: str
    task_name: str
    config: TrialConfig
    timestamp: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
    result: TrialResult | None = None
