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

namespace Indotalent.Features.Purchase.GoodsReceive;

public class GoodsReceiveService : BaseService
{
    private readonly RestClient _client;

    public GoodsReceiveService(
            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<GetGoodsReceiveListResponse>>?> GetGoodsReceiveListAsync()
    {
        var request = new RestRequest("api/goods-receive", Method.Get);
        return await ExecuteWithResponseAsync<List<GetGoodsReceiveListResponse>>(_client, request);
    }

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

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

    public async Task<ApiResponse<CreateGoodsReceiveResponse>?> CreateGoodsReceiveAsync(CreateGoodsReceiveRequest data)
    {
        var request = new RestRequest("api/goods-receive", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateGoodsReceiveResponse>(_client, request);
    }

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

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

    public async Task<ApiResponse<bool>?> CreateGoodsReceiveItemAsync(CreateGoodsReceiveItemRequest data)
    {
        var request = new RestRequest("api/goods-receive/inventory-transaction", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<bool>(_client, request);
    }

    public async Task<ApiResponse<bool>?> UpdateGoodsReceiveItemAsync(UpdateGoodsReceiveItemRequest data)
    {
        var request = new RestRequest("api/goods-receive/inventory-transaction/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<bool>(_client, request);
    }

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