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

namespace Indotalent.Features.Purchase.PurchaseReturn;

public class PurchaseReturnService : BaseService
{
    private readonly RestClient _client;

    public PurchaseReturnService(
            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<GetPurchaseReturnListResponse>>?> GetPurchaseReturnListAsync()
    {
        var request = new RestRequest("api/purchase-return", Method.Get);
        return await ExecuteWithResponseAsync<List<GetPurchaseReturnListResponse>>(_client, request);
    }

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

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

    public async Task<ApiResponse<CreatePurchaseReturnResponse>?> CreatePurchaseReturnAsync(CreatePurchaseReturnRequest data)
    {
        var request = new RestRequest("api/purchase-return", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreatePurchaseReturnResponse>(_client, request);
    }

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

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

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

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

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