import SwiftUI
import SharedModels

struct AnswerReviewView: View {
    let form: VerbForm?
    let spans: [IrregularSpan]
    let speechService: SpeechService
    let onRate: (ReviewQuality) -> Void
    var showAnswer: Bool = true
    var onPrevious: (() -> Void)? = nil
    var onNext: (() -> Void)? = nil

    init(
        form: VerbForm?,
        spans: [IrregularSpan] = [],
        speechService: SpeechService,
        onRate: @escaping (ReviewQuality) -> Void,
        showAnswer: Bool = true,
        onPrevious: (() -> Void)? = nil,
        onNext: (() -> Void)? = nil
    ) {
        self.form = form
        self.spans = spans
        self.speechService = speechService
        self.onRate = onRate
        self.showAnswer = showAnswer
        self.onPrevious = onPrevious
        self.onNext = onNext
    }

    var body: some View {
        VStack(spacing: 20) {
            // Correct form display with irregular highlighting
            if showAnswer, let form {
                VStack(spacing: 8) {
                    Text("Correct Answer")
                        .font(.caption)
                        .foregroundStyle(.secondary)
                        .textCase(.uppercase)

                    IrregularHighlightText(
                        form: form.form,
                        spans: spans,
                        font: .title.bold()
                    )
                }

                // Pronunciation button
                Button {
                    speechService.speak(form.form)
                } label: {
                    Label("Listen", systemImage: "speaker.wave.2.fill")
                        .font(.subheadline)
                        .padding(.horizontal, 16)
                        .padding(.vertical, 8)
                }
                .glassEffect(in: .capsule)

                // Regularity badge
                if form.regularity != "regular" {
                    Text(form.regularity.capitalized)
                        .font(.caption)
                        .foregroundStyle(.secondary)
                        .padding(.horizontal, 10)
                        .padding(.vertical, 4)
                        .background(.quaternary, in: .capsule)
                }

                Divider()
                    .padding(.horizontal)
            }

            // Rating buttons
            Text("How well did you know it?")
                .font(.subheadline)
                .foregroundStyle(.secondary)

            HStack(spacing: 12) {
                RatingButton(label: "Again", color: .red, quality: .again, action: onRate)
                RatingButton(label: "Hard", color: .orange, quality: .hard, action: onRate)
                RatingButton(label: "Good", color: .green, quality: .good, action: onRate)
                RatingButton(label: "Easy", color: .blue, quality: .easy, action: onRate)
            }
            .padding(.horizontal)

            // Navigation arrows
            HStack {
                if let onPrevious {
                    Button { onPrevious() } label: {
                        Label("Previous", systemImage: "chevron.left")
                            .font(.subheadline)
                    }
                    .tint(.secondary)
                }

                Spacer()

                if let onNext {
                    Button { onNext() } label: {
                        Label("Skip", systemImage: "chevron.right")
                            .font(.subheadline)
                            .labelStyle(.titleAndIcon)
                    }
                    .tint(.secondary)
                }
            }
            .padding(.horizontal)
        }
        .padding(.vertical)
    }
}

private struct RatingButton: View {
    let label: String
    let color: Color
    let quality: ReviewQuality
    let action: (ReviewQuality) -> Void

    var body: some View {
        Button {
            action(quality)
        } label: {
            Text(label)
                .font(.subheadline.weight(.semibold))
                .frame(maxWidth: .infinity)
                .padding(.vertical, 12)
        }
        .tint(color)
        .glassEffect(in: RoundedRectangle(cornerRadius: 12))
    }
}

#Preview {
    AnswerReviewView(
        form: nil,
        speechService: SpeechService(),
        onRate: { _ in }
    )
    .padding()
}
