using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace AipGateway.API.Application.Pipeline.Middlewares.Behaviours
{
    public class UnhandledExceptionBehaviour
    {
        private readonly ILogger<HttpRequest> _log;
        private readonly RequestDelegate _next;

        public UnhandledExceptionBehaviour(ILogger<HttpRequest> logger, RequestDelegate next)
        {
            _log = logger;
            _next = next;
            _log.LogError("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: UnhandledExceptionBehaviour");
        }

        public async Task InvokeAsync(HttpContext context)
        {
            try
            {
                //_log.LogError("UnhandledExceptionBehaviour Invoke Before:");
                await _next.Invoke(context);
                //_log.LogError("UnhandledExceptionBehaviour Invoke  After:");
                _log.LogError("UnhandledExceptionBehaviour.........................................................................");
            }
            catch (Exception ex)
            {
                var requestName = context.Request.Path;
                _log.LogError(ex, "API Request: Unhandled Exception for Request {Name}", requestName);

                throw;
            }
        }
    }

}

