//! Message ID type using TypeID format.

use mti::prelude::*;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use std::str::FromStr;

/// A unique message identifier using TypeID format.
///
/// Format: `msg_<uuid_v7>`
/// Example: `msg_01h455vb4pex5vsknk084sn02q`
///
/// MessageIds are time-sortable (UUIDv7) and self-describing with the `msg_` prefix.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MessageId(MagicTypeId);

/// Error returned when parsing an invalid message ID.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InvalidMessageId {
    /// TypeID parsing failed.
    Parse(MagicTypeIdError),
    /// Wrong prefix (expected "msg").
    WrongPrefix {
        expected: &'static str,
        actual: String,
    },
}

impl fmt::Display for InvalidMessageId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Parse(e) => write!(f, "invalid message ID: {e}"),
            Self::WrongPrefix { expected, actual } => {
                write!(f, "expected prefix '{expected}', got '{actual}'")
            }
        }
    }
}

impl std::error::Error for InvalidMessageId {}

impl MessageId {
    /// The TypeID prefix for message identifiers.
    pub const PREFIX: &'static str = "msg";

    /// Creates a new message ID with a fresh UUIDv7 (time-sortable).
    #[must_use]
    pub fn new() -> Self {
        Self(Self::PREFIX.create_type_id::<V7>())
    }

    /// Parses a message ID from a string, validating the prefix.
    ///
    /// # Errors
    ///
    /// Returns an error if the string is not a valid TypeID or has the wrong prefix.
    pub fn parse(s: &str) -> Result<Self, InvalidMessageId> {
        let id = MagicTypeId::from_str(s).map_err(InvalidMessageId::Parse)?;

        if id.prefix().as_str() != Self::PREFIX {
            return Err(InvalidMessageId::WrongPrefix {
                expected: Self::PREFIX,
                actual: id.prefix().as_str().to_string(),
            });
        }

        Ok(Self(id))
    }

    /// Returns the underlying MagicTypeId.
    #[must_use]
    pub fn inner(&self) -> &MagicTypeId {
        &self.0
    }

    /// Returns the TypeID suffix (base32-encoded UUID).
    #[must_use]
    pub fn suffix(&self) -> String {
        self.0.suffix().to_string()
    }
}

impl Default for MessageId {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for MessageId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl FromStr for MessageId {
    type Err = InvalidMessageId;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

impl AsRef<MagicTypeId> for MessageId {
    fn as_ref(&self) -> &MagicTypeId {
        &self.0
    }
}

impl Serialize for MessageId {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.0.to_string().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for MessageId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        Self::parse(&s).map_err(serde::de::Error::custom)
    }
}

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

    #[test]
    fn new_creates_valid_message_id() {
        let id = MessageId::new();
        assert!(id.to_string().starts_with("msg_"));
    }

    #[test]
    fn parse_valid_message_id() {
        let id_str = MessageId::new().to_string();
        let parsed = MessageId::parse(&id_str);
        assert!(parsed.is_ok());
    }

    #[test]
    fn parse_wrong_prefix_fails() {
        let result = MessageId::parse("cor_01h455vb4pex5vsknk084sn02q");
        assert!(matches!(
            result,
            Err(InvalidMessageId::WrongPrefix {
                expected: "msg",
                ..
            })
        ));
    }

    #[test]
    fn parse_invalid_format_fails() {
        let result = MessageId::parse("not-a-valid-typeid");
        assert!(matches!(result, Err(InvalidMessageId::Parse(_))));
    }

    #[test]
    fn message_ids_are_unique() {
        let id1 = MessageId::new();
        let id2 = MessageId::new();
        assert_ne!(id1, id2);
    }

    #[test]
    fn display_format() {
        let id = MessageId::new();
        let s = id.to_string();
        assert!(s.starts_with("msg_"));
        assert_eq!(s.len(), 30); // "msg_" (4) + suffix (26)
    }

    #[test]
    fn serde_roundtrip() -> Result<(), serde_json::Error> {
        let id = MessageId::new();
        let json = serde_json::to_string(&id)?;
        let restored: MessageId = serde_json::from_str(&json)?;
        assert_eq!(id, restored);
        Ok(())
    }

    #[test]
    fn from_str_works() -> Result<(), InvalidMessageId> {
        let id = MessageId::new();
        let s = id.to_string();
        let parsed: MessageId = s.parse()?;
        assert_eq!(id, parsed);
        Ok(())
    }
}
