class ReviewExerciseModel {
  final String idStudentLearning;
  final String idLevel;
  final String nameSection;
  final String nameTopic;
  final String nameLevel;
  final int score;
  final bool isPass;
  final List<ReviewExerciseDetail> stdExercises;

  ReviewExerciseModel({
    required this.idStudentLearning,
    required this.idLevel,
    required this.nameSection,
    required this.nameTopic,
    required this.nameLevel,
    required this.score,
    required this.isPass,
    required this.stdExercises,
  });

  factory ReviewExerciseModel.fromJson(Map<String, dynamic> json) {
    return ReviewExerciseModel(
      idStudentLearning: json['ID_STUDENT_LEARNING'],
      idLevel: json['ID_LEVEL'],
      nameSection: json['NAME_SECTION'],
      nameTopic: json['NAME_TOPIC'],
      nameLevel: json['NAME_LEVEL'],
      score: json['SCORE'],
      isPass: json['IS_PASS'] == 1,
      stdExercises: (json['stdExercises'] as List)
          .map((e) => ReviewExerciseDetail.fromJson(e))
          .toList(),
    );
  }
}

class ReviewExerciseDetail {
  final String idStudentExercise;
  final String idAdminExercise;
  final String title;
  final String question;
  final String questionType;
  final int scoreWeight;
  final String? image;
  final String? video;
  final String? audio;

  final String answerStudent;
  final int isCorrect;
  final double resultScoreStudent;
  final List<ReviewMultipleChoice>? multipleChoices;
  final List<ReviewMatchingPair>? matchingPairs;

  ReviewExerciseDetail({
    required this.idStudentExercise,
    required this.idAdminExercise,
    required this.title,
    required this.question,
    required this.questionType,
    this.image,
    this.video,
    this.audio,
    required this.scoreWeight,
    required this.answerStudent,
    required this.isCorrect,
    required this.resultScoreStudent,
    this.multipleChoices,
    this.matchingPairs,
  });

  factory ReviewExerciseDetail.fromJson(Map<String, dynamic> json) {
    return ReviewExerciseDetail(
      idStudentExercise: json['ID_STUDENT_EXERCISE'] ?? '',
      idAdminExercise: json['ID_ADMIN_EXERCISE'] ?? '',
      title: json['TITLE'] ?? '',
      question: json['QUESTION'] ?? '',
      questionType: json['QUESTION_TYPE'] ?? '',
      image: json['IMAGE'],
      video: json['VIDEO'],
      audio: json['AUDIO'],
      scoreWeight: json['SCORE_WEIGHT'] ?? 0,
      answerStudent: json['ANSWER_STUDENT'] ?? '',
      isCorrect: json['IS_CORRECT'] ?? 0,
      resultScoreStudent:
          double.tryParse(json['RESULT_SCORE_STUDENT'].toString()) ?? 0.0,
      multipleChoices: json['multipleChoices'] != null
          ? (json['multipleChoices'] as List)
              .map((e) => ReviewMultipleChoice.fromJson(e))
              .toList()
          : null,
      matchingPairs: json['matchingPairs'] != null
          ? (json['matchingPairs'] as List)
              .map((e) => ReviewMatchingPair.fromJson(e))
              .toList()
          : null,
    );
  }

  List<ReviewMatchingPair> get matchingPairsOrEmpty {
    return matchingPairs ?? [];
  }

  List<ReviewMultipleChoice> get multipleChoicesOrEmpty {
    return multipleChoices ?? [];
  }
}

class ReviewMultipleChoice {
  final String optionA;
  final String optionB;
  final String optionC;
  final String optionD;
  final String optionE;

  ReviewMultipleChoice({
    required this.optionA,
    required this.optionB,
    required this.optionC,
    required this.optionD,
    required this.optionE,
  });

  factory ReviewMultipleChoice.fromJson(Map<String, dynamic> json) {
    return ReviewMultipleChoice(
      optionA: json['OPTION_A'] ?? '',
      optionB: json['OPTION_B'] ?? '',
      optionC: json['OPTION_C'] ?? '',
      optionD: json['OPTION_D'] ?? '',
      optionE: json['OPTION_E'] ?? '',
    );
  }
}

class ReviewMatchingPair {
  final String leftPair;
  final String rightPair;

  ReviewMatchingPair({
    required this.leftPair,
    required this.rightPair,
  });

  factory ReviewMatchingPair.fromJson(Map<String, dynamic> json) {
    return ReviewMatchingPair(
      leftPair: json['LEFT_PAIR'],
      rightPair: json['RIGHT_PAIR'],
    );
  }
}
