namespace MyOffice.Web.Infrastructure.Filters;

using System.Collections;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using MyOffice.Web.Models;

public class ResponseFilter : IActionFilter
{
	private readonly ILogger<ResponseFilter> _logger;

	public ResponseFilter(ILogger<ResponseFilter> logger)
	{
		_logger = logger;
	}

	public void OnActionExecuting(ActionExecutingContext context)
	{
	}

	public void OnActionExecuted(ActionExecutedContext context)
	{
		// TODO: restore
		/*var route = $"{context.Controller.GetType().Name}.{context.ActionDescriptor.DisplayName}";
		if (context.Result == null)
			throw new NotSupportedException($"[{route}] Response required");
		if (!(context.Result is ObjectResult result))
			throw new NotSupportedException($"[{route}] Response must be an ObjectResult - {context.Result?.GetType().Name}");
		if (result.Value == null)
			throw new NotSupportedException($"[{route}] Response must be an ObjectResult with value");

		if (result.Value is IResponseModel)
		{
			return;
		}

		var type = result.Value.GetType();
		if (type.IsGenericType 
			&& result.Value is IEnumerable
			&& type.GenericTypeArguments.Any(x => x.GetInterfaces().Any(y => y == typeof(IResponseModel))))
		{
			return;
		}
			
		throw new NotSupportedException($"[{route}] Response must be an ObjectResult with an IResponseModel - {result.Value?.GetType().Name}");*/
	}
}