import 'dart:developer';

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

import '../../../domain/order/order.dart';
import '../datasource/remote_data_provider.dart';

@Injectable(as: IOrderRepository)
class OrderRepository implements IOrderRepository {
  final OrderRemoteDataProvider _dataProvider;
  final String _logName = 'OrderRepository';

  OrderRepository(this._dataProvider);

  @override
  Future<Either<OrderFailure, List<Order>>> get({
    int page = 1,
    int limit = 20,
    String? status,
    String? search,
    required DateTime dateFrom,
    required DateTime dateTo,
  }) async {
    try {
      final result = await _dataProvider.fetch(
        page: page,
        limit: limit,
        status: status,
        search: search,
        dateFrom: dateFrom,
        dateTo: dateTo,
      );

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

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

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