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

namespace Indotalent.Features.Inventory.PositiveAdjustment;

public class PositiveAdjustmentService : BaseService
{
    private readonly RestClient _client;

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

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

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

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

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

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

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

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

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