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

namespace CashLineERP.Domain.Finance;

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

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

    public long FromCashAccountId { get; private set; }

    public long ToCashAccountId { get; private set; }

    public DateOnly TransferDate { get; private set; }

    public string CurrencyCode { get; private set; } = "EGP";

    public decimal AmountAmount { get; private set; }

    public string? ReferenceNumber { get; private set; }

    public string? Notes { get; private set; }

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

    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 long? GeneralLedgerBatchId { get; private set; }

    public TreasuryTransfer(
        string transferNumber,
        long fromCashAccountId,
        long toCashAccountId,
        DateOnly transferDate,
        string currencyCode,
        decimal amountAmount,
        string? referenceNumber,
        string? notes)
    {
        if (fromCashAccountId == toCashAccountId)
        {
            throw new InvalidOperationException("Treasury transfer requires different source and destination cash accounts.");
        }

        if (amountAmount <= 0m)
        {
            throw new InvalidOperationException("Treasury transfer amount must be greater than zero.");
        }

        TransferNumber = transferNumber;
        FromCashAccountId = fromCashAccountId;
        ToCashAccountId = toCashAccountId;
        TransferDate = transferDate;
        CurrencyCode = currencyCode;
        AmountAmount = amountAmount;
        ReferenceNumber = referenceNumber;
        Notes = notes;
    }

    private TreasuryTransfer()
    {
    }

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

        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 treasury transfers can be approved.");
        }

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

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

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

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

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