using System.Net.Http.Headers;
using OpenWarehouse.auth.Common.ApiInterfeiceLib;
using OpenWarehouse.auth.lib;
using OpenWarehouse.auth.lib.Model.Role;
using OpenWarehouse.auth.lib.Model.Service;
using OpenWarehouse.warehouse.api.Common.CustimIdentifyManager;
using OpenWarehouse.warehouse.api.Common.ServiceClaims;

namespace OpenWarehouse.warehouse.api.Common.DefaultServiceAccountAndRole;

public class DefaultIdentify(ILogger<DefaultIdentify> logger, IConfiguration configuration,
    IServiceClaimsManager serviceClaimsManager)
{
    
    private List<RoleModel> _listOfRole = new()
    {
        new RoleModel()
        {
            RoleName = ServicePrivilage.ServiceAdmin.GetName(),
            Privilage = new()
            {
                ServicePrivilage.ServiceAdmin.GetName(),
                ServicePrivilage.ServiceUser.GetName(),
            }
        },
        new RoleModel()
        {
            RoleName = ServicePrivilage.ServiceUser.GetName(),
            Privilage = new()
            {
                ServicePrivilage.ServiceUser.GetName(),
            }
        }
    };
    
    
    
    public async Task<bool> TestAuth(string token)
    {
        var handler = new AuthTokenHandler(token);
        var httpClient = new HttpClient(handler);
        var client = new OpenWarehouseAuthClient(configuration["Auth:Url"], httpClient);

        try
        {
            return await client.TestServiceTokenAsync();
        }
        catch (ApiException e)
        {
            if (e.StatusCode == 401) return false;

            throw;
        }
        
    }

    public async Task<CreateServiceTokenReturnModel> SeedAsync(string adminToken)
    {
        var httpClient = new HttpClient();
        var token = adminToken.Split(" ");
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token[0], token[1]);
        var client = new OpenWarehouseAuthClient(configuration["Auth:Url"], httpClient);

        try
        {
            var service = new CreateServiceTokenModel()
            {
                ServiceName = "Warehouse",
                ServicePrivilages = ServicePrivilageClass.GetServicePrivilageNameList(),
                ServiceRoles = _listOfRole
            };
            var result = await client.GetTokenForServiceAsync(service);
            
            logger.LogInformation($"Auth service initialized: \n " +
                                  $"Is success: {result.IsSuccess} \n " +
                                  $"Service username: {result.ServiceUsername} \n " +
                                  $"Service allerady exist: {result.UserAllereadyExist}");
            
            return result;
        }
        catch (ApiException e)
        {
            logger.LogWarning($"ApiException with code {e.StatusCode} and nessage {e.Message}");
            throw;
        }
        catch (Exception e)
        {
           
            logger.LogWarning(e.Message);
            throw;
        }

        throw new Exception("This exemption shoudnt happen");
    }
    

}