using System.Text.Json.Nodes;
using Csob.Client;
using Csob.Models;

namespace Csob.Services;

/// <summary>PISP – single payment initiation, status/detail, cancellation and the sign (SCA) flow.</summary>
public sealed class PaymentsService
{
    private readonly CsobApiAccessor _accessor;

    public PaymentsService(CsobApiAccessor accessor) => _accessor = accessor;

    public Task<JsonNode?> InitiateAsync(PaymentInitiationRequest request, CancellationToken ct)
        => _accessor.Client.PostAsync(CsobApiPaths.Payments, request, ct);

    public Task<JsonNode?> DetailAsync(string id, CancellationToken ct)
        => _accessor.Client.GetAsync(CsobApiPaths.Payment(id), null, ct);

    public Task<JsonNode?> StatusAsync(string id, CancellationToken ct)
        => _accessor.Client.GetAsync(CsobApiPaths.PaymentStatus(id), null, ct);

    public Task<JsonNode?> CancelAsync(string id, CancellationToken ct)
        => _accessor.Client.DeleteAsync(CsobApiPaths.Payment(id), ct);

    /// <summary>Starts transaction authorization (SCA). Returns the redirect details for the PSU.</summary>
    public Task<JsonNode?> StartSignAsync(string id, string signId, JsonNode? body, CancellationToken ct)
        => _accessor.Client.PostAsync(CsobApiPaths.PaymentSign(id, signId), body, ct);

    /// <summary>Gets the current state of an authorization (sign) transaction.</summary>
    public Task<JsonNode?> SignStatusAsync(string id, string signId, CancellationToken ct)
        => _accessor.Client.GetAsync(CsobApiPaths.PaymentSign(id, signId), null, ct);

    /// <summary>Finalizes an authorization (sign) transaction.</summary>
    public Task<JsonNode?> FinalizeSignAsync(string id, string signId, JsonNode? body, CancellationToken ct)
        => _accessor.Client.PutAsync(CsobApiPaths.PaymentSign(id, signId), body, ct);
}
