using foodmarket.Domain.Catalog;
using foodmarket.Domain.Common;

namespace foodmarket.Domain.Inventory;

// Immutable, append-only journal of every stock change.
// Stock table is a materialized aggregate over this journal.
public class StockMovement : TenantEntity
{
    public Guid ProductId { get; set; }
    public Product Product { get; set; } = null!;

    public Guid StoreId { get; set; }
    public Store Store { get; set; } = null!;

    /// <summary>Signed quantity: positive = receipt, negative = issue.</summary>
    public decimal Quantity { get; set; }

    /// <summary>Per-unit cost at the time of movement (optional). Used for cost rollup / P&amp;L.</summary>
    public decimal? UnitCost { get; set; }

    public MovementType Type { get; set; }

    /// <summary>Source document discriminator, e.g. "supply", "retail-sale", "write-off", "enter", "transfer-out".</summary>
    public string DocumentType { get; set; } = "";

    public Guid? DocumentId { get; set; }
    public string? DocumentNumber { get; set; }

    public DateTime OccurredAt { get; set; } = DateTime.UtcNow;

    public Guid? CreatedByUserId { get; set; }
    public string? Notes { get; set; }
}

public enum MovementType
{
    Initial = 0,
    Supply = 1,               // приёмка от поставщика
    RetailSale = 2,           // розничная продажа
    WholesaleSale = 3,        // оптовая отгрузка
    CustomerReturn = 4,       // возврат покупателя
    SupplierReturn = 5,       // возврат поставщику
    TransferOut = 6,          // перемещение со склада
    TransferIn = 7,           // перемещение на склад
    WriteOff = 8,             // списание
    Enter = 9,                // оприходование
    InventoryAdjustment = 10, // корректировка по результату инвентаризации
}
