using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Server.Middleware
{
    /// 
    /// Redirect requests to robots.txt to web/robots.txt.
    /// 
    public class RobotsRedirectionMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly ILogger _logger;
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The next delegate in the pipeline.
        /// The logger.
        public RobotsRedirectionMiddleware(
            RequestDelegate next,
            ILogger logger)
        {
            _next = next;
            _logger = logger;
        }
        /// 
        /// Executes the middleware action.
        /// 
        /// The current HTTP context.
        /// The async task.
        public async Task Invoke(HttpContext httpContext)
        {
            var localPath = httpContext.Request.Path.ToString();
            if (string.Equals(localPath, "/robots.txt", StringComparison.OrdinalIgnoreCase))
            {
                _logger.LogDebug("Redirecting robots.txt request to web/robots.txt");
                httpContext.Response.Redirect("web/robots.txt");
                return;
            }
            await _next(httpContext).ConfigureAwait(false);
        }
    }
}