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

namespace Indotalent.Features.Utilities.ProgramManager;

public class ProgramManagerService : BaseService
{
    private readonly RestClient _client;

    public ProgramManagerService(
            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<ProgramManagerLookupResponse>?> GetLookupDataAsync()
    {
        var request = new RestRequest("api/program-manager/lookup", Method.Get);
        return await ExecuteWithResponseAsync<ProgramManagerLookupResponse>(_client, request);
    }

    public async Task<ApiResponse<List<GetProgramManagerListResponse>>?> GetProgramManagerListAsync()
    {
        var request = new RestRequest("api/program-manager", Method.Get);
        return await ExecuteWithResponseAsync<List<GetProgramManagerListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<CreateProgramManagerResponse>?> CreateProgramManagerAsync(CreateProgramManagerRequest data)
    {
        var request = new RestRequest("api/program-manager", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateProgramManagerResponse>(_client, request);
    }

    public async Task<ApiResponse<UpdateProgramManagerResponse>?> UpdateProgramManagerAsync(UpdateProgramManagerRequest data)
    {
        var request = new RestRequest("api/program-manager/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateProgramManagerResponse>(_client, request);
    }

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