import 'dart:developer';

import 'package:data_channel/data_channel.dart';
import 'package:injectable/injectable.dart';

import '../../../common/api/api_client.dart';
import '../../../common/api/api_failure.dart';
import '../../../common/function/app_function.dart';
import '../../../common/url/api_path.dart';
import '../../../domain/payment_method/payment_method.dart';
import '../payment_method_dtos.dart';

@injectable
class PaymentMethodRemoteDataProvider {
  final ApiClient _apiClient;
  final _logName = 'PaymentMethodRemoteDataProvider';

  PaymentMethodRemoteDataProvider(this._apiClient);

  Future<DC<PaymentMethodFailure, ListPaymentMethodDto>> fetchPaymentMethods({
    int page = 1,
    int limit = 30,
  }) async {
    try {
      final response = await _apiClient.get(
        ApiPath.paymentMethods,
        params: {'page': page, 'limit': limit},
        headers: getAuthorizationHeader(),
      );

      if (response.data['success'] == false) {
        return DC.error(const PaymentMethodFailure.unexpectedError());
      }

      final paymentMethods = ListPaymentMethodDto.fromJson(
        response.data['data'] as Map<String, dynamic>,
      );

      return DC.data(paymentMethods);
    } on ApiFailure catch (e, s) {
      log('fetchPaymentMethodsError', name: _logName, error: e, stackTrace: s);
      return DC.error(PaymentMethodFailure.serverError(e));
    }
  }
}
