// EN: Kitchen ticket entity for kitchen display.
// VI: Entity KitchenTicket cho hiển thị bếp.

using FnbEngine.Domain.Events;
using FnbEngine.Domain.SeedWork;

namespace FnbEngine.Domain.AggregatesModel.KitchenAggregate;

/// <summary>
/// EN: Kitchen ticket entity - represents an order item in the kitchen.
/// VI: Entity KitchenTicket - đại diện cho item đơn hàng trong bếp.
/// </summary>
public class KitchenTicket : Entity, IAggregateRoot
{
    private Guid _sessionId;
    private Guid _orderItemId;
    private Guid _productId;
    private string _itemName = null!;
    private string? _station; // Bar, Kitchen, Grill, etc.
    private int _priority;
    private int _quantity;
    private string _status = null!; // Pending, InProgress, Ready, Served
    private DateTime _createdAt;
    private DateTime? _completedAt;

    public Guid SessionId => _sessionId;
    public Guid OrderItemId => _orderItemId;

    /// <summary>
    /// EN: Product ID from Catalog Service (for recipe lookup).
    /// VI: ID san pham tu Catalog Service (de tra cuu cong thuc).
    /// </summary>
    public Guid ProductId => _productId;
    public string ItemName => _itemName;
    public string? Station => _station;
    public int Priority => _priority;

    /// <summary>
    /// EN: Quantity of items ordered.
    /// VI: So luong item da dat.
    /// </summary>
    public int Quantity => _quantity;
    public string Status => _status;
    public DateTime CreatedAt => _createdAt;
    public DateTime? CompletedAt => _completedAt;

    protected KitchenTicket()
    {
    }

    /// <summary>
    /// EN: Create a kitchen ticket with explicit product ID and quantity.
    ///     Product ID is REQUIRED to ensure correct recipe lookup and inventory deduction.
    /// VI: Tao phieu bep voi product ID tuong minh va so luong.
    ///     Product ID la BAT BUOC de dam bao tra cuu cong thuc va tru kho chinh xac.
    /// </summary>
    public KitchenTicket(Guid sessionId, Guid orderItemId, Guid productId, string itemName, int quantity = 1, string? station = null, int priority = 0)
    {
        Id = Guid.NewGuid();
        _sessionId = sessionId;
        _orderItemId = orderItemId;
        _productId = productId;
        _itemName = itemName;
        _quantity = quantity > 0 ? quantity : 1;
        _station = station;
        _priority = priority;
        _status = "Pending";
        _createdAt = DateTime.UtcNow;
    }

    public void MarkAsInProgress()
    {
        _status = "InProgress";
    }

    public void MarkAsReady()
    {
        _status = "Ready";
        _completedAt = DateTime.UtcNow;
    }

    /// <summary>
    /// EN: Mark ticket as served and raise domain event for inventory deduction.
    /// VI: Danh dau phieu da phuc vu va phat domain event de tru kho.
    /// </summary>
    public void MarkAsServed()
    {
        _status = "Served";

        // EN: Raise domain event for inventory auto-deduction.
        // VI: Phat domain event de tu dong tru kho.
        AddDomainEvent(new KitchenTicketServedDomainEvent(this));
    }
}
