using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
using Exsersewo.Common.Converters;
using Exsersewo.Common.Utilities;
using Microsoft.AspNetCore.Mvc;

namespace Kynareth.Helpers;

public static class ResponseHelper
{
	private static readonly JsonSerializerOptions SerializerOptions = new()
	{
		NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
		DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
		WriteIndented = true,
		Converters = { new LongToStringConverter() }
	};
	
	public static object Send(this HttpContext Context, EventResult result, HttpStatusCode status = HttpStatusCode.OK)
	{
		Context.Response.ContentType = "application/json";
		Context.Response.StatusCode = (int)status;

		return new JsonResult(result, SerializerOptions);
	}

	public static object SendStream<T>(this HttpContext Context, T stream, string contentType, HttpStatusCode status = HttpStatusCode.OK) where T : Stream
		=> new StreamResult(stream)
		{
			ContentType = contentType,
			StatusCode = (int)status
		};
}