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

namespace CashLineERP.Domain.Inventory;

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

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

    public long BranchId { get; private set; }

    public long WarehouseId { get; private set; }

    public DateOnly ControlDate { 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 TotalQuantityAffected { 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<InventoryStatusControlLine> Lines { get; private set; } = [];

    public InventoryStatusControl(
        string controlNumber,
        long branchId,
        long warehouseId,
        DateOnly controlDate,
        string reasonCode,
        string? notes)
    {
        ControlNumber = controlNumber;
        BranchId = branchId;
        WarehouseId = warehouseId;
        ControlDate = controlDate;
        ReasonCode = reasonCode;
        Notes = notes;
    }

    private InventoryStatusControl()
    {
    }

    public void AddLine(
        int lineNumber,
        long itemId,
        long? warehouseLocationId,
        decimal quantityAffected,
        string unitOfMeasureCode,
        decimal? unitCost,
        string currentStockStatus,
        string targetStockStatus,
        string? batchNumber,
        string? serialNumber,
        DateOnly? expiryDate,
        string? notes)
    {
        if (quantityAffected <= 0m)
        {
            throw new InvalidOperationException("Inventory status-control line quantity must be greater than zero.");
        }

        Lines.Add(new InventoryStatusControlLine(
            inventoryStatusControlId: Id,
            lineNumber: lineNumber,
            itemId: itemId,
            warehouseLocationId: warehouseLocationId,
            quantityAffected: quantityAffected,
            unitOfMeasureCode: unitOfMeasureCode,
            unitCost: unitCost,
            currentStockStatus: currentStockStatus,
            targetStockStatus: targetStockStatus,
            batchNumber: batchNumber,
            serialNumber: serialNumber,
            expiryDate: expiryDate,
            controlDate: ControlDate,
            notes: notes));

        TotalQuantityAffected = Lines.Sum(line => line.QuantityAffected);
    }

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

        if (Lines.Count == 0)
        {
            throw new InvalidOperationException("Stock-status control 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 stock-status controls can be approved.");
        }

        if (SubmittedByUserId == actorUserId)
        {
            throw new InvalidOperationException("Stock-status control 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("Stock-status control must be approved before posting.");
        }

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