using Leanetec.EConnect.Domain.Entities;
using MediatR;

namespace Leanetec.EConnect.Client.Articles;

/// <summary>
/// Extension methods for the <see cref="IMediator"/> interface to simplify working with <see cref="Article"/> entities.
/// </summary>
public static class GetExtensions
{
    #region GetArticleById

    /// <summary>
    /// Retrieves a single <see cref="Article"/> by its unique identifier.
    /// </summary>
    /// <param name="mediator">The <see cref="IMediator"/> instance used to send the request.</param>
    /// <param name="id">The unique identifier of the article to retrieve.</param>
    /// <param name="apiVersion">Optional API version to target the request.</param>
    /// <returns>
    /// A task that represents the asynchronous operation. The task result contains the matching <see cref="Article"/>, or <c>null</c> if not found.
    /// </returns>
    public static async Task<Article?> GetArticleByIdAsync(this IMediator mediator, int id, int? apiVersion = null)
    {
        var articles = await mediator.Send(new GetRequest(id) { ApiVersion = apiVersion });
        return articles.FirstOrDefault();
    }

    /// <summary>
    /// Retrieves a single <see cref="Article"/> by its unique identifier.
    /// </summary>
    /// <param name="mediator">The <see cref="IMediator"/> instance used to send the request.</param>
    /// <param name="id">The unique identifier of the article to retrieve.</param>
    /// <param name="apiVersion">Optional API version to target the request.</param>
    /// <returns>
    /// The matching <see cref="Article"/>, or <c>null</c> if not found.
    /// </returns>
    public static Article? GetArticleById(this IMediator mediator, int id, int? apiVersion = null)
    {
        var articles = mediator.Send(new GetRequest(id) { ApiVersion = apiVersion }).GetAwaiter().GetResult();
        return articles.FirstOrDefault();
    }

    #endregion

    #region GetAllArticles

    /// <summary>
    /// Retrieves all available <see cref="Article"/> entities.
    /// </summary>
    /// <param name="mediator">The <see cref="IMediator"/> instance used to send the request.</param>
    /// <param name="apiVersion">Optional API version to target the request.</param>
    /// <returns>
    /// A task that represents the asynchronous operation. The task result contains a collection of all available <see cref="Article"/> entities.
    /// </returns>
    public static Task<IEnumerable<Article>> GetAllArticlesAsync(this IMediator mediator, int? apiVersion = null) => mediator.Send(new GetRequest()
    {
        ApiVersion = apiVersion
    });

    /// <summary>
    /// Retrieves all available <see cref="Article"/> entities.
    /// </summary>
    /// <param name="mediator">The <see cref="IMediator"/> instance used to send the request.</param>
    /// <param name="apiVersion">Optional API version to target the request.</param>
    /// <returns>
    /// A collection of all available <see cref="Article"/> entities.
    /// </returns>
    public static IEnumerable<Article> GetAllArticles(this IMediator mediator, int? apiVersion = null) => mediator.Send(new GetRequest()
    {
        ApiVersion = apiVersion
    }).GetAwaiter().GetResult();

    #endregion
}
