using MktXService.Domain.SeedWork;

namespace MktXService.Domain.AggregatesModel.ConversationAggregate;

/// <summary>
/// EN: Message entity representing a single message in a conversation.
/// VI: Entity message đại diện cho một tin nhắn trong hội thoại.
/// </summary>
public class Message : Entity
{
    private string? _twitterMessageId;
    private string _direction = null!;
    private string _type = null!;
    private string _content = null!;
    private List<MessageAttachment>? _attachments;
    private bool _isFromBot;
    private DateTime _sentAt;
    private DateTime _createdAt;

    /// <summary>
    /// EN: Parent conversation ID.
    /// VI: ID hội thoại cha.
    /// </summary>
    public Guid ConversationId { get; private set; }

    /// <summary>
    /// EN: Twitter message ID (if synced with Twitter).
    /// VI: ID tin nhắn Twitter (nếu đã đồng bộ với Twitter).
    /// </summary>
    public string? TwitterMessageId => _twitterMessageId;

    /// <summary>
    /// EN: Message direction (inbound/outbound).
    /// VI: Hướng tin nhắn (inbound/outbound).
    /// </summary>
    public string Direction => _direction;

    /// <summary>
    /// EN: Message type (text/image/card/quick_reply).
    /// VI: Loại tin nhắn (text/image/card/quick_reply).
    /// </summary>
    public string Type => _type;

    /// <summary>
    /// EN: Message content/text.
    /// VI: Nội dung tin nhắn.
    /// </summary>
    public string Content => _content;

    /// <summary>
    /// EN: Message attachments (images, files, etc.).
    /// VI: Tệp đính kèm tin nhắn (ảnh, file, v.v.).
    /// </summary>
    public IReadOnlyCollection<MessageAttachment>? Attachments => _attachments?.AsReadOnly();

    /// <summary>
    /// EN: Whether this message was sent by a bot.
    /// VI: Tin nhắn này có được gửi bởi bot không.
    /// </summary>
    public bool IsFromBot => _isFromBot;

    /// <summary>
    /// EN: When the message was sent.
    /// VI: Thời điểm tin nhắn được gửi.
    /// </summary>
    public DateTime SentAt => _sentAt;

    /// <summary>
    /// EN: Creation timestamp in system.
    /// VI: Thời gian tạo trong hệ thống.
    /// </summary>
    public DateTime CreatedAt => _createdAt;

    /// <summary>
    /// EN: Private constructor for EF Core.
    /// VI: Constructor private cho EF Core.
    /// </summary>
    protected Message()
    {
    }

    /// <summary>
    /// EN: Create a new message.
    /// VI: Tạo một tin nhắn mới.
    /// </summary>
    public Message(
        Guid conversationId,
        string direction,
        string type,
        string content,
        bool isFromBot = false,
        string? twitterMessageId = null,
        List<MessageAttachment>? attachments = null) : this()
    {
        if (conversationId == Guid.Empty)
            throw new ArgumentException("Conversation ID cannot be empty", nameof(conversationId));
        if (string.IsNullOrWhiteSpace(direction))
            throw new ArgumentException("Direction cannot be empty", nameof(direction));
        if (string.IsNullOrWhiteSpace(type))
            throw new ArgumentException("Type cannot be empty", nameof(type));
        if (string.IsNullOrWhiteSpace(content))
            throw new ArgumentException("Content cannot be empty", nameof(content));

        Id = Guid.NewGuid();
        ConversationId = conversationId;
        _twitterMessageId = twitterMessageId;
        _direction = direction;
        _type = type;
        _content = content;
        _attachments = attachments;
        _isFromBot = isFromBot;
        _sentAt = DateTime.UtcNow;
        _createdAt = DateTime.UtcNow;
    }

    /// <summary>
    /// EN: Set Twitter message ID after sending.
    /// VI: Thiết lập Twitter message ID sau khi gửi.
    /// </summary>
    public void SetTwitterMessageId(string twitterMessageId)
    {
        _twitterMessageId = twitterMessageId;
    }
}

/// <summary>
/// EN: Message attachment value object.
/// VI: Value object đính kèm tin nhắn.
/// </summary>
public record MessageAttachment(string Type, string Url, string? Name = null);
