using CashLineERP.Domain.Common;
using CashLineERP.SharedKernel.Inventory;

namespace CashLineERP.Domain.Inventory;

public sealed class InventoryAdjustmentLine : Entity
{
    public long Id { get; private set; }

    public long InventoryAdjustmentId { get; private set; }

    public int LineNumber { get; private set; }

    public long ItemId { get; private set; }

    public long? WarehouseLocationId { get; private set; }

    public decimal QuantityDelta { get; private set; }

    public string UnitOfMeasureCode { get; private set; } = string.Empty;

    public decimal UnitCost { get; private set; }

    public decimal LineAmount { get; private set; }

    public string StockStatus { get; private set; } = "AVAILABLE";

    public string? BatchNumber { get; private set; }

    public string? SerialNumber { get; private set; }

    public string? Notes { get; private set; }

    public InventoryAdjustmentLine(
        long inventoryAdjustmentId,
        int lineNumber,
        long itemId,
        long? warehouseLocationId,
        decimal quantityDelta,
        string unitOfMeasureCode,
        decimal unitCost,
        string stockStatus,
        string? batchNumber,
        string? serialNumber,
        string? notes)
    {
        LotSerialTrackingGuard.EnsureSerialQuantity(serialNumber, Math.Abs(quantityDelta), "Inventory adjustment line");
        InventoryAdjustmentId = inventoryAdjustmentId;
        LineNumber = lineNumber;
        ItemId = itemId;
        WarehouseLocationId = warehouseLocationId;
        QuantityDelta = quantityDelta;
        UnitOfMeasureCode = unitOfMeasureCode;
        UnitCost = unitCost;
        LineAmount = Math.Abs(quantityDelta * unitCost);
        StockStatus = InventoryStockStatusPolicy.Normalize(stockStatus);
        BatchNumber = LotSerialTrackingGuard.NormalizeTrackingCode(batchNumber);
        SerialNumber = LotSerialTrackingGuard.NormalizeTrackingCode(serialNumber);
        Notes = notes;
    }

    private InventoryAdjustmentLine()
    {
    }
}
