using FnbEngine.Domain.SeedWork;

namespace FnbEngine.Domain.AggregatesModel.RecipeAggregate;

public class Recipe : Entity, IAggregateRoot
{
    private Guid _shopId;
    private Guid _productId;
    private string _name = null!;
    private string? _instructions;
    private int _prepTimeMinutes;
    private bool _isActive;
    private DateTime _createdAt;
    private DateTime? _updatedAt;
    private readonly List<RecipeIngredient> _ingredients = new();

    public Guid ShopId => _shopId;
    public Guid ProductId => _productId;
    public string Name => _name;
    public string? Instructions => _instructions;
    public int PrepTimeMinutes => _prepTimeMinutes;
    public bool IsActive => _isActive;
    public DateTime CreatedAt => _createdAt;
    public DateTime? UpdatedAt => _updatedAt;
    public IReadOnlyList<RecipeIngredient> Ingredients => _ingredients.AsReadOnly();

    protected Recipe() { }

    public Recipe(Guid shopId, Guid productId, string name, string? instructions, int prepTimeMinutes)
    {
        Id = Guid.NewGuid();
        _shopId = shopId;
        _productId = productId;
        _name = name;
        _instructions = instructions;
        _prepTimeMinutes = prepTimeMinutes;
        _isActive = true;
        _createdAt = DateTime.UtcNow;
    }

    public void Update(Guid productId, string name, string? instructions, int prepTimeMinutes)
    {
        _productId = productId;
        _name = name;
        _instructions = instructions;
        _prepTimeMinutes = prepTimeMinutes;
        _updatedAt = DateTime.UtcNow;
    }

    // EN: Add ingredient with optional inventory item link for COGS calculation.
    // VI: Thêm nguyên liệu với liên kết tùy chọn đến mặt hàng tồn kho để tính giá vốn.
    public void AddIngredient(string ingredientName, decimal quantity, string unit, decimal costPerUnit,
        Guid? inventoryItemId = null, decimal quantityPerServing = 0)
    {
        _ingredients.Add(new RecipeIngredient(Id, ingredientName, quantity, unit, costPerUnit, inventoryItemId, quantityPerServing));
    }

    public void ClearIngredients()
    {
        _ingredients.Clear();
        _updatedAt = DateTime.UtcNow;
    }

    public void Deactivate()
    {
        _isActive = false;
        _updatedAt = DateTime.UtcNow;
    }
}
