using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Extensions {
  public static class HttpRequestExtensions {


    /// <summary>
    /// 
    /// </summary>
    /// <param name="request"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public static List<string> GetHeader(this HttpRequest request, string name) {
      var headers = request.Headers[name].ToList();
      return headers != null ? headers : new List<string>();
    }



    /// <summary>
    /// Return clean JWT Bearer token from Authorisation Header
    /// </summary>
    public static string? GeBearerToken(this HttpRequest request) {
      var header = request.GetHeader("Authorization").FirstOrDefault();
      return header !=null
        ? header.Replace("Bearer ", "")
        : default;
    }

    /// <summary>
    /// Returns JWT Bearer token Vault path from custom AuthorizationPath Header
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public static string? GetBearerPath(this HttpRequest request) {
      var header = request.GetHeader("AuthorizationPath").FirstOrDefault();
      return header;
    }
  }
}
