using FluentAssertions;
using WhatsAppService.Domain.AggregatesModel.WhatsAppAccountAggregate;
using WhatsAppService.Domain.Exceptions;
using Xunit;

namespace WhatsAppService.UnitTests.Domain;

/// <summary>
/// EN: Unit tests for WhatsAppAccount aggregate.
/// VI: Unit tests cho aggregate WhatsAppAccount.
/// </summary>
public class WhatsAppAccountAggregateTests
{
    [Fact]
    public void Create_WithValidInput_ShouldStartInPendingStatus()
    {
        // Arrange
        var shopId = Guid.NewGuid();

        // Act
        var account = new WhatsAppAccount(shopId, "phone-1", "encrypted-token", "https://hook.example");

        // Assert
        account.Id.Should().NotBe(Guid.Empty);
        account.ShopId.Should().Be(shopId);
        account.Status.Should().Be(WhatsAppAccountStatus.Pending);
        account.AccessTokenEncrypted.Should().Be("encrypted-token");
        account.DomainEvents.Should().NotBeEmpty();
    }

    [Fact]
    public void Activate_FromPending_ShouldSetActiveStatus()
    {
        // Arrange
        var account = new WhatsAppAccount(Guid.NewGuid(), "phone-1", "encrypted-token");

        // Act
        account.Activate();

        // Assert
        account.Status.Should().Be(WhatsAppAccountStatus.Active);
    }

    [Fact]
    public void UpdateAccessToken_WithEmptyToken_ShouldThrowDomainException()
    {
        // Arrange
        var account = new WhatsAppAccount(Guid.NewGuid(), "phone-1", "encrypted-token");

        // Act
        var act = () => account.UpdateAccessToken(string.Empty);

        // Assert
        act.Should().Throw<WhatsAppAccountDomainException>()
            .WithMessage("Access token cannot be empty");
    }

    [Fact]
    public void Disconnect_ThenActivate_ShouldThrowDomainException()
    {
        // Arrange
        var account = new WhatsAppAccount(Guid.NewGuid(), "phone-1", "encrypted-token");
        account.Disconnect();

        // Act
        var act = () => account.Activate();

        // Assert
        act.Should().Throw<WhatsAppAccountDomainException>()
            .WithMessage("Cannot activate a disconnected account");
    }
}
