using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Threading.Tasks;
// using Microsoft.Extensions.DependencyInjection;  // For AddHttpClient(), AddHttpClient<T>
using System.Net.Http;
using Auth.Contracts.DTOs.Users; // For RoleVM  

namespace Auth.RCL.Services
{
    public class RoleAPIConsumer
    {
        private readonly HttpClient client;

        // public RealEstateFilter RealEstateFilter { get; set; } = new();

        public RoleAPIConsumer(IHttpClientFactory httpClientFactory)
        {
            this.client = httpClientFactory.CreateClient("Auth");

        }

        public async Task<bool> RemoveUserFromRole(string userId, string roleName)
        {
            try
            {
                // query = query + $"Language={appState.Language}";

                // var response = await client.GetFromJsonAsync<List<Product>>($"{query}");
                var response = await client.DeleteAsync($"role-management/{roleName}/user/{userId}");
                return response.IsSuccessStatusCode;
            }
            catch (HttpRequestException ex)
            {
                // _logger.LogError(ex, "GET Role failed for endpoint: {Endpoint}", endpoint);
                throw;
            }
        }

        public async Task<bool> AddUserToRole(string userId, string roleName)
        {
            try
            {
                var response = await client.PostAsync(
                    $"role-management/{roleName}/user/{userId}",
                    null
                );
                return response.IsSuccessStatusCode;
                // return await response.Content.ReadFromJsonAsync<RequestVM>();
            }
            catch (HttpRequestException ex)
            {
                // _logger.LogError(ex, "GET Role failed for endpoint: {Endpoint}", endpoint);
                throw;
            }
        }

        public async Task<List<RoleVM>> GetRoles()
        {
            try
            {
                return await client.GetFromJsonAsync<List<RoleVM>>("role-management/roles");
            }
            catch (HttpRequestException)
            {
                throw;
            }
        }

        public async Task<bool> CreateRole(RoleVM role)
        {
            try
            {
                var response = await client.PostAsJsonAsync("role-management/roles", role);
                return response.IsSuccessStatusCode;
            }
            catch (HttpRequestException)
            {
                throw;
            }
        }

        public async Task<bool> DeleteRole(string roleId)
        {
            try
            {
                var response = await client.DeleteAsync($"role-management/roles/{roleId}");
                return response.IsSuccessStatusCode;
            }
            catch (HttpRequestException)
            {
                throw;
            }
        }

        public async Task<List<string>> GetRolePermissions(string roleId)
        {
            try
            {
                return await client.GetFromJsonAsync<List<string>>($"role-management/roles/{roleId}/permissions");
            }
            catch (HttpRequestException)
            {
                throw;
            }
        }

        public async Task<bool> AddPermissionToRole(string roleId, string permission)
        {
            try
            {
                var response = await client.PostAsJsonAsync($"role-management/roles/{roleId}/permissions", permission);
                return response.IsSuccessStatusCode;
            }
            catch (HttpRequestException)
            {
                throw;
            }
        }

        public async Task<bool> RemovePermissionFromRole(string roleId, string permission)
        {
            try
            {
                // Send as JSON body for DELETE request
                var request = new HttpRequestMessage(HttpMethod.Delete, $"role-management/roles/{roleId}/permissions")
                {
                    Content = JsonContent.Create(permission)
                };
                var response = await client.SendAsync(request);
                return response.IsSuccessStatusCode;
            }
            catch (HttpRequestException)
            {
                throw;
            }
        }
    }
}
