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

namespace Indotalent.Features.Inventory.TransferOut;

public class TransferOutService : BaseService
{
    private readonly RestClient _client;

    public TransferOutService(
            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<GetTransferOutListResponse>>?> GetTransferOutListAsync()
    {
        var request = new RestRequest("api/transfer-out", Method.Get);
        return await ExecuteWithResponseAsync<List<GetTransferOutListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<TransferOutLookupResponse>?> GetTransferOutLookupAsync()
    {
        var request = new RestRequest("api/transfer-out/lookup", Method.Get);
        return await ExecuteWithResponseAsync<TransferOutLookupResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateTransferOutResponse>?> CreateTransferOutAsync(CreateTransferOutRequest data)
    {
        var request = new RestRequest("api/transfer-out", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateTransferOutResponse>(_client, request);
    }

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

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

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

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

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