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

namespace Indotalent.Features.Sales.SalesReturn;

public class SalesReturnService : BaseService
{
    private readonly RestClient _client;

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

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

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

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

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

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

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

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

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