using System;
using System.Collections.Generic;
using MongoDB.Bson.Serialization.Attributes;

namespace Knot.Modules.Messaging.Domain;

[BsonDiscriminator("MediaMessage")]
public class MediaMessage : Message
{
    public override string Type => MediaType.ToString().ToLower();
    public override string? Content { get; protected set; } // Map to Caption
    public string? Caption { get => Content; private set => Content = value; }
    public MediaType MediaType { get; private set; } // image, video, file, voice

    private List<Media> _media = new();
    public IReadOnlyCollection<Media> Media => _media.AsReadOnly();

    private MediaMessage() : base() 
    { 
        MediaType = MediaType.File;
    }

    public MediaMessage(
        Guid id, 
        Guid chatId, 
        Guid senderId, 
        MediaType mediaType,
        string? caption,
        Guid? replyToId, 
        Guid? forwardedFromId, 
        DateTime createdAt, 
        bool isImported) 
        : base(id, chatId, senderId, replyToId, forwardedFromId, createdAt, isImported)
    {
        MediaType = mediaType;
        Caption = caption;

        if (!isImported) 
        {
            RaiseDomainEvent(new MessageSentDomainEvent(Id, ChatId, SenderId, Caption));
        }
    }

    public void AddMedia(MediaType type, string url, string? filename, long? size)
    {
        _media.Add(new Domain.Media(Id, type.ToString().ToLower(), url, filename, size));
    }

    public void AppendImportedCaption(string additionalCaption)
    {
        if (string.IsNullOrEmpty(Caption))
        {
            Caption = additionalCaption;
        }
        else
        {
            Caption += "\n" + additionalCaption;
        }
    }

    public override void Edit(string newCaption)
    {
        base.Edit(newCaption);
    }

    public override void Delete()
    {
        Caption = null;
        base.Delete();
    }
}


