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

namespace Indotalent.Features.Inventory.StockCount;

public class StockCountService : BaseService
{
    private readonly RestClient _client;

    public StockCountService(
            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<GetStockCountListResponse>>?> GetStockCountListAsync()
    {
        var request = new RestRequest("api/stock-count", Method.Get);
        return await ExecuteWithResponseAsync<List<GetStockCountListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<StockCountLookupResponse>?> GetStockCountLookupAsync()
    {
        var request = new RestRequest("api/stock-count/lookup", Method.Get);
        return await ExecuteWithResponseAsync<StockCountLookupResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateStockCountResponse>?> CreateStockCountAsync(CreateStockCountRequest data)
    {
        var request = new RestRequest("api/stock-count", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateStockCountResponse>(_client, request);
    }

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

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

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

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

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