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

namespace Indotalent.Features.Purchase.PaymentDisburse;

public class PaymentDisburseService : BaseService
{
    private readonly RestClient _client;

    public PaymentDisburseService(
            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<GetPaymentDisburseListResponse>>?> GetPaymentDisburseListAsync()
    {
        var request = new RestRequest("api/payment-disburse", Method.Get);
        return await ExecuteWithResponseAsync<List<GetPaymentDisburseListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<GetPaymentDisburseByIdResponse>?> GetBillDetailForPaymentAsync(string billId)
    {
        var request = new RestRequest($"api/payment-disburse/bill-detail/{billId}", Method.Get);
        return await ExecuteWithResponseAsync<GetPaymentDisburseByIdResponse>(_client, request);
    }

    public async Task<ApiResponse<PaymentDisburseLookupResponse>?> GetPaymentDisburseLookupAsync()
    {
        var request = new RestRequest("api/payment-disburse/lookup", Method.Get);
        return await ExecuteWithResponseAsync<PaymentDisburseLookupResponse>(_client, request);
    }

    public async Task<ApiResponse<CreatePaymentDisburseResponse>?> CreatePaymentDisburseAsync(CreatePaymentDisburseRequest data)
    {
        var request = new RestRequest("api/payment-disburse", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreatePaymentDisburseResponse>(_client, request);
    }

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

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