import 'package:english_learning/features/learning/modules/exercises/models/review_exercise_model.dart';

class ExerciseModel {
  final String idAdminExercise;
  final String idLevel;
  final String title;
  final String question;
  final String questionType;
  final String? audio;
  final String? video;
  final String? image;
  final DateTime timeAdminExc;
  final dynamic choices;
  final String? answerStudent;
  final bool? isCorrect;

  ExerciseModel({
    required this.idAdminExercise,
    required this.idLevel,
    required this.title,
    required this.question,
    required this.questionType,
    this.audio,
    this.video,
    this.image,
    required this.timeAdminExc,
    required this.choices,
    this.answerStudent,
    this.isCorrect,
  });

  factory ExerciseModel.fromJson(Map<String, dynamic> json) {
    dynamic choices;
    switch (json['QUESTION_TYPE']) {
      case 'MCQ':
        if (json['multipleChoices'] != null &&
            json['multipleChoices'].isNotEmpty) {
          choices = MultipleChoice.fromJson(json['multipleChoices'][0]);
        }
        break;
      case 'TFQ':
        if (json['trueFalse'] != null && json['trueFalse'].isNotEmpty) {
          choices = TrueFalse.fromJson(json['trueFalse'][0]);
        }
        break;
      case 'MPQ':
        if (json['matchingPairs'] != null && json['matchingPairs'].isNotEmpty) {
          choices = MatchingPair.fromJsonList(json['matchingPairs']);
        }
        break;
    }

    return ExerciseModel(
      idAdminExercise: json['ID_ADMIN_EXERCISE'],
      idLevel: json['ID_LEVEL'],
      title: json['TITLE'],
      question: json['QUESTION'],
      questionType: json['QUESTION_TYPE'],
      audio: json['AUDIO'],
      video: json['VIDEO'],
      image: json['IMAGE'],
      timeAdminExc: DateTime.parse(json['TIME_ADMIN_EXC']),
      choices: choices,
    );
  }
}

class MultipleChoice {
  final String idMultipleChoices;
  final String idAdminExercise;
  final String optionA;
  final String optionB;
  final String optionC;
  final String optionD;
  final String optionE;
  final DateTime timeMultipleChoices;

  MultipleChoice({
    required this.idMultipleChoices,
    required this.idAdminExercise,
    required this.optionA,
    required this.optionB,
    required this.optionC,
    required this.optionD,
    required this.optionE,
    required this.timeMultipleChoices,
  });

  factory MultipleChoice.fromJson(Map<String, dynamic> json) {
    return MultipleChoice(
      idMultipleChoices: json['ID_MULTIPLE_CHOICES'],
      idAdminExercise: json['ID_ADMIN_EXERCISE'],
      optionA: json['OPTION_A'],
      optionB: json['OPTION_B'],
      optionC: json['OPTION_C'],
      optionD: json['OPTION_D'],
      optionE: json['OPTION_E'],
      timeMultipleChoices: DateTime.parse(json['TIME_MULTIPLE_CHOICES']),
    );
  }
}

class TrueFalse {
  final String idTrueFalse;
  final String idAdminExercise;
  final DateTime timeTrueFalse;

  TrueFalse({
    required this.idTrueFalse,
    required this.idAdminExercise,
    required this.timeTrueFalse,
  });

  factory TrueFalse.fromJson(Map<String, dynamic> json) {
    return TrueFalse(
      idTrueFalse: json['ID_TRUE_FALSE'],
      idAdminExercise: json['ID_ADMIN_EXERCISE'],
      timeTrueFalse: DateTime.parse(json['TIME_TRUE_FALSE']),
    );
  }
}

class Pair {
  final String left;
  final String right;

  Pair({required this.left, required this.right});

  factory Pair.fromJson(Map<String, dynamic> json) {
    return Pair(
      left: json['LEFT_PAIR'] ?? '',
      right: json['RIGHT_PAIR'] ?? '',
    );
  }
}

class MatchingPair {
  final List<Pair> pairs;

  MatchingPair({required this.pairs});

  factory MatchingPair.fromJsonList(List? jsonList) {
    if (jsonList == null) {
      return MatchingPair(pairs: []);
    }

    List<Pair> pairs = jsonList.map((item) => Pair.fromJson(item)).toList();
    return MatchingPair(pairs: pairs);
  }

  // Helper methods
  List<String> get leftPairs => pairs.map((pair) => pair.left).toList();
  List<String> get rightPairs => pairs.map((pair) => pair.right).toList();
}

extension ReviewMatchingPairListExtension on List<ReviewMatchingPair> {
  MatchingPair toMatchingPair() {
    List<Pair> pairs = map((review) => Pair(
          left: review.leftPair,
          right: review.rightPair,
        )).toList();
    return MatchingPair(pairs: pairs);
  }
}
