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

namespace Indotalent.Features.Thirdparty.VendorContact;

public class VendorContactService : BaseService
{
    private readonly RestClient _client;

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

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

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

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

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

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

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