using MediatR;

namespace Leanetec.EConnect.Client.Alive;

public static class GetExtensions
{
    /// <summary>
    /// Checks if the application is alive by sending a <see cref="GetRequest"/> via MediatR.
    /// </summary>
    /// <param name="mediator">The MediatR instance to send the request through.</param>
    /// <param name="role">Optional role of the logged-in user.</param>
    /// <param name="apiVersion">Optional API version.</param>
    /// <returns>A <see cref="Task{Boolean}"/> representing whether the application is alive.</returns>
    public static Task<bool> IsAliveAsync(this IMediator mediator, Role? role = null, int? apiVersion = null) =>
        mediator.Send(new GetRequest(role)
        {
            ApiVersion = apiVersion
        });

    /// <summary>
    /// Checks if the application is alive by sending a <see cref="GetRequest"/> synchronously via MediatR.
    /// </summary>
    /// <param name="mediator">The MediatR instance to send the request through.</param>
    /// <param name="role">Optional role of the logged-in user.</param>
    /// <param name="apiVersion">Optional API version.</param>
    /// <returns>True if the application is alive; otherwise, false.</returns>
    public static bool IsAlive(this IMediator mediator, Role? role = null, int? apiVersion = null) =>
        mediator.Send(new GetRequest(role)
        {
            ApiVersion = apiVersion
        }).GetAwaiter().GetResult();
}
