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

namespace Indotalent.Features.Pipeline.Budget;

public class BudgetService : BaseService
{
    private readonly RestClient _client;

    public BudgetService(
            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<GetBudgetListResponse>>?> GetBudgetListAsync()
    {
        var request = new RestRequest("api/budget", Method.Get);
        return await ExecuteWithResponseAsync<List<GetBudgetListResponse>>(_client, request);
    }

    public async Task<ApiResponse<GetBudgetByIdResponse>?> GetBudgetByIdAsync(string id)
    {
        var request = new RestRequest($"api/budget/{id}", Method.Get);
        return await ExecuteWithResponseAsync<GetBudgetByIdResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateBudgetResponse>?> CreateBudgetAsync(CreateBudgetRequest data)
    {
        var request = new RestRequest("api/budget", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateBudgetResponse>(_client, request);
    }

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

    public async Task<ApiResponse<UpdateBudgetResponse>?> UpdateBudgetAsync(UpdateBudgetRequest data)
    {
        var request = new RestRequest("api/budget/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateBudgetResponse>(_client, request);
    }

    public async Task<ApiResponse<BudgetLookupResponse>?> GetBudgetLookupAsync()
    {
        var request = new RestRequest("api/budget/lookup", Method.Get);
        return await ExecuteWithResponseAsync<BudgetLookupResponse>(_client, request);
    }
}