using System.Security.Claims;
using BMA.EHR.Domain.Models.Placement;
using BMA.EHR.Recurit.Exam.Service.Extensions;
using BMA.EHR.Recurit.Exam.Service.Core;
using BMA.EHR.Recurit.Exam.Service.Data;
using BMA.EHR.Recurit.Exam.Service.Models;
using BMA.EHR.Recurit.Exam.Service.Request;
using BMA.EHR.Recurit.Exam.Service.Response;
using BMA.EHR.Recurit.Exam.Service.Responses.Document;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Configuration;
using System.Net.Http.Headers;

namespace BMA.EHR.Recurit.Exam.Service.Services
{
    public class PermissionRepository
    {
        #region " Fields "

        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IConfiguration _configuration;

        #endregion

        #region " Constructor and Destructor "

        public PermissionRepository(IHttpContextAccessor httpContextAccessor,
                                 IConfiguration configuration)
        {
            _httpContextAccessor = httpContextAccessor;
            _configuration = configuration;
        }

        #endregion

        #region " Properties "

        protected string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];

        #endregion

        #region " Methods "

        public async Task<dynamic> GetPermissionAPIAsync(string action, string system)
        {
            try
            {
                var apiPath = $"{_configuration["API"]}/org/permission/dotnet/{action}/{system}";

                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken.Replace("Bearer ", ""));
                    client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]);
                    var req = await client.GetAsync(apiPath);
                    var res = await req.Content.ReadAsStringAsync();
                    return res;
                }
            }
            catch
            {
                throw;
            }
        }

        #endregion

    }
}
