import SwiftUI
import SharedModels
import SwiftData

struct DeckStudyView: View {
    let deck: CourseDeck
    @Environment(\.modelContext) private var modelContext
    @State private var isStudying = false
    @State private var speechService = SpeechService()
    @State private var deckCards: [VocabCard] = []

    var body: some View {
        cardListView
            .navigationTitle(deck.title)
            .navigationBarTitleDisplayMode(.inline)
            .onAppear { loadCards() }
            .fullScreenCover(isPresented: $isStudying) {
                NavigationStack {
                    VocabFlashcardView(
                        cards: deckCards.shuffled(),
                        speechService: speechService,
                        onDone: { isStudying = false }
                    )
                    .toolbar {
                        ToolbarItem(placement: .cancellationAction) {
                            Button("Close") { isStudying = false }
                        }
                    }
                }
            }
    }

    private func loadCards() {
        let deckId = deck.id
        let descriptor = FetchDescriptor<VocabCard>(
            predicate: #Predicate<VocabCard> { $0.deckId == deckId },
            sortBy: [SortDescriptor(\VocabCard.front)]
        )
        deckCards = (try? modelContext.fetch(descriptor)) ?? []
    }

    // MARK: - Card List

    private var cardListView: some View {
        ScrollView {
            VStack(spacing: 16) {
                // Header
                VStack(spacing: 8) {
                    Text("Week \(deck.weekNumber)")
                        .font(.subheadline)
                        .foregroundStyle(.secondary)

                    Text(deck.title)
                        .font(.title2.weight(.bold))

                    HStack(spacing: 12) {
                        Label("\(deckCards.count) cards", systemImage: "rectangle.on.rectangle")

                        if deck.isReversed {
                            Label("EN → ES", systemImage: "arrow.right")
                        } else {
                            Label("ES → EN", systemImage: "arrow.right")
                        }
                    }
                    .font(.caption)
                    .foregroundStyle(.secondary)
                }
                .padding(.vertical, 8)

                // Study button
                Button {
                    withAnimation { isStudying = true }
                } label: {
                    Label("Study Flashcards", systemImage: "brain.head.profile")
                        .font(.headline)
                        .frame(maxWidth: .infinity)
                        .padding(.vertical, 14)
                }
                .buttonStyle(.borderedProminent)
                .tint(.orange)
                .padding(.horizontal)

                // Card list
                LazyVStack(spacing: 0) {
                    ForEach(Array(deckCards.enumerated()), id: \.offset) { _, card in
                        VStack(alignment: .leading, spacing: 8) {
                            HStack(spacing: 10) {
                                Button {
                                    speechService.speak(card.front)
                                } label: {
                                    Image(systemName: "speaker.wave.2")
                                        .font(.body)
                                        .foregroundStyle(.blue)
                                        .frame(width: 32, height: 32)
                                        .contentShape(Rectangle())
                                }
                                .buttonStyle(.borderless)

                                Text(card.front)
                                    .font(.body.weight(.medium))

                                Spacer()

                                Text(card.back)
                                    .font(.body)
                                    .foregroundStyle(.secondary)
                                    .multilineTextAlignment(.trailing)
                            }

                            // Example sentences
                            if !card.examplesES.isEmpty {
                                VStack(alignment: .leading, spacing: 6) {
                                    ForEach(Array(zip(card.examplesES, card.examplesEN).enumerated()), id: \.offset) { _, pair in
                                        VStack(alignment: .leading, spacing: 2) {
                                            Text(pair.0)
                                                .font(.caption)
                                                .italic()
                                            Text(pair.1)
                                                .font(.caption)
                                                .foregroundStyle(.secondary)
                                        }
                                    }
                                }
                                .padding(.leading, 42)
                            }
                        }
                        .padding(.horizontal, 16)
                        .padding(.vertical, 12)

                        Divider()
                            .padding(.leading, 16)
                    }
                }
                .background(.fill.quinary, in: RoundedRectangle(cornerRadius: 12))
                .padding(.horizontal)
            }
            .padding(.vertical)
            .adaptiveContainer()
        }
    }
}

#Preview {
    NavigationStack {
        DeckStudyView(deck: CourseDeck(
            id: "test", weekNumber: 1, title: "Greetings", cardCount: 5,
            courseName: "Test", isReversed: false
        ))
    }
    .modelContainer(for: [CourseDeck.self, VocabCard.self], inMemory: true)
}
