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

namespace Indotalent.Features.Thirdparty.CustomerContact;

public class CustomerContactService : BaseService
{
    private readonly RestClient _client;

    public CustomerContactService(
            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<GetCustomerContactListResponse>>?> GetCustomerContactListAsync()
    {
        var request = new RestRequest("api/customer-contact", Method.Get);
        return await ExecuteWithResponseAsync<List<GetCustomerContactListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<LookupCustomerContactResponse>?> GetCustomerContactLookupAsync()
    {
        var request = new RestRequest("api/customer-contact/lookup", Method.Get);
        return await ExecuteWithResponseAsync<LookupCustomerContactResponse>(_client, request);
    }

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

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

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

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