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

namespace Indotalent.Features.Thirdparty.VendorGroup;

public class VendorGroupService : BaseService
{
    private readonly RestClient _client;

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

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

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

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

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

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