import SwiftUI

struct QuizResultView: View {
    let total: Int
    let wrongWords: [Word]
    let duration: TimeInterval
    let onRestart: () -> Void
    let onExit: () -> Void

    private var correctCount: Int { total - wrongWords.count }

    private var durationText: String {
        let seconds = Int(duration)
        return seconds >= 60 ? "\(seconds / 60) 分 \(seconds % 60) 秒" : "\(seconds) 秒"
    }

    var body: some View {
        List {
            Section {
                VStack(spacing: 8) {
                    Text("\(correctCount) / \(total)")
                        .font(.system(size: 44, weight: .bold))
                    Text("用时 \(durationText)")
                        .font(.subheadline)
                        .foregroundStyle(.secondary)
                }
                .frame(maxWidth: .infinity)
                .padding(.vertical, 16)
            }

            if !wrongWords.isEmpty {
                Section("错题（\(wrongWords.count)）") {
                    ForEach(wrongWords) { word in
                        NavigationLink(value: AppRoute.word(word)) {
                            HStack {
                                Text(word.word)
                                    .font(.headline)
                                Spacer()
                                Text(word.meaning)
                                    .foregroundStyle(.secondary)
                            }
                        }
                    }
                }
            }

            Section {
                Button("再来一轮", action: onRestart)
                    .frame(maxWidth: .infinity)
                Button("返回", action: onExit)
                    .frame(maxWidth: .infinity)
                    .foregroundStyle(.secondary)
            }
        }
    }
}

#Preview {
    NavigationStack {
        QuizResultView(
            total: 10,
            wrongWords: [
                Word(word: "red", phonetic: "/red/", meaning: "红色", example: "The apple is red.", exampleMeaning: "苹果是红色的。")
            ],
            duration: 75,
            onRestart: {},
            onExit: {}
        )
    }
}
