// EN: Session entity for dining sessions.
// VI: Entity Session cho phiên ăn uống.

using FnbEngine.Domain.Exceptions;
using FnbEngine.Domain.SeedWork;

namespace FnbEngine.Domain.AggregatesModel.SessionAggregate;

/// <summary>
/// EN: Session entity - represents a dining session at a table.
/// VI: Entity Session - đại diện cho phiên ăn tại bàn.
/// </summary>
public class Session : Entity, IAggregateRoot
{
    private Guid _tableId;
    private Guid _shopId;
    private int _guestCount;
    private DateTime _startedAt;
    private DateTime? _closedAt;
    private string _status = null!; // Active, Closed

    public Guid TableId => _tableId;
    public Guid ShopId => _shopId;
    public int GuestCount => _guestCount;
    public DateTime StartedAt => _startedAt;
    public DateTime? ClosedAt => _closedAt;
    public string Status => _status;

    protected Session()
    {
    }

    public Session(Guid tableId, Guid shopId, int guestCount = 1)
    {
        if (tableId == Guid.Empty)
            throw new DomainException("Table ID cannot be empty");
        if (shopId == Guid.Empty)
            throw new DomainException("Shop ID cannot be empty");
        if (guestCount <= 0)
            throw new DomainException("Guest count must be greater than zero");

        Id = Guid.NewGuid();
        _tableId = tableId;
        _shopId = shopId;
        _guestCount = guestCount;
        _startedAt = DateTime.UtcNow;
        _status = "Active";
    }

    public void Close()
    {
        if (_status == "Closed")
            throw new DomainException("Session is already closed");

        _closedAt = DateTime.UtcNow;
        _status = "Closed";
    }
}
