namespace CashLineERP.Domain.IslamicFinance.Settlements;

public sealed class IslamicFinanceSettlementLine
{
    public long Id { get; private set; }
    public long IslamicFinanceSettlementHeaderId { get; private set; }
    public long MurabahaDisbursementId { get; private set; }
    public decimal SettledAmount { get; private set; }
    public string StatusCode { get; private set; } = "LINKED";
    public DateTime CreatedAtUtc { get; private set; } = DateTime.UtcNow;
    public long? CreatedByUserId { get; private set; }
    public DateTime? UpdatedAtUtc { get; private set; }
    public long? UpdatedByUserId { get; private set; }

    public IslamicFinanceSettlementLine(
        long islamicFinanceSettlementHeaderId,
        long murabahaDisbursementId,
        decimal settledAmount,
        long? createdByUserId = null)
    {
        if (islamicFinanceSettlementHeaderId <= 0 || murabahaDisbursementId <= 0)
        {
            throw new InvalidOperationException("Islamic Finance settlement line requires valid settlement and disbursement identifiers.");
        }

        if (settledAmount <= 0m)
        {
            throw new InvalidOperationException("Islamic Finance settlement line requires a positive settled amount.");
        }

        IslamicFinanceSettlementHeaderId = islamicFinanceSettlementHeaderId;
        MurabahaDisbursementId = murabahaDisbursementId;
        SettledAmount = settledAmount;
        CreatedByUserId = createdByUserId;
    }

    private IslamicFinanceSettlementLine()
    {
    }

    public void MarkSettled(long actorUserId)
    {
        if (string.Equals(StatusCode, "SETTLED", StringComparison.OrdinalIgnoreCase))
        {
            return;
        }

        StatusCode = "SETTLED";
        UpdatedAtUtc = DateTime.UtcNow;
        UpdatedByUserId = actorUserId;
    }
}
