using Pilz.Net.Api;
using Pilz.Net.Api.Messages;
using System.Net;

namespace Pilz.Net.Extensions;

public static class ApiMessageExtensions
{
    public static ApiResult ToItemResult<T>(this T? @this)
    {
        return @this.ToItemResult(HttpStatusCode.OK);
    }

    public static ApiResult ToItemResult<T>(this T? @this, HttpStatusCode statusCode)
    {
        if (@this == null)
            return ApiResult.NotFound();
        return new ApiResult(statusCode, @this.ToItemMsg());
    }

    public static ApiResult ToItemsResult<T>(this IEnumerable<T>? @this)
    {
        return @this.ToItemsResult(HttpStatusCode.OK);
    }

    public static ApiResult ToItemsResult<T>(this IEnumerable<T>? @this, HttpStatusCode statusCode)
    {
        if (@this == null)
            return ApiResult.NotFound();
        return new ApiResult(statusCode, @this.ToItemsMsg());
    }

    public static GeneralItemMessages<T>.Item ToItemMsg<T>(this T @this)
    {
        return new GeneralItemMessages<T>.Item(@this);
    }

    public static GeneralItemMessages<T>.Items ToItemsMsg<T>(this IEnumerable<T> @this)
    {
        return new GeneralItemMessages<T>.Items([.. @this]);
    }

    public static GeneralItemMessages<T>.Items ToItemsMsg<T>(this T[] @this)
    {
        return new GeneralItemMessages<T>.Items([.. @this]);
    }
}
