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

namespace Indotalent.Features.Purchase.PurchaseRequisition;

public class PurchaseRequisitionService : BaseService
{
    private readonly RestClient _client;

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

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

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

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

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

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

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

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

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