//! Slack incoming message dispatch and slash command handling.

use std::collections::HashMap;
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use tokio::sync::{Mutex as TokioMutex, oneshot};
use serde::{Deserialize, Serialize};

use crate::agents::AgentPool;
use crate::chat::transport::matrix::{ConversationEntry, ConversationRole, RoomConversation};
use crate::slog;
use crate::chat::ChatTransport;
use crate::http::context::{PermissionDecision, PermissionForward};
use super::meta::SlackTransport;
use super::history::{SlackConversationHistory, save_slack_history};
use super::format::markdown_to_slack;

// ── Slash command types ─────────────────────────────────────────────────

/// Payload sent by Slack for slash commands (application/x-www-form-urlencoded).
#[derive(Deserialize, Debug)]
pub struct SlackSlashCommandPayload {
    /// The slash command that was invoked (e.g. "/storkit-status").
    pub command: String,
    /// Any text typed after the command (e.g. "42" for "/storkit-show 42").
    #[serde(default)]
    pub text: String,
    /// The user who invoked the command.
    #[serde(default)]
    pub user_id: String,
    /// The channel where the command was invoked.
    #[serde(default)]
    pub channel_id: String,
}

/// JSON response for Slack slash commands.
#[derive(Serialize)]
pub(super) struct SlashCommandResponse {
    pub(super) response_type: &'static str,
    pub(super) text: String,
}

/// Map a Slack slash command name to the corresponding bot command keyword.
///
/// Supported: `/storkit-status`, `/storkit-cost`, `/storkit-show`,
/// `/storkit-git`, `/storkit-htop`.
pub(super) fn slash_command_to_bot_keyword(command: &str) -> Option<&'static str> {
    // Strip leading "/" and the "storkit-" prefix.
    let name = command.strip_prefix('/').unwrap_or(command);
    let keyword = name.strip_prefix("storkit-")?;
    match keyword {
        "status" => Some("status"),
        "cost" => Some("cost"),
        "show" => Some("show"),
        "git" => Some("git"),
        "htop" => Some("htop"),
        _ => None,
    }
}

// ── Shared webhook context (used by mod.rs handlers) ───────────────────

/// Shared context for the Slack webhook handler, injected via Poem's `Data` extractor.
pub struct SlackWebhookContext {
    pub signing_secret: String,
    pub transport: Arc<SlackTransport>,
    pub project_root: PathBuf,
    pub agents: Arc<AgentPool>,
    pub bot_name: String,
    /// The bot's "user ID" for command dispatch.
    pub bot_user_id: String,
    pub ambient_rooms: Arc<Mutex<HashSet<String>>>,
    /// Per-channel conversation history for LLM passthrough.
    pub history: SlackConversationHistory,
    /// Maximum number of conversation entries to keep per channel.
    pub history_size: usize,
    /// Allowed channel IDs (messages from other channels are ignored).
    pub channel_ids: HashSet<String>,
    /// Permission requests from the MCP `prompt_permission` tool arrive here.
    pub perm_rx: Arc<TokioMutex<tokio::sync::mpsc::UnboundedReceiver<PermissionForward>>>,
    /// Pending permission replies keyed by channel ID.
    pub pending_perm_replies:
        Arc<TokioMutex<HashMap<String, oneshot::Sender<PermissionDecision>>>>,
    /// Seconds before an unanswered permission prompt is auto-denied.
    pub permission_timeout_secs: u64,
}

// ── Permission approval detection ──────────────────────────────────────

/// Returns `true` if the message body should be interpreted as permission approval.
fn is_permission_approval(body: &str) -> bool {
    let trimmed = body.trim().to_ascii_lowercase();
    matches!(
        trimmed.as_str(),
        "yes" | "y" | "approve" | "allow" | "ok"
    )
}

// ── Incoming message dispatch ───────────────────────────────────────────

pub(super) async fn handle_incoming_message(
    ctx: &SlackWebhookContext,
    channel: &str,
    user: &str,
    message: &str,
) {
    use crate::chat::commands::{CommandDispatch, try_handle_command};

    // If there is a pending permission prompt for this channel, interpret the
    // message as a yes/no response instead of starting a new command/LLM flow.
    {
        let mut pending = ctx.pending_perm_replies.lock().await;
        if let Some(tx) = pending.remove(channel) {
            let decision = if is_permission_approval(message) {
                PermissionDecision::Approve
            } else {
                PermissionDecision::Deny
            };
            let _ = tx.send(decision);
            let confirmation = if decision == PermissionDecision::Approve {
                "Permission approved."
            } else {
                "Permission denied."
            };
            let formatted = markdown_to_slack(confirmation);
            let _ = ctx.transport.send_message(channel, &formatted, "").await;
            return;
        }
    }

    let dispatch = CommandDispatch {
        bot_name: &ctx.bot_name,
        bot_user_id: &ctx.bot_user_id,
        project_root: &ctx.project_root,
        agents: &ctx.agents,
        ambient_rooms: &ctx.ambient_rooms,
        room_id: channel,
    };

    if let Some(response) = try_handle_command(&dispatch, message) {
        slog!("[slack] Sending command response to {channel}");
        let response = markdown_to_slack(&response);
        if let Err(e) = ctx.transport.send_message(channel, &response, "").await {
            slog!("[slack] Failed to send reply to {channel}: {e}");
        }
        return;
    }

    // Check for async commands (htop, delete).
    if let Some(htop_cmd) = crate::chat::transport::matrix::htop::extract_htop_command(
        message,
        &ctx.bot_name,
        &ctx.bot_user_id,
    ) {
        use crate::chat::transport::matrix::htop::HtopCommand;
        slog!("[slack] Handling htop command from {user} in {channel}");
        match htop_cmd {
            HtopCommand::Stop => {
                let _ = ctx
                    .transport
                    .send_message(channel, "htop stopped.", "")
                    .await;
            }
            HtopCommand::Start { duration_secs } => {
                // On Slack, htop uses native message editing for live updates.
                let snapshot =
                    crate::chat::transport::matrix::htop::build_htop_message(&ctx.agents, 0, duration_secs);
                let snapshot = markdown_to_slack(&snapshot);
                let msg_id = match ctx.transport.send_message(channel, &snapshot, "").await {
                    Ok(id) => id,
                    Err(e) => {
                        slog!("[slack] Failed to send htop message: {e}");
                        return;
                    }
                };
                // Spawn a background task that edits the message periodically.
                let transport = Arc::clone(&ctx.transport);
                let agents = Arc::clone(&ctx.agents);
                let ch = channel.to_string();
                tokio::spawn(async move {
                    let interval = std::time::Duration::from_secs(2);
                    let total_ticks = (duration_secs as usize) / 2;
                    for tick in 1..=total_ticks {
                        tokio::time::sleep(interval).await;
                        let updated = crate::chat::transport::matrix::htop::build_htop_message(
                            &agents,
                            (tick * 2) as u32,
                            duration_secs,
                        );
                        let updated = markdown_to_slack(&updated);
                        if let Err(e) =
                            transport.edit_message(&ch, &msg_id, &updated, "").await
                        {
                            slog!("[slack] Failed to edit htop message: {e}");
                            break;
                        }
                    }
                });
            }
        }
        return;
    }

    if let Some(del_cmd) = crate::chat::transport::matrix::delete::extract_delete_command(
        message,
        &ctx.bot_name,
        &ctx.bot_user_id,
    ) {
        let response = match del_cmd {
            crate::chat::transport::matrix::delete::DeleteCommand::Delete { story_number } => {
                slog!("[slack] Handling delete command from {user}: story {story_number}");
                crate::chat::transport::matrix::delete::handle_delete(
                    &ctx.bot_name,
                    &story_number,
                    &ctx.project_root,
                    &ctx.agents,
                )
                .await
            }
            crate::chat::transport::matrix::delete::DeleteCommand::BadArgs => {
                format!("Usage: `{} delete <number>`", ctx.bot_name)
            }
        };
        let response = markdown_to_slack(&response);
        let _ = ctx.transport.send_message(channel, &response, "").await;
        return;
    }

    if crate::chat::transport::matrix::rebuild::extract_rebuild_command(
        message,
        &ctx.bot_name,
        &ctx.bot_user_id,
    )
    .is_some()
    {
        slog!("[slack] Handling rebuild command from {user} in {channel}");
        let ack = "Rebuilding server… this may take a moment.";
        let _ = ctx.transport.send_message(channel, ack, "").await;
        let response = crate::chat::transport::matrix::rebuild::handle_rebuild(
            &ctx.bot_name,
            &ctx.project_root,
            &ctx.agents,
        )
        .await;
        let response = markdown_to_slack(&response);
        let _ = ctx.transport.send_message(channel, &response, "").await;
        return;
    }

    if let Some(rmtree_cmd) = crate::chat::transport::matrix::rmtree::extract_rmtree_command(
        message,
        &ctx.bot_name,
        &ctx.bot_user_id,
    ) {
        let response = match rmtree_cmd {
            crate::chat::transport::matrix::rmtree::RmtreeCommand::Rmtree { story_number } => {
                slog!("[slack] Handling rmtree command from {user} in {channel}: story {story_number}");
                crate::chat::transport::matrix::rmtree::handle_rmtree(
                    &ctx.bot_name,
                    &story_number,
                    &ctx.project_root,
                    &ctx.agents,
                )
                .await
            }
            crate::chat::transport::matrix::rmtree::RmtreeCommand::BadArgs => {
                format!("Usage: `{} rmtree <number>`", ctx.bot_name)
            }
        };
        let response = markdown_to_slack(&response);
        let _ = ctx.transport.send_message(channel, &response, "").await;
        return;
    }

    if crate::chat::transport::matrix::reset::extract_reset_command(
        message,
        &ctx.bot_name,
        &ctx.bot_user_id,
    )
    .is_some()
    {
        slog!("[slack] Handling reset command from {user} in {channel}");
        {
            let mut guard = ctx.history.lock().await;
            let conv = guard.entry(channel.to_string()).or_insert_with(RoomConversation::default);
            conv.session_id = None;
            conv.entries.clear();
            save_slack_history(&ctx.project_root, &guard);
        }
        let _ = ctx
            .transport
            .send_message(channel, "Session cleared.", "")
            .await;
        return;
    }

    if let Some(start_cmd) = crate::chat::transport::matrix::start::extract_start_command(
        message,
        &ctx.bot_name,
        &ctx.bot_user_id,
    ) {
        let response = match start_cmd {
            crate::chat::transport::matrix::start::StartCommand::Start {
                story_number,
                agent_hint,
            } => {
                slog!("[slack] Handling start command from {user} in {channel}: story {story_number}");
                crate::chat::transport::matrix::start::handle_start(
                    &ctx.bot_name,
                    &story_number,
                    agent_hint.as_deref(),
                    &ctx.project_root,
                    &ctx.agents,
                )
                .await
            }
            crate::chat::transport::matrix::start::StartCommand::BadArgs => {
                format!("Usage: `{} start <number>`", ctx.bot_name)
            }
        };
        let response = markdown_to_slack(&response);
        let _ = ctx.transport.send_message(channel, &response, "").await;
        return;
    }

    if let Some(assign_cmd) = crate::chat::transport::matrix::assign::extract_assign_command(
        message,
        &ctx.bot_name,
        &ctx.bot_user_id,
    ) {
        let response = match assign_cmd {
            crate::chat::transport::matrix::assign::AssignCommand::Assign { story_number, model } => {
                slog!("[slack] Handling assign command from {user} in {channel}: story {story_number} model {model}");
                crate::chat::transport::matrix::assign::handle_assign(
                    &ctx.bot_name,
                    &story_number,
                    &model,
                    &ctx.project_root,
                    &ctx.agents,
                )
                .await
            }
            crate::chat::transport::matrix::assign::AssignCommand::BadArgs => {
                format!("Usage: `{} assign <number> <model>`", ctx.bot_name)
            }
        };
        let response = markdown_to_slack(&response);
        let _ = ctx.transport.send_message(channel, &response, "").await;
        return;
    }

    // No command matched — forward to LLM for conversational response.
    slog!("[slack] No command matched, forwarding to LLM for {user} in {channel}");
    handle_llm_message(ctx, channel, user, message).await;
}

/// Forward a message to Claude Code and send the response back via Slack.
async fn handle_llm_message(
    ctx: &SlackWebhookContext,
    channel: &str,
    user: &str,
    user_message: &str,
) {
    use crate::llm::providers::claude_code::{ClaudeCodeProvider, ClaudeCodeResult};
    use crate::chat::util::drain_complete_paragraphs;
    use std::sync::atomic::{AtomicBool, Ordering};
    use tokio::sync::watch;

    // Look up existing session ID for this channel.
    let resume_session_id: Option<String> = {
        let guard = ctx.history.lock().await;
        guard
            .get(channel)
            .and_then(|conv| conv.session_id.clone())
    };

    let bot_name = &ctx.bot_name;
    let prompt = format!(
        "[Your name is {bot_name}. Refer to yourself as {bot_name}, not Claude.]\n\n{user}: {user_message}"
    );

    let provider = ClaudeCodeProvider::new();
    let (_cancel_tx, mut cancel_rx) = watch::channel(false);

    // Channel for sending complete chunks to the Slack posting task.
    let (msg_tx, mut msg_rx) = tokio::sync::mpsc::unbounded_channel::<String>();
    let msg_tx_for_callback = msg_tx.clone();

    // Spawn a task to post messages as they arrive.
    let post_transport = Arc::clone(&ctx.transport);
    let post_channel = channel.to_string();
    let post_task = tokio::spawn(async move {
        while let Some(chunk) = msg_rx.recv().await {
            let formatted = markdown_to_slack(&chunk);
            let _ = post_transport.send_message(&post_channel, &formatted, "").await;
        }
    });

    // Shared buffer between the sync token callback and the async scope.
    let buffer = Arc::new(std::sync::Mutex::new(String::new()));
    let buffer_for_callback = Arc::clone(&buffer);
    let sent_any_chunk = Arc::new(AtomicBool::new(false));
    let sent_any_chunk_for_callback = Arc::clone(&sent_any_chunk);

    let project_root_str = ctx.project_root.to_string_lossy().to_string();
    let chat_fut = provider.chat_stream(
        &prompt,
        &project_root_str,
        resume_session_id.as_deref(),
        None,
        &mut cancel_rx,
        move |token| {
            let mut buf = buffer_for_callback.lock().unwrap();
            buf.push_str(token);
            let paragraphs = drain_complete_paragraphs(&mut buf);
            for chunk in paragraphs {
                sent_any_chunk_for_callback.store(true, Ordering::Relaxed);
                let _ = msg_tx_for_callback.send(chunk);
            }
        },
        |_thinking| {},
        |_activity| {},
    );
    tokio::pin!(chat_fut);

    // Lock the permission receiver for the duration of this chat session.
    let mut perm_rx_guard = ctx.perm_rx.lock().await;

    let result = loop {
        tokio::select! {
            r = &mut chat_fut => break r,

            Some(perm_fwd) = perm_rx_guard.recv() => {
                let prompt_msg = format!(
                    "*Permission Request*\n\nTool: `{}`\n```json\n{}\n```\n\nReply *yes* to approve or *no* to deny.",
                    perm_fwd.tool_name,
                    serde_json::to_string_pretty(&perm_fwd.tool_input)
                        .unwrap_or_else(|_| perm_fwd.tool_input.to_string()),
                );
                let formatted = markdown_to_slack(&prompt_msg);
                let _ = ctx.transport.send_message(channel, &formatted, "").await;

                // Store the response sender so the incoming message handler
                // can resolve it when the user replies yes/no.
                ctx.pending_perm_replies
                    .lock()
                    .await
                    .insert(channel.to_string(), perm_fwd.response_tx);

                // Spawn a timeout task: auto-deny if the user does not respond.
                let pending = Arc::clone(&ctx.pending_perm_replies);
                let timeout_channel = channel.to_string();
                let timeout_transport = Arc::clone(&ctx.transport);
                let timeout_secs = ctx.permission_timeout_secs;
                tokio::spawn(async move {
                    tokio::time::sleep(std::time::Duration::from_secs(timeout_secs)).await;
                    if let Some(tx) = pending.lock().await.remove(&timeout_channel) {
                        let _ = tx.send(PermissionDecision::Deny);
                        let msg = "Permission request timed out — denied (fail-closed).";
                        let _ = timeout_transport.send_message(&timeout_channel, msg, "").await;
                    }
                });
            }
        }
    };
    drop(perm_rx_guard);

    // Flush remaining text.
    let remaining = buffer.lock().unwrap().trim().to_string();
    let did_send_any = sent_any_chunk.load(Ordering::Relaxed);

    let (assistant_reply, new_session_id) = match result {
        Ok(ClaudeCodeResult {
            messages,
            session_id,
        }) => {
            let reply = if !remaining.is_empty() {
                let _ = msg_tx.send(remaining.clone());
                remaining
            } else if !did_send_any {
                let last_text = messages
                    .iter()
                    .rev()
                    .find(|m| {
                        m.role == crate::llm::types::Role::Assistant && !m.content.is_empty()
                    })
                    .map(|m| m.content.clone())
                    .unwrap_or_default();
                if !last_text.is_empty() {
                    let _ = msg_tx.send(last_text.clone());
                }
                last_text
            } else {
                remaining
            };
            slog!("[slack] session_id from chat_stream: {:?}", session_id);
            (reply, session_id)
        }
        Err(e) => {
            slog!("[slack] LLM error: {e}");
            let err_msg = format!("Error processing your request: {e}");
            let _ = msg_tx.send(err_msg.clone());
            (err_msg, None)
        }
    };

    // Signal the posting task to finish and wait for it.
    drop(msg_tx);
    let _ = post_task.await;

    // Record this exchange in conversation history.
    if !assistant_reply.starts_with("Error processing") {
        let mut guard = ctx.history.lock().await;
        let conv = guard.entry(channel.to_string()).or_default();

        if new_session_id.is_some() {
            conv.session_id = new_session_id;
        }

        conv.entries.push(ConversationEntry {
            role: ConversationRole::User,
            sender: user.to_string(),
            content: user_message.to_string(),
        });
        conv.entries.push(ConversationEntry {
            role: ConversationRole::Assistant,
            sender: String::new(),
            content: assistant_reply,
        });

        // Trim to configured maximum.
        if conv.entries.len() > ctx.history_size {
            let excess = conv.entries.len() - ctx.history_size;
            conv.entries.drain(..excess);
        }

        save_slack_history(&ctx.project_root, &guard);
    }
}

// ── Tests ───────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    // ── Slash command types ────────────────────────────────────────────

    #[test]
    fn parse_slash_command_payload() {
        let body = "command=%2Fstorkit-status&text=&user_id=U123&channel_id=C456";
        let payload: SlackSlashCommandPayload = serde_urlencoded::from_str(body).unwrap();
        assert_eq!(payload.command, "/storkit-status");
        assert_eq!(payload.text, "");
        assert_eq!(payload.user_id, "U123");
        assert_eq!(payload.channel_id, "C456");
    }

    #[test]
    fn parse_slash_command_payload_with_text() {
        let body = "command=%2Fstorkit-show&text=42&user_id=U123&channel_id=C456";
        let payload: SlackSlashCommandPayload = serde_urlencoded::from_str(body).unwrap();
        assert_eq!(payload.command, "/storkit-show");
        assert_eq!(payload.text, "42");
    }

    // ── slash_command_to_bot_keyword ───────────────────────────────────

    #[test]
    fn slash_command_maps_status() {
        assert_eq!(slash_command_to_bot_keyword("/storkit-status"), Some("status"));
    }

    #[test]
    fn slash_command_maps_cost() {
        assert_eq!(slash_command_to_bot_keyword("/storkit-cost"), Some("cost"));
    }

    #[test]
    fn slash_command_maps_show() {
        assert_eq!(slash_command_to_bot_keyword("/storkit-show"), Some("show"));
    }

    #[test]
    fn slash_command_maps_git() {
        assert_eq!(slash_command_to_bot_keyword("/storkit-git"), Some("git"));
    }

    #[test]
    fn slash_command_maps_htop() {
        assert_eq!(slash_command_to_bot_keyword("/storkit-htop"), Some("htop"));
    }

    #[test]
    fn slash_command_unknown_returns_none() {
        assert_eq!(slash_command_to_bot_keyword("/storkit-unknown"), None);
    }

    #[test]
    fn slash_command_non_storkit_returns_none() {
        assert_eq!(slash_command_to_bot_keyword("/other-command"), None);
    }

    // ── SlashCommandResponse serialization ────────────────────────────

    #[test]
    fn slash_response_is_ephemeral() {
        let resp = SlashCommandResponse {
            response_type: "ephemeral",
            text: "hello".to_string(),
        };
        let json: serde_json::Value = serde_json::from_str(
            &serde_json::to_string(&resp).unwrap()
        ).unwrap();
        assert_eq!(json["response_type"], "ephemeral");
        assert_eq!(json["text"], "hello");
    }

    // ── Slash command shares handlers with mention-based commands ──────

    fn test_agents() -> Arc<crate::agents::AgentPool> {
        Arc::new(crate::agents::AgentPool::new_test(3000))
    }

    fn test_ambient_rooms() -> Arc<Mutex<HashSet<String>>> {
        Arc::new(Mutex::new(HashSet::new()))
    }

    #[test]
    fn slash_command_dispatches_through_command_registry() {
        // Verify that the synthetic message built by the slash handler
        // correctly dispatches through try_handle_command.
        use crate::chat::commands::{CommandDispatch, try_handle_command};

        let agents = test_agents();
        let ambient_rooms = test_ambient_rooms();
        let room_id = "C01ABCDEF".to_string();

        // Simulate what slash_command_receive does: build a synthetic message.
        let bot_name = "Storkit";
        let keyword = slash_command_to_bot_keyword("/storkit-status").unwrap();
        let synthetic = format!("{bot_name} {keyword}");

        let dispatch = CommandDispatch {
            bot_name,
            bot_user_id: "slack-bot",
            project_root: std::path::Path::new("/tmp"),
            agents: &agents,
            ambient_rooms: &ambient_rooms,
            room_id: &room_id,
        };

        let result = try_handle_command(&dispatch, &synthetic);
        assert!(result.is_some(), "status slash command should produce output via registry");
        assert!(result.unwrap().contains("Pipeline Status"));
    }

    #[test]
    fn slash_command_show_passes_args_through_registry() {
        use crate::chat::commands::{CommandDispatch, try_handle_command};

        let agents = test_agents();
        let ambient_rooms = test_ambient_rooms();
        let room_id = "C01ABCDEF".to_string();

        let bot_name = "Storkit";
        let keyword = slash_command_to_bot_keyword("/storkit-show").unwrap();
        // Simulate /storkit-show with text "999"
        let synthetic = format!("{bot_name} {keyword} 999");

        let dispatch = CommandDispatch {
            bot_name,
            bot_user_id: "slack-bot",
            project_root: std::path::Path::new("/tmp"),
            agents: &agents,
            ambient_rooms: &ambient_rooms,
            room_id: &room_id,
        };

        let result = try_handle_command(&dispatch, &synthetic);
        assert!(result.is_some(), "show slash command should produce output");
        let output = result.unwrap();
        assert!(output.contains("999"), "show output should reference the story number: {output}");
    }

    // ── rebuild command extraction ─────────────────────────────────────

    #[test]
    fn rebuild_command_extracted_from_slack_message() {
        let result = crate::chat::transport::matrix::rebuild::extract_rebuild_command(
            "Storkit rebuild",
            "Storkit",
            "slack-bot",
        );
        assert!(result.is_some(), "'Storkit rebuild' should be recognised");
    }

    #[test]
    fn rebuild_command_extracted_plain_no_mention() {
        // Slack slash-command synthetic messages may not include a bot mention.
        let result = crate::chat::transport::matrix::rebuild::extract_rebuild_command(
            "rebuild",
            "Storkit",
            "slack-bot",
        );
        assert!(result.is_some(), "plain 'rebuild' should be recognised");
    }

    #[test]
    fn non_rebuild_slack_message_not_extracted() {
        let result = crate::chat::transport::matrix::rebuild::extract_rebuild_command(
            "Storkit status",
            "Storkit",
            "slack-bot",
        );
        assert!(result.is_none(), "'status' should not be recognised as rebuild");
    }

    // ── reset command extraction ───────────────────────────────────────

    #[test]
    fn reset_command_extracted_from_slack_message() {
        let result = crate::chat::transport::matrix::reset::extract_reset_command(
            "Storkit reset",
            "Storkit",
            "slack-bot",
        );
        assert!(result.is_some(), "'Storkit reset' should be recognised");
    }

    #[test]
    fn reset_command_extracted_plain_no_mention() {
        let result = crate::chat::transport::matrix::reset::extract_reset_command(
            "reset",
            "Storkit",
            "slack-bot",
        );
        assert!(result.is_some(), "plain 'reset' should be recognised");
    }

    #[tokio::test]
    async fn reset_command_clears_slack_session() {
        use std::sync::Arc;
        use tokio::sync::Mutex as TokioMutex;
        use crate::chat::transport::matrix::{ConversationEntry, ConversationRole, RoomConversation};

        let channel = "C01ABCDEF";
        let history: SlackConversationHistory = Arc::new(TokioMutex::new({
            let mut m = HashMap::new();
            m.insert(channel.to_string(), RoomConversation {
                session_id: Some("old-session".to_string()),
                entries: vec![ConversationEntry {
                    role: ConversationRole::User,
                    sender: "U01GHIJKL".to_string(),
                    content: "previous message".to_string(),
                }],
            });
            m
        }));

        let tmp = tempfile::tempdir().unwrap();
        let sk = tmp.path().join(".storkit");
        std::fs::create_dir_all(&sk).unwrap();

        {
            let mut guard = history.lock().await;
            let conv = guard.entry(channel.to_string()).or_insert_with(RoomConversation::default);
            conv.session_id = None;
            conv.entries.clear();
            save_slack_history(tmp.path(), &guard);
        }

        let guard = history.lock().await;
        let conv = guard.get(channel).unwrap();
        assert!(conv.session_id.is_none(), "session_id should be cleared");
        assert!(conv.entries.is_empty(), "entries should be cleared");
    }

    // ── start command extraction ───────────────────────────────────────

    #[test]
    fn start_command_extracted_from_plain_slack_message() {
        // Slack messages may arrive without a bot mention prefix.
        // extract_start_command must recognise "start 42" by itself.
        let result = crate::chat::transport::matrix::start::extract_start_command(
            "start 42",
            "Timmy",
            "@timmy:home.local",
        );
        assert!(result.is_some(), "plain 'start 42' should be recognised");
        assert_eq!(
            result,
            Some(crate::chat::transport::matrix::start::StartCommand::Start {
                story_number: "42".to_string(),
                agent_hint: None,
            })
        );
    }

    #[test]
    fn start_command_extracted_with_bot_name_prefix_slack() {
        let result = crate::chat::transport::matrix::start::extract_start_command(
            "Timmy start 99",
            "Timmy",
            "@timmy:home.local",
        );
        assert!(result.is_some(), "'Timmy start 99' should be recognised");
    }

    #[test]
    fn non_start_slack_message_not_extracted() {
        let result = crate::chat::transport::matrix::start::extract_start_command(
            "help",
            "Timmy",
            "@timmy:home.local",
        );
        assert!(result.is_none(), "'help' should not be recognised as start");
    }

    // ── assign command extraction ──────────────────────────────────────

    #[test]
    fn assign_command_extracted_from_plain_message_slack() {
        let result = crate::chat::transport::matrix::assign::extract_assign_command(
            "assign 42 opus",
            "Timmy",
            "@timmy:home.local",
        );
        assert!(
            matches!(
                result,
                Some(crate::chat::transport::matrix::assign::AssignCommand::Assign { .. })
            ),
            "plain 'assign 42 opus' should be recognised on Slack"
        );
    }

    #[test]
    fn assign_command_extracted_with_bot_name_prefix_slack() {
        let result = crate::chat::transport::matrix::assign::extract_assign_command(
            "Timmy assign 42 sonnet",
            "Timmy",
            "@timmy:home.local",
        );
        assert!(
            matches!(
                result,
                Some(crate::chat::transport::matrix::assign::AssignCommand::Assign { .. })
            ),
            "'Timmy assign 42 sonnet' should be recognised on Slack"
        );
    }

    #[test]
    fn assign_command_returns_bad_args_without_model_slack() {
        let result = crate::chat::transport::matrix::assign::extract_assign_command(
            "assign 42",
            "Timmy",
            "@timmy:home.local",
        );
        assert_eq!(
            result,
            Some(crate::chat::transport::matrix::assign::AssignCommand::BadArgs)
        );
    }

    #[test]
    fn non_assign_slack_message_not_extracted() {
        let result = crate::chat::transport::matrix::assign::extract_assign_command(
            "status",
            "Timmy",
            "@timmy:home.local",
        );
        assert!(result.is_none(), "'status' should not be recognised as assign on Slack");
    }
}
