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

namespace CashLineERP.Domain.Inventory;

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

    public long InventoryStatusControlId { get; private set; }

    public int LineNumber { get; private set; }

    public long ItemId { get; private set; }

    public long? WarehouseLocationId { get; private set; }

    public decimal QuantityAffected { get; private set; }

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

    public decimal? UnitCost { get; private set; }

    public string CurrentStockStatus { get; private set; } = InventoryStockStatusPolicy.Available;

    public string TargetStockStatus { get; private set; } = InventoryStockStatusPolicy.Quarantine;

    public string? BatchNumber { get; private set; }

    public string? SerialNumber { get; private set; }

    public DateOnly? ExpiryDate { get; private set; }

    public string? Notes { get; private set; }

    public InventoryStatusControlLine(
        long inventoryStatusControlId,
        int lineNumber,
        long itemId,
        long? warehouseLocationId,
        decimal quantityAffected,
        string unitOfMeasureCode,
        decimal? unitCost,
        string currentStockStatus,
        string targetStockStatus,
        string? batchNumber,
        string? serialNumber,
        DateOnly? expiryDate,
        DateOnly controlDate,
        string? notes)
    {
        LotSerialTrackingGuard.EnsureSerialQuantity(serialNumber, quantityAffected, "Inventory status-control line");
        InventoryStockStatusPolicy.EnsureTransitionAllowed(currentStockStatus, targetStockStatus, controlDate, expiryDate, "Inventory status-control line");

        InventoryStatusControlId = inventoryStatusControlId;
        LineNumber = lineNumber;
        ItemId = itemId;
        WarehouseLocationId = warehouseLocationId;
        QuantityAffected = quantityAffected;
        UnitOfMeasureCode = unitOfMeasureCode;
        UnitCost = unitCost;
        CurrentStockStatus = InventoryStockStatusPolicy.Normalize(currentStockStatus);
        TargetStockStatus = InventoryStockStatusPolicy.Normalize(targetStockStatus);
        BatchNumber = LotSerialTrackingGuard.NormalizeTrackingCode(batchNumber);
        SerialNumber = LotSerialTrackingGuard.NormalizeTrackingCode(serialNumber);
        ExpiryDate = expiryDate;
        Notes = notes;
    }

    private InventoryStatusControlLine()
    {
    }
}
