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

namespace CashLineERP.Domain.Inventory;

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

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

    public long BranchId { get; private set; }

    public long FromWarehouseId { get; private set; }

    public long ToWarehouseId { get; private set; }

    public DateOnly TransferDate { get; private set; }

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

    public string? Notes { get; private set; }

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

    public decimal TotalTransferQuantity { get; private set; }

    public DateTime? SubmittedAtUtc { get; private set; }

    public long? SubmittedByUserId { get; private set; }

    public DateTime? ApprovedAtUtc { get; private set; }

    public long? ApprovedByUserId { get; private set; }

    public DateTime? PostedAtUtc { get; private set; }

    public long? PostedByUserId { get; private set; }

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

    public WarehouseTransfer(
        string transferNumber,
        long branchId,
        long fromWarehouseId,
        long toWarehouseId,
        DateOnly transferDate,
        string reasonCode,
        string? notes)
    {
        if (fromWarehouseId == toWarehouseId)
        {
            throw new InvalidOperationException("Warehouse transfer requires different source and destination warehouses.");
        }

        TransferNumber = transferNumber;
        BranchId = branchId;
        FromWarehouseId = fromWarehouseId;
        ToWarehouseId = toWarehouseId;
        TransferDate = transferDate;
        ReasonCode = reasonCode;
        Notes = notes;
    }

    private WarehouseTransfer()
    {
    }

    public void AddLine(
        int lineNumber,
        long itemId,
        long? fromWarehouseLocationId,
        long? toWarehouseLocationId,
        decimal quantityTransferred,
        string unitOfMeasureCode,
        decimal? unitCost,
        string stockStatus,
        string? batchNumber,
        string? serialNumber,
        string? notes)
    {
        if (quantityTransferred <= 0m)
        {
            throw new InvalidOperationException("Warehouse transfer quantity must be greater than zero.");
        }

        Lines.Add(new WarehouseTransferLine(
            warehouseTransferId: Id,
            lineNumber: lineNumber,
            itemId: itemId,
            fromWarehouseLocationId: fromWarehouseLocationId,
            toWarehouseLocationId: toWarehouseLocationId,
            quantityTransferred: quantityTransferred,
            unitOfMeasureCode: unitOfMeasureCode,
            unitCost: unitCost,
            stockStatus: stockStatus,
            batchNumber: batchNumber,
            serialNumber: serialNumber,
            notes: notes));

        TotalTransferQuantity = Lines.Sum(line => line.QuantityTransferred);
    }

    public void Submit(long actorUserId)
    {
        if (StatusId != WorkflowStatusSeedIds.Draft && StatusId != WorkflowStatusSeedIds.Returned)
        {
            throw new InvalidOperationException("Only draft or returned warehouse transfers can be submitted.");
        }

        if (Lines.Count == 0)
        {
            throw new InvalidOperationException("Warehouse transfer must contain at least one line before submission.");
        }

        StatusId = WorkflowStatusSeedIds.Submitted;
        SubmittedAtUtc = DateTime.UtcNow;
        SubmittedByUserId = actorUserId;
        Touch(actorUserId);
    }

    public void Approve(long actorUserId)
    {
        if (StatusId == WorkflowStatusSeedIds.Approved)
        {
            return;
        }

        if (StatusId != WorkflowStatusSeedIds.Submitted)
        {
            throw new InvalidOperationException("Only submitted warehouse transfers can be approved.");
        }

        if (SubmittedByUserId == actorUserId)
        {
            throw new InvalidOperationException("Warehouse transfer maker-checker control prohibits self-approval.");
        }

        StatusId = WorkflowStatusSeedIds.Approved;
        ApprovedAtUtc = DateTime.UtcNow;
        ApprovedByUserId = actorUserId;
        Touch(actorUserId);
    }

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

        if (StatusId != WorkflowStatusSeedIds.Approved)
        {
            throw new InvalidOperationException("Warehouse transfer must be approved before posting.");
        }

        StatusId = WorkflowStatusSeedIds.Posted;
        PostedAtUtc = DateTime.UtcNow;
        PostedByUserId = actorUserId;
        Touch(actorUserId);
    }
}
