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

namespace CashLineERP.Domain.Procurement;

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

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

    public long BranchId { get; private set; }

    public long PurchaseOrderId { get; private set; }

    public long SupplierId { get; private set; }

    public DateTime ReceivedAtUtc { get; private set; }

    public long ReceivedByUserId { get; private set; }

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

    public string InspectionStatus { get; private set; } = "RECEIVED";

    public List<GoodsReceiptLine> Lines { get; private set; } = [];

    public GoodsReceipt(
        string goodsReceiptNumber,
        long branchId,
        long purchaseOrderId,
        long supplierId,
        DateTime receivedAtUtc,
        long receivedByUserId,
        string inspectionStatus = "RECEIVED")
    {
        GoodsReceiptNumber = goodsReceiptNumber;
        BranchId = branchId;
        PurchaseOrderId = purchaseOrderId;
        SupplierId = supplierId;
        ReceivedAtUtc = receivedAtUtc;
        ReceivedByUserId = receivedByUserId;
        InspectionStatus = inspectionStatus;
    }

    private GoodsReceipt()
    {
    }

    public void AddLine(
        int lineNumber,
        long purchaseOrderLineId,
        long itemId,
        long warehouseId,
        long? warehouseLocationId,
        decimal quantityReceived,
        decimal quantityAccepted,
        decimal quantityRejected,
        string? batchNumber,
        string? serialNumber)
    {
        Lines.Add(new GoodsReceiptLine(
            goodsReceiptId: Id,
            lineNumber: lineNumber,
            purchaseOrderLineId: purchaseOrderLineId,
            itemId: itemId,
            warehouseId: warehouseId,
            warehouseLocationId: warehouseLocationId,
            quantityReceived: quantityReceived,
            quantityAccepted: quantityAccepted,
            quantityRejected: quantityRejected,
            batchNumber: batchNumber,
            serialNumber: serialNumber));
    }

    public void Post(long actorUserId)
    {
        StatusId = WorkflowStatusSeedIds.Posted;
        Touch(actorUserId);
    }
}
