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

namespace Indotalent.Features.Purchase.PurchaseOrder;

public class PurchaseOrderService : BaseService
{
    private readonly RestClient _client;

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

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

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

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

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

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

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

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

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