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

namespace Indotalent.Features.Setting.Currency;

public class CurrencyService : BaseService
{
    private readonly RestClient _client;

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

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

    public async Task<ApiResponse<CreateCurrencyResponse>?> CreateCurrencyAsync(CreateCurrencyRequest data)
    {
        var request = new RestRequest("api/currency", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateCurrencyResponse>(_client, request);
    }

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

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

    public async Task<ApiResponse<UpdateCurrencyResponse>?> UpdateCurrencyAsync(UpdateCurrencyRequest data)
    {
        var request = new RestRequest("api/currency/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateCurrencyResponse>(_client, request);
    }
}