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

namespace Indotalent.Features.Pipeline.LeadContact;

public class LeadContactService : BaseService
{
    private readonly RestClient _client;

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

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

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

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

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

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

    public async Task<ApiResponse<ChangeLeadContactAvatarResponse>?> ChangeAvatarAsync(ChangeLeadContactAvatarRequest data)
    {
        var request = new RestRequest("api/lead-contact/change-avatar", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<ChangeLeadContactAvatarResponse>(_client, request);
    }

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