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

namespace Indotalent.Features.Inventory.Warehouse;

public class WarehouseService : BaseService
{
    private readonly RestClient _client;

    public WarehouseService(
            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<GetWarehouseListResponse>>?> GetWarehouseListAsync()
    {
        var request = new RestRequest("api/warehouse", Method.Get);
        return await ExecuteWithResponseAsync<List<GetWarehouseListResponse>>(_client, request);
    }

    public async Task<ApiResponse<GetWarehouseByIdResponse>?> GetWarehouseByIdAsync(string id)
    {
        var request = new RestRequest($"api/warehouse/{id}", Method.Get);
        return await ExecuteWithResponseAsync<GetWarehouseByIdResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateWarehouseResponse>?> CreateWarehouseAsync(CreateWarehouseRequest data)
    {
        var request = new RestRequest("api/warehouse", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateWarehouseResponse>(_client, request);
    }

    public async Task<bool> DeleteWarehouseByIdAsync(string id)
    {
        var request = new RestRequest($"api/warehouse/delete/{id}", Method.Post);

        var response = await ExecuteWithResponseAsync<object>(_client, request);
        return response?.IsSuccess ?? false;
    }

    public async Task<ApiResponse<UpdateWarehouseResponse>?> UpdateWarehouseAsync(UpdateWarehouseRequest data)
    {
        var request = new RestRequest("api/warehouse/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateWarehouseResponse>(_client, request);
    }
}