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

namespace Indotalent.Features.Sales.PaymentReceive;

public class PaymentReceiveService : BaseService
{
    private readonly RestClient _client;

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

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

    public async Task<ApiResponse<GetPaymentReceiveByIdResponse>?> GetInvoiceDetailForPaymentAsync(string invoiceId)
    {
        var request = new RestRequest($"api/payment-receive/invoice-detail/{invoiceId}", Method.Get);
        return await ExecuteWithResponseAsync<GetPaymentReceiveByIdResponse>(_client, request);
    }

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

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

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

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