using AS1024.CommunityDocumentationPage.Interfaces;
using AS1024.CommunityDocumentationPage.Models;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;

namespace AS1024.CommunityDocumentationPage.DIScopes
{
    public class NetboxBgpCommunityDocumentation : IBgpCommunityDocumentation
    {
        private readonly IConfiguration configuration;

        public string DcimName => "netbox";
        public NetboxBgpCommunityDocumentation(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        public async Task<RouteTargets> GetBgpCommunities()
        {
            using HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", configuration["APIKey"]);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage result = await client.GetAsync(BuildNetBoxURI());
            string stringResult = await result.Content.ReadAsStringAsync();
#pragma warning disable CS8603 // Possible null reference return.
            return JsonConvert.DeserializeObject<RouteTargets>(stringResult);
#pragma warning restore CS8603 // Possible null reference return.
        }

        protected Uri BuildNetBoxURI()
        {
            UriBuilder endUrl = new UriBuilder
            {
                Path = "/api/ipam/route-targets/",
                Host = configuration["NetBoxHost"],
                Scheme = "https"
            };

            return endUrl.Uri;
        }
    }
}
