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

namespace Indotalent.Features.Thirdparty.Vendor;

public class VendorService : BaseService
{
    private readonly RestClient _client;

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

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

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

    public async Task<ApiResponse<CreateVendorResponse>?> CreateVendorAsync(CreateVendorRequest data)
    {
        var request = new RestRequest("api/vendor", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateVendorResponse>(_client, request);
    }

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

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

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

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

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