using FluentAssertions;
using MktZaloService.Domain.AggregatesModel.ConversationAggregate;
using MktZaloService.Domain.Enums;
using MktZaloService.Domain.Exceptions;
using Xunit;

namespace MktZaloService.UnitTests.Domain;

/// <summary>
/// EN: Unit tests for conversation aggregate behavior.
/// VI: Unit tests cho hành vi aggregate conversation.
/// </summary>
public class ConversationAggregateTests
{
    [Fact]
    public void CreateConversation_ShouldInitializeAsActive()
    {
        // Act
        var conversation = new Conversation("zalo-user-1", Guid.NewGuid());

        // Assert
        conversation.Status.Should().Be(ConversationStatus.Active);
        conversation.MessageCount.Should().Be(0);
        conversation.EndedAt.Should().BeNull();
    }

    [Fact]
    public void AddMessage_ShouldIncreaseCountAndUpdatePreview()
    {
        // Arrange
        var conversation = new Conversation("zalo-user-2", Guid.NewGuid());

        // Act
        var message = conversation.AddMessage(MessageType.Text, "Xin chào Zalo", MessageDirection.Incoming, false);

        // Assert
        message.Should().NotBeNull();
        conversation.MessageCount.Should().Be(1);
        conversation.LastMessagePreview.Should().Be("Xin chào Zalo");
        conversation.LastMessageAt.Should().NotBeNull();
    }

    [Fact]
    public void AddMessage_WhenClosed_ShouldThrowConversationClosedException()
    {
        // Arrange
        var conversation = new Conversation("zalo-user-3", Guid.NewGuid());
        conversation.Close();

        // Act
        var action = () => conversation.AddMessage(MessageType.Text, "Should fail", MessageDirection.Incoming, false);

        // Assert
        action.Should().Throw<ConversationClosedException>();
    }
}
