using System.Text.Json;
using WhatsAppService.Domain.SeedWork;

namespace WhatsAppService.Domain.AggregatesModel.ConversationAggregate;

/// <summary>
/// EN: Value object representing message content (text, media, interactive).
/// VI: Value object đại diện cho nội dung tin nhắn (text, media, interactive).
/// </summary>
public class MessageContent : ValueObject
{
    /// <summary>
    /// EN: Content type (text, image, video, document, audio, interactive, template).
    /// VI: Loại nội dung (text, image, video, document, audio, interactive, template).
    /// </summary>
    public string Type { get; private set; }

    /// <summary>
    /// EN: Text content for text messages.
    /// VI: Nội dung text cho tin nhắn text.
    /// </summary>
    public string? Text { get; private set; }

    /// <summary>
    /// EN: Media URL for media messages.
    /// VI: URL media cho tin nhắn media.
    /// </summary>
    public string? MediaUrl { get; private set; }

    /// <summary>
    /// EN: Caption for media messages.
    /// VI: Caption cho tin nhắn media.
    /// </summary>
    public string? Caption { get; private set; }

    /// <summary>
    /// EN: MIME type for media content.
    /// VI: MIME type cho nội dung media.
    /// </summary>
    public string? MimeType { get; private set; }

    /// <summary>
    /// EN: Interactive content as JSON for buttons/lists.
    /// VI: Nội dung interactive dạng JSON cho buttons/lists.
    /// </summary>
    public string? InteractiveJson { get; private set; }

    /// <summary>
    /// EN: Valid content types.
    /// VI: Các loại nội dung hợp lệ.
    /// </summary>
    public static class ContentTypes
    {
        public const string Text = "text";
        public const string Image = "image";
        public const string Video = "video";
        public const string Document = "document";
        public const string Audio = "audio";
        public const string Sticker = "sticker";
        public const string Interactive = "interactive";
        public const string Template = "template";
    }

    /// <summary>
    /// EN: Private constructor for EF Core.
    /// VI: Constructor private cho EF Core.
    /// </summary>
    private MessageContent()
    {
        Type = ContentTypes.Text;
    }

    /// <summary>
    /// EN: Create a text message content.
    /// VI: Tạo nội dung tin nhắn text.
    /// </summary>
    public static MessageContent CreateText(string text)
    {
        if (string.IsNullOrWhiteSpace(text))
            throw new ArgumentException("Text content cannot be empty");

        return new MessageContent
        {
            Type = ContentTypes.Text,
            Text = text
        };
    }

    /// <summary>
    /// EN: Create a media message content.
    /// VI: Tạo nội dung tin nhắn media.
    /// </summary>
    public static MessageContent CreateMedia(string type, string mediaUrl, string? caption = null, string? mimeType = null)
    {
        var validMediaTypes = new[] { ContentTypes.Image, ContentTypes.Video, ContentTypes.Document, ContentTypes.Audio, ContentTypes.Sticker };
        if (!validMediaTypes.Contains(type.ToLowerInvariant()))
            throw new ArgumentException($"Invalid media type. Valid values: {string.Join(", ", validMediaTypes)}");

        if (string.IsNullOrWhiteSpace(mediaUrl))
            throw new ArgumentException("Media URL cannot be empty");

        return new MessageContent
        {
            Type = type.ToLowerInvariant(),
            MediaUrl = mediaUrl,
            Caption = caption,
            MimeType = mimeType
        };
    }

    /// <summary>
    /// EN: Create an interactive message content.
    /// VI: Tạo nội dung tin nhắn interactive.
    /// </summary>
    public static MessageContent CreateInteractive(object interactiveContent)
    {
        return new MessageContent
        {
            Type = ContentTypes.Interactive,
            InteractiveJson = JsonSerializer.Serialize(interactiveContent)
        };
    }

    /// <summary>
    /// EN: Create a template message content.
    /// VI: Tạo nội dung tin nhắn template.
    /// </summary>
    public static MessageContent CreateTemplate(string templateName, object? parameters = null)
    {
        var templateData = new { name = templateName, parameters };
        return new MessageContent
        {
            Type = ContentTypes.Template,
            InteractiveJson = JsonSerializer.Serialize(templateData)
        };
    }

    protected override IEnumerable<object> GetEqualityComponents()
    {
        yield return Type;
        yield return Text ?? string.Empty;
        yield return MediaUrl ?? string.Empty;
        yield return Caption ?? string.Empty;
        yield return MimeType ?? string.Empty;
        yield return InteractiveJson ?? string.Empty;
    }
}
