using CashLineERP.Domain.Common;

namespace CashLineERP.Domain.IslamicFinance.Murabaha;

public sealed class IslamicFinanceMurabahaAsset : Entity
{
    public long Id { get; private set; }
    public long MurabahaCaseId { get; private set; }
    public string AssetDescription { get; private set; } = string.Empty;
    public long? SupplierPartyId { get; private set; }
    public string? PurchaseReferenceNumber { get; private set; }
    public decimal PurchaseCostAmount { get; private set; }

    public IslamicFinanceMurabahaAsset(
        long murabahaCaseId,
        string assetDescription,
        long? supplierPartyId,
        string? purchaseReferenceNumber,
        decimal purchaseCostAmount)
    {
        if (murabahaCaseId <= 0)
        {
            throw new InvalidOperationException("Islamic Finance Murabaha asset requires a valid Murabaha case identifier.");
        }

        if (string.IsNullOrWhiteSpace(assetDescription))
        {
            throw new InvalidOperationException("Islamic Finance Murabaha asset description is required.");
        }

        if (supplierPartyId.HasValue && supplierPartyId.Value <= 0)
        {
            throw new InvalidOperationException("Islamic Finance Murabaha asset supplier identifier must be positive when provided.");
        }

        if (purchaseCostAmount <= 0m)
        {
            throw new InvalidOperationException("Islamic Finance Murabaha asset purchase cost must be positive.");
        }

        MurabahaCaseId = murabahaCaseId;
        AssetDescription = assetDescription.Trim();
        SupplierPartyId = supplierPartyId;
        PurchaseReferenceNumber = string.IsNullOrWhiteSpace(purchaseReferenceNumber)
            ? null
            : purchaseReferenceNumber.Trim().ToUpperInvariant();
        PurchaseCostAmount = purchaseCostAmount;
    }

    private IslamicFinanceMurabahaAsset()
    {
    }
}
