using AutoMapper;
using DevExpress.XtraPrinting.Native;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using WorkFlowCheck.MAUI.DataLayer;
using WorkFlowCheck.MAUI.Services.Interfaces;

namespace WorkFlowCheck.MAUI.Services
{

    public class BaseService
    {
        public readonly HttpClient _httpClient;
        public readonly AppDbContext _dbContext;
        public readonly IConfiguration _configuration;
        public readonly IMapper _mapper;

        public BaseService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext, IMapper mapper)
        {
            _httpClient = httpClient;
            _dbContext = dbContext;
            _configuration = configuration;
            _mapper = mapper;

            var apiBaseUrl = configuration["ApiBaseUrl"]; // API-cím elérése a konfigurációból
            _httpClient.BaseAddress = new Uri(apiBaseUrl);

            var apiKey = configuration["ApiKey"];
            _httpClient.DefaultRequestHeaders.Add("X-Api-Key", apiKey);
        }
        protected async Task PrepareAuthenticatedRequestAsync()
        {
            var jwtToken = await SecureStorage.GetAsync("AuthToken");

            if (!string.IsNullOrEmpty(jwtToken) && _httpClient != null)
            {
                _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
            }
        }
    }
}
