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

namespace Indotalent.Features.Sales.SalesOrder;

public class SalesOrderService : BaseService
{
    private readonly RestClient _client;

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

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

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

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

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

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

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

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

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