using Indotalent.ConfigBackEnd.Interfaces;
using Indotalent.ConfigFrontEnd.Common;
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.TenantUser;

public class TenantUserService : BaseService
{
    private readonly RestClient _client;

    public TenantUserService(
            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<GetTenantUserListResponse>>?> GetTenantUserListAsync()
    {
        var request = new RestRequest("api/tenant-user", Method.Get);
        return await ExecuteWithResponseAsync<List<GetTenantUserListResponse>>(_client, request);
    }

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

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

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

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

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