import 'dart:developer';

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

import '../../../domain/payment_method/payment_method.dart';
import '../datasources/remote_data_provider.dart';

@Injectable(as: IPaymentMethodRepository)
class PaymentMethodRepository implements IPaymentMethodRepository {
  final PaymentMethodRemoteDataProvider _remoteDataProvider;
  final _logName = 'PaymentMethodRepository';

  PaymentMethodRepository(this._remoteDataProvider);

  @override
  Future<Either<PaymentMethodFailure, ListPaymentMethod>> getPaymentMethods({
    int page = 1,
    int limit = 30,
  }) async {
    try {
      final result = await _remoteDataProvider.fetchPaymentMethods(
        page: page,
        limit: limit,
      );

      if (result.hasError) {
        return left(result.error!);
      }

      final paymentMethods = result.data!.toDomain();
      return right(paymentMethods);
    } catch (e) {
      log('getPaymentMethodError', name: _logName, error: e);
      return left(const PaymentMethodFailure.unexpectedError());
    }
  }
}
