using FacebookService.Domain.SeedWork;

namespace FacebookService.Domain.AggregatesModel.ConversationAggregate;

/// <summary>
/// EN: Message entity representing a single message in a conversation.
/// VI: Entity tin nhắn đại diện cho một tin nhắn trong cuộc hội thoại.
/// </summary>
public class Message : Entity
{
    // EN: Private fields for encapsulation
    // VI: Fields private để đóng gói
    private string _senderId = null!;
    private string _content = null!;
    private MessageType _messageType = null!;
    private MessageDirection _direction = null!;
    private Dictionary<string, object> _metadata = new();
    private DateTime _sentAt;
    private string? _facebookMessageId;

    /// <summary>
    /// EN: Facebook sender ID (user or page). / VI: ID người gửi Facebook.
    /// </summary>
    public string SenderId => _senderId;

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

    /// <summary>
    /// EN: Type of message. / VI: Loại tin nhắn.
    /// </summary>
    public MessageType MessageType => _messageType;

    /// <summary>
    /// EN: Message type ID for EF Core. / VI: ID loại tin nhắn cho EF Core.
    /// </summary>
    public int MessageTypeId { get; private set; }

    /// <summary>
    /// EN: Direction of message (incoming/outgoing).
    /// VI: Hướng tin nhắn (đến/đi).
    /// </summary>
    public MessageDirection Direction => _direction;

    /// <summary>
    /// EN: Direction ID for EF Core. / VI: ID hướng cho EF Core.
    /// </summary>
    public int DirectionId { get; private set; }

    /// <summary>
    /// EN: Additional metadata (quick reply payload, attachment info, etc.).
    /// VI: Metadata bổ sung (quick reply payload, thông tin attachment, v.v.).
    /// </summary>
    public IReadOnlyDictionary<string, object> Metadata => _metadata.AsReadOnly();

    /// <summary>
    /// EN: Timestamp when message was sent. / VI: Thời gian gửi tin nhắn.
    /// </summary>
    public DateTime SentAt => _sentAt;

    /// <summary>
    /// EN: Facebook message ID (mid). / VI: ID tin nhắn Facebook.
    /// </summary>
    public string? FacebookMessageId => _facebookMessageId;

    /// <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 tin nhắn mới.
    /// </summary>
    public Message(
        string senderId,
        string content,
        MessageType messageType,
        MessageDirection direction,
        string? facebookMessageId = null,
        Dictionary<string, object>? metadata = null) : this()
    {
        Id = Guid.NewGuid();
        _senderId = senderId;
        _content = content;
        _messageType = messageType;
        MessageTypeId = messageType.Id;
        _direction = direction;
        DirectionId = direction.Id;
        _facebookMessageId = facebookMessageId;
        _metadata = metadata ?? new Dictionary<string, object>();
        _sentAt = DateTime.UtcNow;
    }

    /// <summary>
    /// EN: Add metadata to the message.
    /// VI: Thêm metadata vào tin nhắn.
    /// </summary>
    public void AddMetadata(string key, object value)
    {
        _metadata[key] = value;
    }
}
