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

namespace Indotalent.Features.Thirdparty.VendorCategory;

public class VendorCategoryService : BaseService
{
    private readonly RestClient _client;

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

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

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

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

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

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