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

namespace Indotalent.Features.Inventory.UnitMeasure;

public class UnitMeasureService : BaseService
{
    private readonly RestClient _client;

    public UnitMeasureService(
            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<GetUnitMeasureListResponse>>?> GetUnitMeasureListAsync()
    {
        var request = new RestRequest("api/unit-measure", Method.Get);
        return await ExecuteWithResponseAsync<List<GetUnitMeasureListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<CreateUnitMeasureResponse>?> CreateUnitMeasureAsync(CreateUnitMeasureRequest data)
    {
        var request = new RestRequest("api/unit-measure", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateUnitMeasureResponse>(_client, request);
    }

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

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

    public async Task<ApiResponse<UpdateUnitMeasureResponse>?> UpdateUnitMeasureAsync(UpdateUnitMeasureRequest data)
    {
        var request = new RestRequest("api/unit-measure/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateUnitMeasureResponse>(_client, request);
    }
}