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

namespace Indotalent.Features.Purchase.DebitNote;

public class DebitNoteService : BaseService
{
    private readonly RestClient _client;

    public DebitNoteService(
            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<GetDebitNoteListResponse>>?> GetDebitNoteListAsync()
    {
        var request = new RestRequest("api/debit-note", Method.Get);
        return await ExecuteWithResponseAsync<List<GetDebitNoteListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<GetDebitNoteByIdResponse>?> GetPurchaseReturnDetailForDebitNoteAsync(string prId)
    {
        var request = new RestRequest($"api/debit-note/purchase-return-detail/{prId}", Method.Get);
        return await ExecuteWithResponseAsync<GetDebitNoteByIdResponse>(_client, request);
    }

    public async Task<ApiResponse<DebitNoteLookupResponse>?> GetDebitNoteLookupAsync()
    {
        var request = new RestRequest("api/debit-note/lookup", Method.Get);
        return await ExecuteWithResponseAsync<DebitNoteLookupResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateDebitNoteResponse>?> CreateDebitNoteAsync(CreateDebitNoteRequest data)
    {
        var request = new RestRequest("api/debit-note", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateDebitNoteResponse>(_client, request);
    }

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

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