using Indotalent.ConfigBackEnd.Interfaces;
using Indotalent.ConfigFrontEnd.Common;
using Indotalent.Features.Multitenant.Tenant.Cqrs;
using Indotalent.Features.Multitenant.TenantUser.Cqrs;
using Indotalent.Infrastructure.Authentication.Identity;
using Indotalent.Shared.Models;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using RestSharp;

namespace Indotalent.Features.Multitenant.Tenant;

public class TenantService : BaseService
{
    private readonly RestClient _client;

    public TenantService(
            IHttpClientFactory clientFactory,
            NavigationManager nav,
            ISnackbar snackbar,
            ICurrentUserService currentUserService,
            TokenProvider tokenProvider)
            : base(clientFactory, nav, snackbar, currentUserService, tokenProvider)
    {
        _client = new RestClient(nav.BaseUri);
    }

    public async Task<ApiResponse<List<GetTenantListResponse>>?> GetTenantListAsync()
    {
        var request = new RestRequest("api/tenant", Method.Get);
        return await ExecuteWithResponseAsync<List<GetTenantListResponse>>(_client, request);
    }

    public async Task<ApiResponse<GetTenantByIdResponse>?> GetTenantByIdAsync(string id)
    {
        var request = new RestRequest($"api/tenant/{id}", Method.Get);
        return await ExecuteWithResponseAsync<GetTenantByIdResponse>(_client, request);
    }

    public async Task<ApiResponse<LookupTenantResponse>?> GetTenantLookupAsync()
    {
        var request = new RestRequest("api/tenant/lookup", Method.Get);
        return await ExecuteWithResponseAsync<LookupTenantResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateTenantResponse>?> CreateTenantAsync(CreateTenantRequest data)
    {
        var request = new RestRequest("api/tenant", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateTenantResponse>(_client, request);
    }

    public async Task<bool> DeleteTenantByIdAsync(string id)
    {
        var request = new RestRequest($"api/tenant/delete/{id}", Method.Post);
        var response = await ExecuteWithResponseAsync<object>(_client, request);
        return response?.IsSuccess ?? false;
    }

    public async Task<ApiResponse<UpdateTenantResponse>?> UpdateTenantAsync(UpdateTenantRequest data)
    {
        var request = new RestRequest("api/tenant/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateTenantResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateTenantUserResponse>?> CreateTenantUserAsync(CreateTenantUserRequest data)
    {
        var request = new RestRequest("api/tenant/tenant-user", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateTenantUserResponse>(_client, request);
    }

    public async Task<ApiResponse<UpdateTenantUserResponse>?> UpdateTenantUserAsync(UpdateTenantUserRequest data)
    {
        var request = new RestRequest("api/tenant/tenant-user/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateTenantUserResponse>(_client, request);
    }

    public async Task<bool> DeleteTenantUserAsync(string id)
    {
        var request = new RestRequest($"api/tenant/tenant-user/delete/{id}", Method.Post);
        var response = await ExecuteWithResponseAsync<object>(_client, request);
        return response?.IsSuccess ?? false;
    }
}