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 _logName = 'CustomerRepository';

  CustomerRepository(this._remoteDataProvider);

  @override
  Future<Either<CustomerFailure, ListCustomer>> getCustomer({
    int page = 1,
    int limit = 10,
  }) async {
    try {
      final result = await _remoteDataProvider.fetchCustomers(
        page: page,
        limit: limit,
      );

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

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