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

namespace Indotalent.Features.Inventory.TransferIn;

public class TransferInService : BaseService
{
    private readonly RestClient _client;

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

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

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

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

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

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

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

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

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