namespace ChatService.Domain.AggregatesModel.ConversationAggregate;

using ChatService.Domain.SeedWork;

/// <summary>
/// EN: Message type enumeration.
/// VI: Enumeration loại tin nhắn.
/// </summary>
public class MessageType : Enumeration
{
    /// <summary>
    /// EN: Text message.
    /// VI: Tin nhắn văn bản.
    /// </summary>
    public static readonly MessageType Text = new(1, nameof(Text).ToLowerInvariant());

    /// <summary>
    /// EN: Image message.
    /// VI: Tin nhắn hình ảnh.
    /// </summary>
    public static readonly MessageType Image = new(2, nameof(Image).ToLowerInvariant());

    /// <summary>
    /// EN: File attachment message.
    /// VI: Tin nhắn file đính kèm.
    /// </summary>
    public static readonly MessageType File = new(3, nameof(File).ToLowerInvariant());

    /// <summary>
    /// EN: Voice message.
    /// VI: Tin nhắn thoại.
    /// </summary>
    public static readonly MessageType Voice = new(4, nameof(Voice).ToLowerInvariant());

    /// <summary>
    /// EN: System message (e.g., user joined).
    /// VI: Tin nhắn hệ thống (vd: user tham gia).
    /// </summary>
    public static readonly MessageType System = new(5, nameof(System).ToLowerInvariant());

    public MessageType(int id, string name) : base(id, name)
    {
    }

    public static IEnumerable<MessageType> List() =>
        new[] { Text, Image, File, Voice, System };

    public static MessageType FromName(string name)
    {
        var state = List().SingleOrDefault(s => 
            string.Equals(s.Name, name, StringComparison.CurrentCultureIgnoreCase));

        if (state == null)
        {
            throw new ArgumentException($"Possible values for MessageType: {string.Join(",", List().Select(s => s.Name))}");
        }

        return state;
    }

    public static MessageType From(int id)
    {
        var state = List().SingleOrDefault(s => s.Id == id);

        if (state == null)
        {
            throw new ArgumentException($"Possible values for MessageType: {string.Join(",", List().Select(s => s.Name))}");
        }

        return state;
    }
}
