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

use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::sync::Arc;

use super::format::markdown_to_slack;
use super::history::{SlackConversationHistory, save_slack_history};
use super::meta::SlackTransport;
use crate::chat::ChatTransport;
use crate::chat::transport::matrix::RoomConversation;
use crate::chat::util::is_permission_approval;
use crate::http::context::PermissionDecision;
use crate::services::Services;
use crate::slog;

mod llm;

use llm::handle_llm_message;

// ── 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. "/huskies-status").
    pub command: String,
    /// Any text typed after the command (e.g. "42" for "/huskies-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: `/huskies-status`, `/huskies-cost`, `/huskies-show`,
/// `/huskies-git`, `/huskies-htop`.
pub(super) fn slash_command_to_bot_keyword(command: &str) -> Option<&'static str> {
    // Strip leading "/" and the "huskies-" prefix.
    let name = command.strip_prefix('/').unwrap_or(command);
    let keyword = name.strip_prefix("huskies-")?;
    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 {
    /// Shared services bundle (project root, agent pool, bot identity, permissions).
    pub services: Arc<Services>,
    /// Slack signing secret for verifying incoming webhook requests.
    pub signing_secret: String,
    /// Slack Web API transport for sending/editing messages.
    pub transport: Arc<SlackTransport>,
    /// 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>,
}

// ── 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.
    {
        if let Some(tx) = ctx
            .services
            .pending_perm_replies
            .resolve_oldest(channel)
            .await
        {
            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 {
        services: &ctx.services,
        project_root: &ctx.services.project_root,
        bot_user_id: &ctx.services.bot_user_id,
        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.services.bot_name,
        &ctx.services.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.services.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.services.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.services.bot_name,
        &ctx.services.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.services.bot_name,
                    &story_number,
                    &ctx.services.project_root,
                    &ctx.services.agents,
                )
                .await
            }
            crate::chat::transport::matrix::delete::DeleteCommand::BadArgs => {
                format!("Usage: `{} delete <number>`", ctx.services.bot_name)
            }
        };
        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.services.bot_name,
        &ctx.services.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.services.bot_name,
                    &story_number,
                    &ctx.services.project_root,
                    &ctx.services.agents,
                )
                .await
            }
            crate::chat::transport::matrix::rmtree::RmtreeCommand::BadArgs => {
                format!("Usage: `{} rmtree <number>`", ctx.services.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.services.bot_name,
        &ctx.services.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.services.project_root, &guard);
        }
        let _ = ctx
            .transport
            .send_message(channel, "Session cleared.", "")
            .await;
        return;
    }

    if crate::chat::compact::extract_compact_command(
        message,
        &ctx.services.bot_name,
        &ctx.services.bot_user_id,
    )
    .is_some()
    {
        slog!("[slack] Handling compact command from {user} in {channel}");
        let response = crate::chat::compact::handle_compact_for_key(
            channel,
            &ctx.history,
            &ctx.services.project_root,
            8_000,
            save_slack_history,
        )
        .await;
        let response = markdown_to_slack(&response);
        let _ = ctx.transport.send_message(channel, &response, "").await;
        return;
    }

    if let Some(start_cmd) = crate::chat::transport::matrix::start::extract_start_command(
        message,
        &ctx.services.bot_name,
        &ctx.services.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.services.bot_name,
                    &story_number,
                    agent_hint.as_deref(),
                    &ctx.services.project_root,
                    &ctx.services.agents,
                )
                .await
            }
            crate::chat::transport::matrix::start::StartCommand::BadArgs => {
                format!("Usage: `{} start <number>`", ctx.services.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.services.bot_name,
        &ctx.services.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.services.bot_name,
                    &story_number,
                    &model,
                    &ctx.services.project_root,
                    &ctx.services.agents,
                )
                .await
            }
            crate::chat::transport::matrix::assign::AssignCommand::BadArgs => {
                format!("Usage: `{} assign <number> <model>`", ctx.services.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.
#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn parse_slash_command_payload() {
        let body = "command=%2Fhuskies-status&text=&user_id=U123&channel_id=C456";
        let payload: SlackSlashCommandPayload = serde_urlencoded::from_str(body).unwrap();
        assert_eq!(payload.command, "/huskies-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=%2Fhuskies-show&text=42&user_id=U123&channel_id=C456";
        let payload: SlackSlashCommandPayload = serde_urlencoded::from_str(body).unwrap();
        assert_eq!(payload.command, "/huskies-show");
        assert_eq!(payload.text, "42");
    }

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

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

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

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

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

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

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

    #[test]
    fn slash_command_non_huskies_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 ──────

    #[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 services = crate::services::Services::new_test(
            std::path::PathBuf::from("/tmp"),
            "Huskies".to_string(),
        );
        let room_id = "C01ABCDEF".to_string();

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

        let dispatch = CommandDispatch {
            services: &services,
            project_root: &services.project_root,
            bot_user_id: "slack-bot",
            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 services = crate::services::Services::new_test(
            std::path::PathBuf::from("/tmp"),
            "Huskies".to_string(),
        );
        let room_id = "C01ABCDEF".to_string();

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

        let dispatch = CommandDispatch {
            services: &services,
            project_root: &services.project_root,
            bot_user_id: "slack-bot",
            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}"
        );
    }

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

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

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

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

        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(),
                    }],
                    ..Default::default()
                },
            );
            m
        }));

        let tmp = tempfile::tempdir().unwrap();
        let sk = tmp.path().join(".huskies");
        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"
        );
    }
}
