use fabro_agent::{AgentEvent, SandboxEvent};

use super::Event;

#[must_use]
pub fn event_name(event: &Event) -> &'static str {
    match event {
        Event::RunCreated { .. } => "run.created",
        Event::WorkflowRunStarted { .. } => "run.started",
        Event::RunSubmitted { .. } => "run.submitted",
        Event::RunStartRequested { .. } => "run.start_requested",
        Event::RunPending { .. } => "run.pending",
        Event::RunApproved { .. } => "run.approved",
        Event::RunDenied { .. } => "run.denied",
        Event::RunRunnable { .. } => "run.runnable",
        Event::RunStarting => "run.starting",
        Event::RunRunning => "run.running",
        Event::RunInterrupt { .. } => "run.interrupt",
        Event::RunSteer { .. } => "run.steer",
        Event::RunPairStarted { .. } => "run.pair.started",
        Event::RunPairEnded { .. } => "run.pair.ended",
        Event::RunPairFailed { .. } => "run.pair.failed",
        Event::RunBlocked { .. } => "run.blocked",
        Event::RunUnblocked => "run.unblocked",
        Event::RunRemoving => "run.removing",
        Event::RunCancelRequested { .. } => "run.cancel.requested",
        Event::RunPauseRequested { .. } => "run.pause.requested",
        Event::RunUnpauseRequested { .. } => "run.unpause.requested",
        Event::RunPaused => "run.paused",
        Event::RunUnpaused => "run.unpaused",
        Event::RunSupersededBy { .. } => "run.superseded_by",
        Event::RunArchived { .. } => "run.archived",
        Event::RunUnarchived { .. } => "run.unarchived",
        Event::RunTitleUpdated { .. } => "run.title.updated",
        Event::RunParentLinked { .. } => "run.parent.linked",
        Event::RunParentUnlinked { .. } => "run.parent.unlinked",
        Event::WorkflowRunCompleted { .. } => "run.completed",
        Event::WorkflowRunFailed { .. } => "run.failed",
        Event::RunNotice { .. } => "run.notice",
        Event::MetadataSnapshotStarted { .. } => "metadata.snapshot.started",
        Event::MetadataSnapshotCompleted { .. } => "metadata.snapshot.completed",
        Event::MetadataSnapshotFailed { .. } => "metadata.snapshot.failed",
        Event::StageStarted { .. } => "stage.started",
        Event::StageCompleted { .. } => "stage.completed",
        Event::StageFailed { .. } => "stage.failed",
        Event::StageRetrying { .. } => "stage.retrying",
        Event::ParallelStarted { .. } => "parallel.started",
        Event::ParallelBranchStarted { .. } => "parallel.branch.started",
        Event::ParallelBranchCompleted { .. } => "parallel.branch.completed",
        Event::ParallelCompleted { .. } => "parallel.completed",
        Event::InterviewStarted { .. } => "interview.started",
        Event::InterviewCompleted { .. } => "interview.completed",
        Event::InterviewTimeout { .. } => "interview.timeout",
        Event::InterviewInterrupted { .. } => "interview.interrupted",
        Event::CheckpointCompleted { .. } => "checkpoint.completed",
        Event::CheckpointFailed { .. } => "checkpoint.failed",
        Event::GitCommit { .. } => "git.commit",
        Event::GitPush { .. } => "git.push",
        Event::GitFetch { .. } => "git.fetch",
        Event::GitReset { .. } => "git.reset",
        Event::EdgeSelected { .. } => "edge.selected",
        Event::LoopRestart { .. } => "loop.restart",
        Event::Prompt { .. } => "stage.prompt",
        Event::PromptCompleted { .. } => "prompt.completed",
        Event::Agent { event, .. } => match event {
            AgentEvent::SessionStarted { .. } => "agent.session.started",
            AgentEvent::SessionEnded => "agent.session.ended",
            AgentEvent::ProcessingEnd => "agent.processing.end",
            AgentEvent::UserInput { .. } => "agent.input",
            AgentEvent::LlmRequestStarted { .. } => "agent.llm.started",
            AgentEvent::LlmFirstOutput { .. } => "agent.llm.first_output",
            AgentEvent::AssistantOutputReplace { .. } => "agent.output.replace",
            AgentEvent::AssistantMessage { .. } => "agent.message",
            AgentEvent::TextDelta { .. } => "agent.text.delta",
            AgentEvent::ReasoningDelta { .. } => "agent.reasoning.delta",
            AgentEvent::ToolCallStarted { .. } => "agent.tool.started",
            AgentEvent::ToolCallOutputDelta { .. } => "agent.tool.output.delta",
            AgentEvent::ToolCallCompleted { .. } => "agent.tool.completed",
            AgentEvent::ToolProcessCompleted { .. } => "agent.tool.process.completed",
            AgentEvent::Error { .. } => "agent.error",
            AgentEvent::Warning { .. } => "agent.warning",
            AgentEvent::LoopDetected => "agent.loop.detected",
            AgentEvent::SteeringInjected { .. } => "agent.steering.injected",
            AgentEvent::RoundInterrupted { .. } => "agent.round.interrupted",
            AgentEvent::CompactionStarted { .. } => "agent.compaction.started",
            AgentEvent::CompactionCompleted { .. } => "agent.compaction.completed",
            AgentEvent::LlmRetry { .. } => "agent.llm.retry",
            AgentEvent::SubAgentSpawned { .. } => "agent.sub.spawned",
            AgentEvent::SubAgentTurnStarted { .. } => "agent.sub.turn.started",
            AgentEvent::SubAgentCompleted { .. } => "agent.sub.completed",
            AgentEvent::SubAgentFailed { .. } => "agent.sub.failed",
            AgentEvent::SubAgentClosed { .. } => "agent.sub.closed",
            AgentEvent::McpServerReady { .. } => "agent.mcp.ready",
            AgentEvent::McpServerFailed { .. } => "agent.mcp.failed",
            AgentEvent::MemoryLoaded { .. } => "agent.memory.loaded",
            AgentEvent::SkillsDiscovered { .. } => "agent.skills.discovered",
            AgentEvent::SkillActivated { .. } => "agent.skill.activated",
            AgentEvent::TodoCreated(_) => "todo.created",
            AgentEvent::TodoUpdated(_) => "todo.updated",
            AgentEvent::TodoDeleted(_) => "todo.deleted",
        },
        Event::SubgraphStarted { .. } => "subgraph.started",
        Event::SubgraphCompleted { .. } => "subgraph.completed",
        Event::Sandbox { event } => match event {
            SandboxEvent::Initializing { .. } => "sandbox.initializing",
            SandboxEvent::Ready { .. } => "sandbox.ready",
            SandboxEvent::InitializeFailed { .. } => "sandbox.failed",
            SandboxEvent::CleanupStarted { .. } => "sandbox.cleanup.started",
            SandboxEvent::CleanupCompleted { .. } => "sandbox.cleanup.completed",
            SandboxEvent::CleanupFailed { .. } => "sandbox.cleanup.failed",
            SandboxEvent::StartStarted { .. } => "sandbox.start.started",
            SandboxEvent::StartCompleted { .. } => "sandbox.start.completed",
            SandboxEvent::StartFailed { .. } => "sandbox.start.failed",
            SandboxEvent::StopStarted { .. } => "sandbox.stop.started",
            SandboxEvent::StopCompleted { .. } => "sandbox.stop.completed",
            SandboxEvent::StopFailed { .. } => "sandbox.stop.failed",
            SandboxEvent::DeleteStarted { .. } => "sandbox.delete.started",
            SandboxEvent::DeleteCompleted { .. } => "sandbox.delete.completed",
            SandboxEvent::DeleteFailed { .. } => "sandbox.delete.failed",
            SandboxEvent::SnapshotPulling { .. } => "sandbox.snapshot.pulling",
            SandboxEvent::SnapshotCreating { .. } => "sandbox.snapshot.creating",
            SandboxEvent::SnapshotReady { .. } => "sandbox.snapshot.ready",
            SandboxEvent::SnapshotFailed { .. } => "sandbox.snapshot.failed",
            SandboxEvent::GitCloneStarted { .. } => "sandbox.git.started",
            SandboxEvent::GitCloneCompleted { .. } => "sandbox.git.completed",
            SandboxEvent::GitCloneFailed { .. } => "sandbox.git.failed",
        },
        Event::SandboxInitialized { .. } => "sandbox.initialized",
        Event::SetupStarted { .. } => "setup.started",
        Event::SetupCommandStarted { .. } => "setup.command.started",
        Event::SetupCommandCompleted { .. } => "setup.command.completed",
        Event::SetupCompleted { .. } => "setup.completed",
        Event::SetupFailed { .. } => "setup.failed",
        Event::StallWatchdogTimeout { .. } => "watchdog.timeout",
        Event::ArtifactCaptured { .. } => "artifact.captured",
        Event::SshAccessReady { .. } => "ssh.ready",
        Event::Failover { .. } => "agent.failover",
        Event::CommandStarted { .. } => "command.started",
        Event::CommandCompleted { .. } => "command.completed",
        Event::AgentSessionStarted { .. } => "agent.session.started",
        Event::AgentSessionActivated { .. } => "agent.session.activated",
        Event::AgentToolsAvailable { .. } => "agent.tools.available",
        Event::AgentSessionDeactivated { .. } => "agent.session.deactivated",
        Event::AgentSessionEnded { .. } => "agent.session.ended",
        Event::AgentInterruptInjected { .. } => "agent.interrupt.injected",
        Event::AgentPairUserMessage { .. } => "agent.pair.user_message",
        Event::AgentPairSystemMessage { .. } => "agent.pair.system_message",
        Event::AgentSteerBuffered { .. } => "agent.steer.buffered",
        Event::AgentSteerDropped { .. } => "agent.steer.dropped",
        Event::AgentAcpStarted { .. } => "agent.acp.started",
        Event::AgentAcpCompleted { .. } => "agent.acp.completed",
        Event::AgentAcpCancelled { .. } => "agent.acp.cancelled",
        Event::AgentAcpTimedOut { .. } => "agent.acp.timed_out",
        Event::PullRequestCreationRequested { .. } => "pull_request.creation_requested",
        Event::PullRequestCreated { .. } => "pull_request.created",
        Event::PullRequestLinked { .. } => "pull_request.linked",
        Event::PullRequestUnlinked { .. } => "pull_request.unlinked",
        Event::PullRequestFailed { .. } => "pull_request.failed",
    }
}

#[cfg(test)]
mod tests {
    use ::fabro_types::{ParallelBranchId, StageId};
    use fabro_agent::AgentEvent;

    use super::*;
    use crate::event::Event;

    #[test]
    fn event_name_matches_new_dot_notation() {
        assert_eq!(
            event_name(&Event::ParallelBranchStarted {
                graph_visit:           None,
                resumed_from_stage_id: None,
                parallel_group_id:     StageId::new("plan", 1),
                parallel_branch_id:    ParallelBranchId::new(StageId::new("plan", 1), 0),
                branch:                "fork".to_string(),
                index:                 0,
                item_label:            None,
            }),
            "parallel.branch.started"
        );
        assert_eq!(
            event_name(&Event::Agent {
                stage:             "code".to_string(),
                visit:             1,
                event:             AgentEvent::SubAgentSpawned {
                    agent_id:   "a1".to_string(),
                    depth:      1,
                    task:       "do it".to_string(),
                    generation: 1,
                },
                session_id:        None,
                parent_session_id: None,
                tool_call_id:      None,
            }),
            "agent.sub.spawned"
        );
        assert_eq!(
            event_name(&Event::Agent {
                stage:             "code".to_string(),
                visit:             1,
                event:             AgentEvent::SubAgentTurnStarted {
                    agent_id:   "a1".to_string(),
                    depth:      1,
                    task:       "fix it".to_string(),
                    generation: 2,
                },
                session_id:        None,
                parent_session_id: None,
                tool_call_id:      None,
            }),
            "agent.sub.turn.started"
        );
        assert_eq!(
            event_name(&Event::Agent {
                stage:             "code".to_string(),
                visit:             1,
                event:             AgentEvent::RoundInterrupted { generation: 1 },
                session_id:        Some("session-1".to_string()),
                parent_session_id: None,
                tool_call_id:      None,
            }),
            "agent.round.interrupted"
        );
        assert_eq!(
            event_name(&Event::AgentToolsAvailable {
                node_id:    "code".to_string(),
                visit:      1,
                session_id: "session-1".to_string(),
                tools:      Vec::new(),
            }),
            "agent.tools.available"
        );
    }

    #[test]
    fn run_archived_event_name_matches_dot_notation() {
        assert_eq!(
            event_name(&Event::RunArchived { actor: None }),
            "run.archived"
        );
        assert_eq!(
            event_name(&Event::RunUnarchived { actor: None }),
            "run.unarchived"
        );
    }
}
