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

namespace Indotalent.Features.Thirdparty.Customer;

public class CustomerService : BaseService
{
    private readonly RestClient _client;

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

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

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

    public async Task<ApiResponse<CreateCustomerResponse>?> CreateCustomerAsync(CreateCustomerRequest data)
    {
        var request = new RestRequest("api/customer", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateCustomerResponse>(_client, request);
    }

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

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

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

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

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