using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.OpenApi.Models;
using NSwag.Annotations;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Reflection;

namespace BMA.EHR.Report.Service
{
    public class OpenApiIgnoreFilter: IDocumentFilter
    {
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            foreach (var apiDescription in context.ApiDescriptions)
            {
                var actionDescriptor = apiDescription.ActionDescriptor as ControllerActionDescriptor;
                if (actionDescriptor != null)
                {
                    var ignoreAttribute = actionDescriptor.MethodInfo.GetCustomAttribute<OpenApiIgnoreAttribute>();
                    if (ignoreAttribute != null)
                    {
                        var routeKey = "/" + apiDescription.RelativePath.TrimEnd('/');
                        if (swaggerDoc.Paths.ContainsKey(routeKey))
                        {
                            swaggerDoc.Paths.Remove(routeKey);
                        }
                    }
                }
            }
        }
    }
}
