import 'package:dio/dio.dart';
import '../../domain/repositories/quiz_repository.dart';
import '../datasources/quiz_remote_datasource.dart';

class QuizRepositoryImpl implements QuizRepository {
  final QuizRemoteDatasource remote;

  QuizRepositoryImpl(this.remote);

  @override
  Future<FilterOptionsModel> getFilters() async {
    try {
      return await remote.getFilters();
    } on DioException catch (e) {
      throw e.error ?? e;
    }
  }

  @override
  Future<QuizStartResponseModel> startQuiz({
    required int numQuestions,
    String? difficulty,
    String? category,
  }) async {
    try {
      return await remote.startQuiz(
        numQuestions: numQuestions,
        difficulty: difficulty,
        category: category,
      );
    } on DioException catch (e) {
      throw e.error ?? e;
    }
  }

  @override
  Future<QuizSubmitResultModel> submitQuiz({
    required int attemptId,
    required List<Map<String, String>> answers,
  }) async {
    try {
      return await remote.submitQuiz(
        attemptId: attemptId,
        answers: answers,
      );
    } on DioException catch (e) {
      throw e.error ?? e;
    }
  }
}