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

namespace Indotalent.Features.Pipeline.Lead;

public class LeadService : BaseService
{
    private readonly RestClient _client;

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

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

    public async Task<ApiResponse<CreateLeadResponse>?> CreateLeadAsync(CreateLeadRequest data)
    {
        var request = new RestRequest("api/lead", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateLeadResponse>(_client, request);
    }

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

    public async Task<ApiResponse<UpdateLeadResponse>?> UpdateLeadAsync(UpdateLeadRequest data)
    {
        var request = new RestRequest("api/lead/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateLeadResponse>(_client, request);
    }

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