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

namespace CashLineERP.Domain.Inventory;

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

    public long StockCountId { get; private set; }

    public int LineNumber { get; private set; }

    public long ItemId { get; private set; }

    public long? WarehouseLocationId { get; private set; }

    public decimal BookQuantity { get; private set; }

    public decimal CountedQuantity { get; private set; }

    public decimal VarianceQuantity { get; private set; }

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

    public decimal UnitCost { get; private set; }

    public decimal VarianceAmount { 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 StockCountLine(
        long stockCountId,
        int lineNumber,
        long itemId,
        long? warehouseLocationId,
        decimal bookQuantity,
        decimal countedQuantity,
        string unitOfMeasureCode,
        decimal unitCost,
        string stockStatus,
        string? batchNumber,
        string? serialNumber,
        string? notes)
    {
        LotSerialTrackingGuard.EnsureSerialCountState(serialNumber, bookQuantity, countedQuantity, "Stock count line");
        StockCountId = stockCountId;
        LineNumber = lineNumber;
        ItemId = itemId;
        WarehouseLocationId = warehouseLocationId;
        BookQuantity = bookQuantity;
        CountedQuantity = countedQuantity;
        VarianceQuantity = countedQuantity - bookQuantity;
        UnitOfMeasureCode = unitOfMeasureCode;
        UnitCost = unitCost;
        VarianceAmount = Math.Abs(VarianceQuantity * unitCost);
        StockStatus = stockStatus;
        BatchNumber = LotSerialTrackingGuard.NormalizeTrackingCode(batchNumber);
        SerialNumber = LotSerialTrackingGuard.NormalizeTrackingCode(serialNumber);
        Notes = notes;
    }

    private StockCountLine()
    {
    }
}
