import 'dart:developer';

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

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

@Injectable(as: IGameRepository)
class GameRepository implements IGameRepository {
  final GameRemoteDataProvider _remoteDataProvider;

  final String _logName = 'GameRepository';

  GameRepository(this._remoteDataProvider);

  @override
  Future<Either<GameFailure, List<GamePrize>>> gamePrizeByGameId({
    required String id,
  }) async {
    try {
      final result = await _remoteDataProvider.gamePrizeByGameId(id: id);

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

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

      return right(data);
    } catch (e, s) {
      log('gamePrizeByGameId', name: _logName, error: e, stackTrace: s);
      return left(const GameFailure.unexpectedError());
    }
  }
}
