#nullable enable
using Baya.Application.Contracts.Invoices;
using Microsoft.Extensions.Options;

namespace Baya.Infrastructure.CrossCutting.Seams;

/// <summary>
/// Deterministic mock <see cref="IMoadianClient"/> (b11) — no external call. By default a submission leaves the
/// invoice at <c>moadian_status = pending</c> with no reference (the real reconciliation later flips it to
/// <c>registered</c>). Set <see cref="MoadianOptions.ForceRegistered"/> to have it return <c>registered</c> with
/// a deterministic fake 22-digit reference so the <c>registered</c>/reconciliation path is testable. The real
/// سامانه مودیان adapter (enrollment, the معاملات/invoice submission API, the 22-digit reference) swaps only this
/// registration.
/// </summary>
public sealed class MockMoadianClient(IOptions<SeamOptions> options) : IMoadianClient
{
    private readonly MoadianOptions _options = options.Value.Moadian;

    public ValueTask<MoadianSubmissionResult> SubmitAsync(InvoiceSubmission submission, CancellationToken cancellationToken = default)
    {
        if (!_options.ForceRegistered)
            return ValueTask.FromResult(new MoadianSubmissionResult(Baya.Domain.Entities.Invoices.MoadianStatus.Pending, null));

        // A deterministic 22-digit reference derived from the booking id (right-aligned, zero-padded).
        var reference = submission.BookingId.ToString().PadLeft(22, '0');
        return ValueTask.FromResult(new MoadianSubmissionResult(Baya.Domain.Entities.Invoices.MoadianStatus.Registered, reference));
    }
}
