#nullable enable
using Baya.Application.Contracts.Payments;

namespace Baya.Infrastructure.CrossCutting.Seams.Real;

/// <summary>
/// Shared base for the real HTTP BNPL adapters (refinement-phase-8, 6.2). Owns the <b>currency boundary</b> — the
/// one place Toman↔IRR conversion is allowed — via <see cref="ICurrencyNormalizer"/>: every amount leaving the
/// domain is turned into the provider's wire currency by <see cref="ToWire"/>, and every amount coming back is
/// normalized to IRR by <see cref="FromWire"/>. Conversion happens <b>only</b> here, never internally. Concrete
/// adapters (<see cref="SnappPayBnplProvider"/>, <see cref="DigipayBnplProvider"/>) implement the provider's verb
/// set; a real deployment reads the provider's wire currency from <c>Seams:Bnpl:WireCurrency</c> (SnappPay/Digipay
/// settle in Rial, so the default is a pass-through).
/// </summary>
public abstract class HttpBnplProviderBase(HttpClient httpClient, ICurrencyNormalizer currency, string wireCurrency)
{
    protected HttpClient Http { get; } = httpClient;

    /// <summary>Domain IRR → the provider's wire amount (Rial pass-through, or Toman when the provider speaks Toman).</summary>
    protected long ToWire(long amountIrr)
        => string.Equals(wireCurrency, "TOMAN", StringComparison.OrdinalIgnoreCase)
            ? currency.ToDisplayToman(amountIrr)
            : amountIrr;

    /// <summary>A provider wire amount → domain IRR (the only inbound conversion point).</summary>
    protected long FromWire(long wireAmount) => currency.ToIrr(wireAmount, wireCurrency);
}
