import 'dart:developer';

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

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

@Injectable(as: ICustomerRepository)
class CustomerRepository implements ICustomerRepository {
  final CustomerRemoteDataProvider _remoteDataProvider;

  final String _logName = 'CustomerRepository';

  CustomerRepository(this._remoteDataProvider);

  @override
  Future<Either<CustomerFailure, CustomerPoint>> getPoints() async {
    try {
      final result = await _remoteDataProvider.fetchCustomerPoint();

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

      final data = result.data!.toDomain();
      return right(data);
    } catch (e, s) {
      log('getPoints', name: _logName, error: e, stackTrace: s);
      return left(const CustomerFailure.unexpectedError());
    }
  }
}
