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

namespace Indotalent.Features.Pipeline.SalesRepresentative;

public class SalesRepresentativeService : BaseService
{
    private readonly RestClient _client;

    public SalesRepresentativeService(
            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<GetSalesRepresentativeListResponse>>?> GetSalesRepresentativeListAsync()
    {
        var request = new RestRequest("api/sales-representative", Method.Get);
        return await ExecuteWithResponseAsync<List<GetSalesRepresentativeListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<LookupSalesRepresentativeResponse>?> GetSalesRepresentativeLookupAsync()
    {
        var request = new RestRequest("api/sales-representative/lookup", Method.Get);
        return await ExecuteWithResponseAsync<LookupSalesRepresentativeResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateSalesRepresentativeResponse>?> CreateSalesRepresentativeAsync(CreateSalesRepresentativeRequest data)
    {
        var request = new RestRequest("api/sales-representative", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateSalesRepresentativeResponse>(_client, request);
    }

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

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

    public async Task<ApiResponse<UpdateSalesRepresentativeResponse>?> UpdateSalesRepresentativeAsync(UpdateSalesRepresentativeRequest data)
    {
        var request = new RestRequest("api/sales-representative/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateSalesRepresentativeResponse>(_client, request);
    }
}