using Microsoft.AspNetCore.Mvc;

namespace Kit.Helpers
{
    public class JsonResultSuccess<T>
    {
        public T? SuccessMessage { get; set; }
    }

    public class JsonResultError
    {
        public string? ErrorMessage { get; set; }
        public string? ErrorStackTrace { get; set; }
        public string? RedirectUrl { get; set; }
    }

    public static class JsonResultExt
    {
        public static JsonResult JsonSuccess<TController>(this TController controller)
            where TController : Controller
            => controller.Json(new JsonResultSuccess<string> { SuccessMessage = null });

        public static JsonResult JsonSuccess<TController, TSuccessMessage>(this TController controller)
            where TController : Controller
            => controller.Json(new JsonResultSuccess<TSuccessMessage> { SuccessMessage = default });

        public static JsonResult JsonSuccess<TController, TSuccessMessage>(this TController controller, TSuccessMessage? successMessage)
            where TController : Controller
             => controller.Json(new JsonResultSuccess<TSuccessMessage> { SuccessMessage = successMessage });

        public static JsonResult JsonError<TController>(this TController controller, string errorMessage)
            where TController : Controller
             => controller.Json(new JsonResultError { ErrorMessage = errorMessage ?? string.Empty });

        public static JsonResult JsonError<TController>(this TController controller, string errorMessage, string errorStackTrace)
            where TController : Controller
             => controller.Json(new JsonResultError { ErrorMessage = errorMessage ?? string.Empty, ErrorStackTrace = errorStackTrace ?? string.Empty });

        public static JsonResult JsonError<TController>(this TController controller, string errorMessage, string errorStackTrace, string? redirectUrl)
            where TController : Controller
             => controller.Json(new JsonResultError { ErrorMessage = errorMessage ?? string.Empty, ErrorStackTrace = errorStackTrace ?? string.Empty, RedirectUrl = redirectUrl });
    }
}