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

namespace Indotalent.Features.Sales.DeliveryOrder;

public class DeliveryOrderService : BaseService
{
    private readonly RestClient _client;

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

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

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

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

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

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

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

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

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