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

namespace Indotalent.Features.Sales.SalesQuotation;

public class SalesQuotationService : BaseService
{
    private readonly RestClient _client;

    public SalesQuotationService(
            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<GetSalesQuotationListResponse>>?> GetSalesQuotationListAsync()
    {
        var request = new RestRequest("api/sales-quotation", Method.Get);
        return await ExecuteWithResponseAsync<List<GetSalesQuotationListResponse>>(_client, request);
    }

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

    public async Task<ApiResponse<SalesQuotationLookupResponse>?> GetSalesQuotationLookupAsync()
    {
        var request = new RestRequest("api/sales-quotation/lookup", Method.Get);
        return await ExecuteWithResponseAsync<SalesQuotationLookupResponse>(_client, request);
    }

    public async Task<ApiResponse<CreateSalesQuotationResponse>?> CreateSalesQuotationAsync(CreateSalesQuotationRequest data)
    {
        var request = new RestRequest("api/sales-quotation", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<CreateSalesQuotationResponse>(_client, request);
    }

    public async Task<ApiResponse<bool>?> UpdateSalesQuotationAsync(UpdateSalesQuotationRequest data)
    {
        var request = new RestRequest("api/sales-quotation/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<bool>(_client, request);
    }

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

    public async Task<ApiResponse<bool>?> CreateSalesQuotationItemAsync(CreateSalesQuotationItemRequest data)
    {
        var request = new RestRequest("api/sales-quotation/sales-quotation-item", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<bool>(_client, request);
    }

    public async Task<ApiResponse<bool>?> UpdateSalesQuotationItemAsync(UpdateSalesQuotationItemRequest data)
    {
        var request = new RestRequest("api/sales-quotation/sales-quotation-item/update", Method.Post);
        request.AddJsonBody(data);
        return await ExecuteWithResponseAsync<bool>(_client, request);
    }

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