import 'dart:developer';

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

import '../../../common/api/api_client.dart';
import '../../../common/api/api_failure.dart';
import '../../../common/function/app_function.dart';
import '../../../common/url/api_path.dart';
import '../../../domain/game/game.dart';
import '../game_dtos.dart';

@injectable
class GameRemoteDataProvider {
  final ApiClient _apiClient;
  final String _logName = "GameRemoteDataProvider";

  GameRemoteDataProvider(this._apiClient);

  Future<DC<GameFailure, List<GamePrizeDto>>> gamePrizeByGameId({
    required String id,
  }) async {
    try {
      final response = await _apiClient.get(
        '${ApiPath.gamePrizeByGameId}/$id',
        headers: getAuthorizationHeader(),
      );

      if (response.data['code'] == 401) {
        return DC.error(
          GameFailure.serverError(ApiFailure.unauthorized('Session Expired')),
        );
      }

      if (response.data['status'] == false) {
        return DC.error(
          GameFailure.dynamicErrorMessage('Terjadi kesalahan coba lagi nanti'),
        );
      }

      final dto = (response.data['data'] as List)
          .map((e) => GamePrizeDto.fromJson(e))
          .toList();
      return DC.data(dto);
    } on ApiFailure catch (e, s) {
      log('gamePrizeByGameId', name: _logName, error: e, stackTrace: s);
      return DC.error(GameFailure.serverError(e));
    }
  }
}
