use anyhow::Result;
use serde::Serialize;

/// Discord webhook embed payload.
#[derive(Debug, Clone, Serialize)]
pub struct DiscordEmbed {
    pub title: String,
    pub description: String,
    pub color: u32,
    pub fields: Vec<DiscordEmbedField>,
    pub timestamp: Option<String>,
}

/// Field within a Discord embed.
#[derive(Debug, Clone, Serialize)]
pub struct DiscordEmbedField {
    pub name: String,
    pub value: String,
    pub inline: bool,
}

/// Discord webhook notification service.
///
/// Sends rich embed messages to Discord channels via webhook URLs.
/// Used for wipe announcements, completion notifications, failure
/// alerts, and crash recovery notifications.
pub struct DiscordNotifier {
    // TODO: Add fields:
    // - webhook_url: String
    // - server_name: String (for embed context)
}

impl DiscordNotifier {
    /// Send a generic notification with a custom embed.
    pub async fn send_notification(&self, _embed: DiscordEmbed) -> Result<()> {
        // TODO: POST to webhook_url with JSON payload { embeds: [embed] }
        // TODO: Handle rate limiting (429 responses with Retry-After)
        todo!()
    }

    /// Send a wipe-starting announcement.
    pub async fn send_wipe_start(&self, _wipe_type: &str, _eta_minutes: u32) -> Result<()> {
        // TODO: Build embed with orange color, wipe type, countdown info
        // TODO: Include server name, expected completion time
        // TODO: Call send_notification
        todo!()
    }

    /// Send a wipe-completed announcement.
    pub async fn send_wipe_complete(
        &self,
        _wipe_type: &str,
        _duration_seconds: u64,
        _new_map: Option<&str>,
        _new_seed: Option<i32>,
    ) -> Result<()> {
        // TODO: Build embed with green color, wipe summary
        // TODO: Include new map/seed info, connect URL
        // TODO: Call send_notification
        todo!()
    }

    /// Send a wipe-failed alert.
    pub async fn send_wipe_failed(
        &self,
        _wipe_type: &str,
        _error: &str,
        _rolled_back: bool,
    ) -> Result<()> {
        // TODO: Build embed with red color, failure details
        // TODO: Include rollback status, error message
        // TODO: Call send_notification
        todo!()
    }

    /// Send a crash detection/recovery alert.
    pub async fn send_crash_alert(
        &self,
        _crash_count: u32,
        _auto_recovered: bool,
    ) -> Result<()> {
        // TODO: Build embed with red/yellow color based on severity
        // TODO: Include crash count, recovery status, uptime before crash
        // TODO: Call send_notification
        todo!()
    }
}
