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

namespace Indotalent.Features.Pipeline.Campaign;

public class CampaignService : BaseService
{
    private readonly RestClient _client;

    public CampaignService(
            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<CampaignLookupResponse>?> GetLookupDataAsync()
    {
        var request = new RestRequest("api/campaign/lookup", Method.Get);
        return await ExecuteWithResponseAsync<CampaignLookupResponse>(_client, request);
    }

    public async Task<ApiResponse<List<GetCampaignListResponse>>?> GetCampaignListAsync()
    {
        var request = new RestRequest("api/campaign", Method.Get);
        return await ExecuteWithResponseAsync<List<GetCampaignListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<CreateCampaignResponse>?> CreateCampaignAsync(CreateCampaignRequest data)
    {
        var request = new RestRequest("api/campaign", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateCampaignResponse>(_client, request);
    }

    public async Task<ApiResponse<UpdateCampaignResponse>?> UpdateCampaignAsync(UpdateCampaignRequest data)
    {
        var request = new RestRequest("api/campaign/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<UpdateCampaignResponse>(_client, request);
    }

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

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

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

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

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

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

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