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

namespace Indotalent.Features.Pipeline.LeadActivity;

public class LeadActivityService : BaseService
{
    private readonly RestClient _client;

    public LeadActivityService(
            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<LeadActivityLookupResponse>?> GetLeadActivityLookupAsync()
    {
        var request = new RestRequest("api/lead-activity/lookup", Method.Get);
        return await ExecuteWithResponseAsync<LeadActivityLookupResponse>(_client, request);
    }

    public async Task<ApiResponse<List<GetLeadActivityListResponse>>?> GetLeadActivityListAsync()
    {
        var request = new RestRequest("api/lead-activity", Method.Get);
        return await ExecuteWithResponseAsync<List<GetLeadActivityListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<CreateLeadActivityResponse>?> CreateLeadActivityAsync(CreateLeadActivityRequest data)
    {
        var request = new RestRequest("api/lead-activity", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateLeadActivityResponse>(_client, request);
    }

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

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