
using Newtonsoft.Json;
using Serilog;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.Web.Services.Interfaces;

namespace WorkFlowCheck.Web.Services
{
    public class UserService : BaseService, IUserService
    {
        public UserService(HttpClient httpClient, IConfiguration configuration, IHttpContextAccessor httpContextAccessor) : base(httpClient, configuration, httpContextAccessor)
        {
        }
        public async Task<ApiResponseDTO<UserDTO>> Authenticate(string username, string password)
        {
            try
            {
                // Az API végpont meghatározása
                string endpoint = $"{_httpClient.BaseAddress}api/User/Authenticate";

                var userSimpleDTO = new UserSimpleDTO()
                {
                    UserName = username,
                    Password = password,
                    
                };
                

                using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, userSimpleDTO))
                {
                    httpResponseMessage.EnsureSuccessStatusCode();

                    var jsonString = await httpResponseMessage.Content.ReadAsStringAsync();
                    var response = JsonConvert.DeserializeObject<ApiResponseDTO<UserDTO>>(jsonString);

                    return response ?? new ApiResponseDTO<UserDTO>
                    {
                        IsSuccess = false,
                    };
                }
            }
            catch (Exception ex)
            {
                // Hiba visszaadása
                return new ApiResponseDTO<UserDTO>
                {
                    IsSuccess = false
                };
            }
        }

        public Task<ApiRequestDTO<List<RoleDTO>>> GetAllRoles() => throw new NotImplementedException();
        public Task<ApiRequestDTO<List<UserDTO>>> GetAllUserRoles() => throw new NotImplementedException();
        public async Task<List<UserDTO>> GetAllUsers()
        {
            string endpoint = $"{_httpClient.BaseAddress}api/User/GetAllUsers";
            var retVal = new List<UserDTO>();
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<UserDTO>>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }

        public Task<RoleDTO> GetRole(int id) => throw new NotImplementedException();
        public async Task<UserDTO> GetUser(int id)
        {
            string endpoint = $"{_httpClient.BaseAddress}api/User/GetUser/{id}";
            var retVal = new UserDTO() { RoleDTO = new List<RoleDTO>() };
            try
            {
                var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<UserDTO>>(endpoint);
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        return response.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return retVal;
        }
        public Task<UserRoleDTO> GetUserRole(int id) => throw new NotImplementedException();
        public Task<RoleDTO> UpdateRole(RoleDTO role) => throw new NotImplementedException();
        public Task<UserDTO> UpdateUser(UserDTO user) => throw new NotImplementedException();
        public Task<UserRoleDTO> UpdateUserRole(UserRoleDTO userRole) => throw new NotImplementedException();
        Task<List<RoleDTO>> IUserService.GetAllRoles() => throw new NotImplementedException();
        Task<List<UserDTO>> IUserService.GetAllUserRoles() => throw new NotImplementedException();
    }
}
