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

namespace Indotalent.Features.Thirdparty.CustomerGroup;

public class CustomerGroupService : BaseService
{
    private readonly RestClient _client;

    public CustomerGroupService(
            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<GetCustomerGroupListResponse>>?> GetCustomerGroupListAsync()
    {
        var request = new RestRequest("api/customer-group", Method.Get);
        return await ExecuteWithResponseAsync<List<GetCustomerGroupListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<CreateCustomerGroupResponse>?> CreateCustomerGroupAsync(CreateCustomerGroupRequest data)
    {
        var request = new RestRequest("api/customer-group", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateCustomerGroupResponse>(_client, request);
    }

    public async Task<bool> DeleteCustomerGroupByIdAsync(string id)
    {
        var request = new RestRequest($"api/customer-group/delete/{id}", Method.Post);

        var response = await ExecuteWithResponseAsync<object>(_client, request);
        return response?.IsSuccess ?? false;
    }

    public async Task<ApiResponse<UpdateCustomerGroupResponse>?> UpdateCustomerGroupAsync(UpdateCustomerGroupRequest data)
    {
        var request = new RestRequest("api/customer-group/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateCustomerGroupResponse>(_client, request);
    }
}