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

namespace Indotalent.Features.Inventory.ProductGroup;

public class ProductGroupService : BaseService
{
    private readonly RestClient _client;

    public ProductGroupService(
            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<GetProductGroupListResponse>>?> GetProductGroupListAsync()
    {
        var request = new RestRequest("api/product-group", Method.Get);
        return await ExecuteWithResponseAsync<List<GetProductGroupListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<CreateProductGroupResponse>?> CreateProductGroupAsync(CreateProductGroupRequest data)
    {
        var request = new RestRequest("api/product-group", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateProductGroupResponse>(_client, request);
    }

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

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

    public async Task<ApiResponse<UpdateProductGroupResponse>?> UpdateProductGroupAsync(UpdateProductGroupRequest data)
    {
        var request = new RestRequest("api/product-group/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateProductGroupResponse>(_client, request);
    }
}