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

  CustomerRepository(this._dataProvider);

  @override
  Future<Either<CustomerFailure, List<Customer>>> get({
    int page = 1,
    int limit = 20,
    String? search,
  }) async {
    try {
      final result = await _dataProvider.fetch(
        page: page,
        limit: limit,
        search: search,
      );

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

      final auth = result.data!.map((item) => item.toDomain()).toList();

      return right(auth);
    } catch (e, s) {
      log('getCustomerError', name: _logName, error: e, stackTrace: s);
      return left(const CustomerFailure.unexpectedError());
    }
  }
}
