using Stripe;

namespace JobTrackerApi.Services;

public interface IStripeBillingGateway
{
    Task<Stripe.Checkout.Session> CreateCheckoutAsync(string secretKey, Stripe.Checkout.SessionCreateOptions options, CancellationToken cancellationToken);
    Task<Stripe.BillingPortal.Session> CreatePortalAsync(string secretKey, Stripe.BillingPortal.SessionCreateOptions options, CancellationToken cancellationToken);
    Event ConstructEvent(string json, string signature, string webhookSecret);
    Task<Subscription> GetSubscriptionAsync(string secretKey, string subscriptionId, CancellationToken cancellationToken);
}

public sealed class StripeBillingGateway : IStripeBillingGateway
{
    public Task<Stripe.Checkout.Session> CreateCheckoutAsync(
        string secretKey,
        Stripe.Checkout.SessionCreateOptions options,
        CancellationToken cancellationToken)
        => new Stripe.Checkout.SessionService(new StripeClient(secretKey))
            .CreateAsync(options, cancellationToken: cancellationToken);

    public Task<Stripe.BillingPortal.Session> CreatePortalAsync(
        string secretKey,
        Stripe.BillingPortal.SessionCreateOptions options,
        CancellationToken cancellationToken)
        => new Stripe.BillingPortal.SessionService(new StripeClient(secretKey))
            .CreateAsync(options, cancellationToken: cancellationToken);

    public Event ConstructEvent(string json, string signature, string webhookSecret)
        => EventUtility.ConstructEvent(json, signature, webhookSecret);

    public Task<Subscription> GetSubscriptionAsync(string secretKey, string subscriptionId, CancellationToken cancellationToken)
        => new SubscriptionService(new StripeClient(secretKey))
            .GetAsync(subscriptionId, cancellationToken: cancellationToken);
}
