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

namespace Indotalent.Features.Pipeline.SalesTeam;

public class SalesTeamService : BaseService
{
    private readonly RestClient _client;

    public SalesTeamService(
            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<GetSalesTeamListResponse>>?> GetSalesTeamListAsync()
    {
        var request = new RestRequest("api/sales-team", Method.Get);
        return await ExecuteWithResponseAsync<List<GetSalesTeamListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<CreateSalesTeamResponse>?> CreateSalesTeamAsync(CreateSalesTeamRequest data)
    {
        var request = new RestRequest("api/sales-team", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateSalesTeamResponse>(_client, request);
    }

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

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

    public async Task<ApiResponse<UpdateSalesTeamResponse>?> UpdateSalesTeamAsync(UpdateSalesTeamRequest data)
    {
        var request = new RestRequest("api/sales-team/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateSalesTeamResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateSalesTeamRepresentativeResponse>?> CreateSalesTeamRepresentativeAsync(CreateSalesTeamRepresentativeRequest data)
    {
        var request = new RestRequest("api/sales-team/representative", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateSalesTeamRepresentativeResponse>(_client, request);
    }

    public async Task<ApiResponse<UpdateSalesTeamRepresentativeResponse>?> UpdateSalesTeamRepresentativeAsync(UpdateSalesTeamRepresentativeRequest data)
    {
        var request = new RestRequest("api/sales-team/representative/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateSalesTeamRepresentativeResponse>(_client, request);
    }

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

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