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

namespace Indotalent.Features.Utilities.BookingManager;

public class BookingManagerService : BaseService
{
    private readonly RestClient _client;

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

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

    public async Task<ApiResponse<LookupBookingDataResponse>?> GetLookupDataAsync()
    {
        var request = new RestRequest("api/booking-manager/lookup-data", Method.Get);
        return await ExecuteWithResponseAsync<LookupBookingDataResponse>(_client, request);
    }

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

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

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