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

namespace Indotalent.Features.Setting.Tax;

public class TaxService : BaseService
{
    private readonly RestClient _client;

    public TaxService(
            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<GetTaxListResponse>>?> GetTaxListAsync()
    {
        var request = new RestRequest("api/tax", Method.Get);
        return await ExecuteWithResponseAsync<List<GetTaxListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<CreateTaxResponse>?> CreateTaxAsync(CreateTaxRequest data)
    {
        var request = new RestRequest("api/tax", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateTaxResponse>(_client, request);
    }

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

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

    public async Task<ApiResponse<UpdateTaxResponse>?> UpdateTaxAsync(UpdateTaxRequest data)
    {
        var request = new RestRequest("api/tax/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateTaxResponse>(_client, request);
    }
}