namespace CartWise.Domain.Entities;

public class PurchaseItem
{
    public Guid PurchaseItemId { get; private set; }

    public Guid PurchaseId { get; private set; }

    public Guid? ProductId { get; private set; }

    public Guid? GroceryConceptId { get; private set; }

    public Guid? ShoppingListItemId { get; private set; }

    public string Description { get; private set; } = null!;

    public decimal Quantity { get; private set; }

    public string? Unit { get; private set; }

    public decimal LinePrice { get; private set; }

    public decimal? UnitPrice { get; private set; }

    public DateTime CreatedUtc { get; private set; }

    private PurchaseItem()
    {
    }

    internal PurchaseItem(
        Guid purchaseId,
        string description,
        decimal linePrice,
        DateTime createdUtc,
        decimal quantity = 1m,
        string? unit = null,
        Guid? productId = null,
        Guid? groceryConceptId = null,
        Guid? shoppingListItemId = null,
        decimal? unitPrice = null)
    {
        if (purchaseId == Guid.Empty)
        {
            throw new ArgumentException("PurchaseId is required.", nameof(purchaseId));
        }

        if (string.IsNullOrWhiteSpace(description))
        {
            throw new ArgumentException("Description is required.", nameof(description));
        }

        if (quantity <= 0)
        {
            throw new ArgumentOutOfRangeException(nameof(quantity), "Quantity must be greater than zero.");
        }

        if (linePrice < 0)
        {
            throw new ArgumentOutOfRangeException(nameof(linePrice), "LinePrice cannot be negative.");
        }

        PurchaseItemId = Guid.NewGuid();
        PurchaseId = purchaseId;
        Description = description.Trim();
        Quantity = quantity;
        Unit = unit;
        LinePrice = linePrice;
        UnitPrice = unitPrice;
        ProductId = productId;
        GroceryConceptId = groceryConceptId;
        ShoppingListItemId = shoppingListItemId;
        CreatedUtc = createdUtc;
    }
}
