using CashLineERP.Domain.Common;

namespace CashLineERP.Domain.Finance;

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

    public long AccountsReceivableInvoiceId { get; private set; }

    public long CustomerId { get; private set; }

    public DateOnly ActionDate { get; private set; }

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

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

    public DateOnly? PromiseToPayDate { get; private set; }

    public DateOnly? NextFollowUpDate { get; private set; }

    public string? Notes { get; private set; }

    public CollectionsFollowUpAction(
        long accountsReceivableInvoiceId,
        long customerId,
        DateOnly actionDate,
        string actionTypeCode,
        string outcomeCode,
        DateOnly? promiseToPayDate,
        DateOnly? nextFollowUpDate,
        string? notes,
        long actorUserId)
    {
        if (string.IsNullOrWhiteSpace(actionTypeCode))
        {
            throw new InvalidOperationException("Collections follow-up action type is required.");
        }

        if (string.IsNullOrWhiteSpace(outcomeCode))
        {
            throw new InvalidOperationException("Collections follow-up outcome code is required.");
        }

        if (promiseToPayDate.HasValue && promiseToPayDate.Value < actionDate)
        {
            throw new InvalidOperationException("Collections promise-to-pay date cannot be earlier than the action date.");
        }

        if (nextFollowUpDate.HasValue && nextFollowUpDate.Value < actionDate)
        {
            throw new InvalidOperationException("Collections next follow-up date cannot be earlier than the action date.");
        }

        AccountsReceivableInvoiceId = accountsReceivableInvoiceId;
        CustomerId = customerId;
        ActionDate = actionDate;
        ActionTypeCode = actionTypeCode.Trim().ToUpperInvariant();
        OutcomeCode = outcomeCode.Trim().ToUpperInvariant();
        PromiseToPayDate = promiseToPayDate;
        NextFollowUpDate = nextFollowUpDate;
        Notes = string.IsNullOrWhiteSpace(notes) ? null : notes.Trim();
        CreatedByUserId = actorUserId;
    }

    private CollectionsFollowUpAction()
    {
    }
}
