using FluentAssertions;
using FnbEngine.Domain.AggregatesModel.SessionAggregate;
using FnbEngine.Domain.Exceptions;
using Xunit;

namespace FnbEngine.UnitTests.Domain;

/// <summary>
/// EN: Unit tests for Session aggregate behavior.
/// VI: Unit tests cho hanh vi aggregate Session.
/// </summary>
public class SessionTests
{
    private static readonly Guid ValidTableId = Guid.NewGuid();
    private static readonly Guid ValidShopId = Guid.NewGuid();

    [Fact]
    public void Constructor_WithValidData_ShouldCreateActiveSession()
    {
        // Act
        var session = new Session(ValidTableId, ValidShopId, 4);

        // Assert
        session.Id.Should().NotBeEmpty();
        session.TableId.Should().Be(ValidTableId);
        session.ShopId.Should().Be(ValidShopId);
        session.GuestCount.Should().Be(4);
        session.Status.Should().Be("Active");
        session.StartedAt.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(5));
        session.ClosedAt.Should().BeNull();
    }

    [Fact]
    public void Constructor_WithDefaultGuestCount_ShouldDefaultToOne()
    {
        // Act
        var session = new Session(ValidTableId, ValidShopId);

        // Assert
        session.GuestCount.Should().Be(1);
    }

    [Fact]
    public void Constructor_WithEmptyTableId_ShouldThrowDomainException()
    {
        // Act
        var action = () => new Session(Guid.Empty, ValidShopId, 2);

        // Assert
        action.Should().Throw<DomainException>()
            .WithMessage("*Table ID*");
    }

    [Fact]
    public void Constructor_WithEmptyShopId_ShouldThrowDomainException()
    {
        // Act
        var action = () => new Session(ValidTableId, Guid.Empty, 2);

        // Assert
        action.Should().Throw<DomainException>()
            .WithMessage("*Shop ID*");
    }

    [Fact]
    public void Constructor_WithZeroGuestCount_ShouldThrowDomainException()
    {
        // Act
        var action = () => new Session(ValidTableId, ValidShopId, 0);

        // Assert
        action.Should().Throw<DomainException>()
            .WithMessage("*Guest count*");
    }

    [Fact]
    public void Constructor_WithNegativeGuestCount_ShouldThrowDomainException()
    {
        // Act
        var action = () => new Session(ValidTableId, ValidShopId, -1);

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

    [Fact]
    public void Close_WhenActive_ShouldSetStatusToClosedAndSetClosedAt()
    {
        // Arrange
        var session = new Session(ValidTableId, ValidShopId, 2);

        // Act
        session.Close();

        // Assert
        session.Status.Should().Be("Closed");
        session.ClosedAt.Should().NotBeNull();
        session.ClosedAt.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(5));
    }

    [Fact]
    public void Close_WhenAlreadyClosed_ShouldThrowDomainException()
    {
        // Arrange
        var session = new Session(ValidTableId, ValidShopId, 2);
        session.Close();

        // Act
        var action = () => session.Close();

        // Assert
        action.Should().Throw<DomainException>()
            .WithMessage("*already closed*");
    }
}
