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

namespace Indotalent.Features.Inventory.Scrapping;

public class ScrappingService : BaseService
{
    private readonly RestClient _client;

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

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

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

    public async Task<ApiResponse<CreateScrappingResponse>?> CreateScrappingAsync(CreateScrappingRequest data)
    {
        var request = new RestRequest("api/scrapping", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateScrappingResponse>(_client, request);
    }

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

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

    public async Task<ApiResponse<bool>?> CreateScrappingItemAsync(CreateScrappingItemRequest data)
    {
        var request = new RestRequest("api/scrapping/inventory-transaction", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<bool>(_client, request);
    }

    public async Task<ApiResponse<bool>?> UpdateScrappingItemAsync(UpdateScrappingItemRequest data)
    {
        var request = new RestRequest("api/scrapping/inventory-transaction/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<bool>(_client, request);
    }

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