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

namespace Indotalent.Features.Utilities.BookingResource;

public class BookingResourceService : BaseService
{
    private readonly RestClient _client;

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

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

    public async Task<ApiResponse<LookupBookingGroupResponse>?> GetBookingGroupLookupAsync()
    {
        var request = new RestRequest("api/booking-resource/lookup-booking-group", Method.Get);
        return await ExecuteWithResponseAsync<LookupBookingGroupResponse>(_client, request);
    }

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

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

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