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

namespace CashLineERP.Domain.Inventory;

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

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

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

    public long BranchId { get; private set; }

    public long ItemId { get; private set; }

    public long? FromWarehouseId { get; private set; }

    public long? FromWarehouseLocationId { get; private set; }

    public long? ToWarehouseId { get; private set; }

    public long? ToWarehouseLocationId { get; private set; }

    public string? BatchNumber { get; private set; }

    public string? SerialNumber { get; private set; }

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

    public decimal QuantityMoved { get; private set; }

    public decimal? UnitCost { get; private set; }

    public DateTime MovedAtUtc { get; private set; }

    public long StatusId { get; private set; } = WorkflowStatusSeedIds.Posted;

    public InventoryMovement(
        string movementNumber,
        string movementType,
        long branchId,
        long itemId,
        long? fromWarehouseId,
        long? fromWarehouseLocationId,
        long? toWarehouseId,
        long? toWarehouseLocationId,
        string? batchNumber,
        string? serialNumber,
        string stockStatus,
        decimal quantityMoved,
        decimal? unitCost,
        DateTime movedAtUtc)
    {
        LotSerialTrackingGuard.EnsureSerialQuantity(serialNumber, quantityMoved, "Inventory movement");
        MovementNumber = movementNumber;
        MovementType = movementType;
        BranchId = branchId;
        ItemId = itemId;
        FromWarehouseId = fromWarehouseId;
        FromWarehouseLocationId = fromWarehouseLocationId;
        ToWarehouseId = toWarehouseId;
        ToWarehouseLocationId = toWarehouseLocationId;
        BatchNumber = LotSerialTrackingGuard.NormalizeTrackingCode(batchNumber);
        SerialNumber = LotSerialTrackingGuard.NormalizeTrackingCode(serialNumber);
        StockStatus = InventoryStockStatusPolicy.Normalize(stockStatus);
        QuantityMoved = quantityMoved;
        UnitCost = unitCost;
        MovedAtUtc = movedAtUtc;
    }

    private InventoryMovement()
    {
    }
}
