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

namespace Indotalent.Features.Utilities.ProgramResource;

public class ProgramResourceService : BaseService
{
    private readonly RestClient _client;

    public ProgramResourceService(
            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<GetProgramResourceListResponse>>?> GetProgramResourceListAsync()
    {
        var request = new RestRequest("api/program-resource", Method.Get);
        return await ExecuteWithResponseAsync<List<GetProgramResourceListResponse>>(_client, request);
    }

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

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

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

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