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

namespace Indotalent.Features.Inventory.Product;

public class ProductService : BaseService
{
    private readonly RestClient _client;

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

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

    public async Task<ApiResponse<LookupProductResponse>?> GetProductLookupAsync()
    {
        var request = new RestRequest("api/product/lookup", Method.Get);
        return await ExecuteWithResponseAsync<LookupProductResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateProductResponse>?> CreateProductAsync(CreateProductRequest data)
    {
        var request = new RestRequest("api/product", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateProductResponse>(_client, request);
    }

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

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

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