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

namespace Indotalent.Features.Inventory.NegativeAdjustment;

public class NegativeAdjustmentService : BaseService
{
    private readonly RestClient _client;

    public NegativeAdjustmentService(
            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<GetNegativeAdjustmentListResponse>>?> GetNegativeAdjustmentListAsync()
    {
        var request = new RestRequest("api/negative-adjustment", Method.Get);
        return await ExecuteWithResponseAsync<List<GetNegativeAdjustmentListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<NegativeAdjustmentLookupResponse>?> GetNegativeAdjustmentLookupAsync()
    {
        var request = new RestRequest("api/negative-adjustment/lookup", Method.Get);
        return await ExecuteWithResponseAsync<NegativeAdjustmentLookupResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateNegativeAdjustmentResponse>?> CreateNegativeAdjustmentAsync(CreateNegativeAdjustmentRequest data)
    {
        var request = new RestRequest("api/negative-adjustment", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateNegativeAdjustmentResponse>(_client, request);
    }

    public async Task<ApiResponse<bool>?> UpdateNegativeAdjustmentAsync(UpdateNegativeAdjustmentRequest data)
    {
        var request = new RestRequest("api/negative-adjustment/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<bool>(_client, request);
    }

    public async Task<bool> DeleteNegativeAdjustmentByIdAsync(string id)
    {
        var request = new RestRequest($"api/negative-adjustment/delete/{id}", Method.Post);
        var response = await ExecuteWithResponseAsync<object>(_client, request);
        return response?.IsSuccess ?? false;
    }

    public async Task<ApiResponse<bool>?> CreateNegativeAdjustmentItemAsync(CreateNegativeAdjustmentItemRequest data)
    {
        var request = new RestRequest("api/negative-adjustment/negative-adjustment-item", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<bool>(_client, request);
    }

    public async Task<ApiResponse<bool>?> UpdateNegativeAdjustmentItemAsync(UpdateNegativeAdjustmentItemRequest data)
    {
        var request = new RestRequest("api/negative-adjustment/negative-adjustment-item/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<bool>(_client, request);
    }

    public async Task<bool> DeleteNegativeAdjustmentItemAsync(string id)
    {
        var request = new RestRequest($"api/negative-adjustment/negative-adjustment-item/delete/{id}", Method.Post);
        var response = await ExecuteWithResponseAsync<object>(_client, request);
        return response?.IsSuccess ?? false;
    }
}