package eu.gaudian.translator.model

import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlin.reflect.KClass

@Serializable
@Parcelize
data class Exercise(
    val id: String,
    val title: String,
    val questions: List<Int>,
    val associatedVocabularyIds: List<Int> = emptyList(),
    val sourceLanguage: String? = null,
    val targetLanguage: String? = null,
    val contextTitle: String? = null,
    val contextText: String? = null,
    val youtubeUrl: String? = null
) : Parcelable


@Serializable
sealed class Question : Parcelable {
    abstract val id: Int
    abstract val name: String

    companion object {

        val allTypes: List<KClass<out Question>> = listOf(
            TrueFalseQuestion::class,
            MultipleChoiceQuestion::class,
            FillInTheBlankQuestion::class,
            WordOrderQuestion::class,
            MatchingPairsQuestion::class,
            ListeningComprehensionQuestion::class,
            CategorizationQuestion::class,
            VocabularyTestQuestion::class
        )
    }
}

@Parcelize
@Serializable
@SerialName("TrueFalseQuestion")
data class TrueFalseQuestion(
    override val id: Int,
    override val name: String,
    val correctAnswer: Boolean,
    val explanation: String = ""
) : Question()

@Parcelize
@Serializable
@SerialName("MultipleChoiceQuestion")
data class MultipleChoiceQuestion(
    override val id: Int,
    override val name: String,
    val options: List<String>,
    val correctAnswerIndex: Int
) : Question()

@Parcelize
@Serializable
@SerialName("FillInTheBlankQuestion")
data class FillInTheBlankQuestion(
    override val id: Int,
    override val name: String, // The sentence with a placeholder like "___"
    val correctAnswer: String,
    val hintBaseForm: String = "",
    val hintOptions: List<String> = emptyList()
) : Question()

@Parcelize
@Serializable
@SerialName("WordOrderQuestion")
data class WordOrderQuestion(
    override val id: Int,
    override val name: String, // The instruction, e.g., "Form the sentence."
    val words: List<String>, // The scrambled words
    val correctOrder: List<String>
) : Question()

@Parcelize
@Serializable
@SerialName("MatchingPairsQuestion")
data class MatchingPairsQuestion(
    override val id: Int,
    override val name: String, // e.g., "Match the English words to their German translation."
    val pairs: Map<String, String> // Key-value pairs to be matched
) : Question()

@Parcelize
@Serializable
@SerialName("ListeningComprehensionQuestion")
data class ListeningComprehensionQuestion(
    override val id: Int,
    override val name: String, // The text to be spoken and transcribed
    val languageCode: String // e.g., "en-US" for TTS
) : Question()

@Parcelize
@Serializable
@SerialName("CategorizationQuestion")
data class CategorizationQuestion(
    override val id: Int,
    override val name: String, // e.g., "Sort these into 'Fruit' and 'Vegetable' categories."
    val items: List<String>,
    val categories: List<String>,
    val correctMapping: Map<String, String> // Maps each item to its correct category
) : Question()

@Parcelize
@Serializable
@SerialName("VocabularyTestQuestion")
data class VocabularyTestQuestion(
    override val id: Int,
    override val name: String,
    val correctAnswer: String,
    val languageDirection: String
) : Question()