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

namespace Indotalent.Features.Pipeline.Expense;

public class ExpenseService : BaseService
{
    private readonly RestClient _client;

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

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

    public async Task<ApiResponse<CreateExpenseResponse>?> CreateExpenseAsync(CreateExpenseRequest data)
    {
        var request = new RestRequest("api/expense", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateExpenseResponse>(_client, request);
    }

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

    public async Task<ApiResponse<UpdateExpenseResponse>?> UpdateExpenseAsync(UpdateExpenseRequest data)
    {
        var request = new RestRequest("api/expense/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateExpenseResponse>(_client, request);
    }

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