import Foundation

enum QuizMode: String, Hashable {
    case enToZh = "英选汉"
    case zhToEn = "汉选英"
}

struct QuizSetupRequest: Hashable {
    let mode: QuizMode
    let words: [Word]
}

struct QuizConfig: Hashable {
    let mode: QuizMode
    let words: [Word]
}

struct QuizQuestion: Identifiable {
    var id: String { word.id }
    let word: Word
    let prompt: String
    let options: [String]
    let answer: String
}

enum QuizGenerator {
    static func makeQuestions(mode: QuizMode, words: [Word]) -> [QuizQuestion] {
        words.shuffled().map { word in
            let distractors = words.filter { $0 != word }.shuffled().prefix(3)
            let options = (distractors.map { optionText($0, mode) } + [optionText(word, mode)]).shuffled()
            return QuizQuestion(
                word: word,
                prompt: promptText(word, mode),
                options: options,
                answer: optionText(word, mode)
            )
        }
    }

    private static func promptText(_ word: Word, _ mode: QuizMode) -> String {
        mode == .enToZh ? word.word : word.meaning
    }

    private static func optionText(_ word: Word, _ mode: QuizMode) -> String {
        mode == .enToZh ? word.meaning : word.word
    }
}
