import SwiftUI

struct FeatureReferenceView: View {
    var body: some View {
        List {
            // MARK: Conjugation

            Section("Practice — Conjugation") {
                featureRow(
                    icon: "rectangle.stack", color: .blue,
                    title: "Flashcard / Typing / MC / Handwriting / Sentence Builder",
                    details: [
                        "Pulls from the verb conjugation database (1,750 verbs)",
                        "Filtered by your Level setting",
                        "Filtered by your Enabled Tenses",
                        "Respects the Include Vosotros setting",
                        "Due cards (SRS) shown first, then random",
                    ]
                )

                featureRow(
                    icon: "tablecells", color: .blue,
                    title: "Full Table",
                    details: [
                        "Shows all 6 person forms for one verb + tense",
                        "Drawn from any regular verb — Level filter is ignored here on purpose, since regular conjugation patterns transfer across vocabulary",
                        "Random tense from your Enabled Tenses",
                    ]
                )

                featureRow(
                    icon: "star.fill", color: .orange,
                    title: "Common Tenses",
                    details: [
                        "Restricts to 6 essential tenses: Present, Preterite, Imperfect, Future, Present Subjunctive, Imperative",
                        "Still filtered by your Level",
                    ]
                )

                featureRow(
                    icon: "exclamationmark.triangle", color: .red,
                    title: "Weak Verbs",
                    details: [
                        "Shows verbs you've struggled with (ease factor < 2.0)",
                        "Only includes verbs you've reviewed at least once",
                        "Weakest verbs shown first",
                    ]
                )

                featureRow(
                    icon: "wand.and.stars", color: .purple,
                    title: "Irregularity Drills",
                    details: [
                        "Spelling Changes: c->qu, g->gu, z->c patterns",
                        "Stem Changes: e->ie, o->ue, e->i patterns",
                        "Unique Irregulars: ser, ir, haber, etc.",
                        "Filtered by your Level and Enabled Tenses",
                    ]
                )
            }

            // MARK: Vocabulary

            Section("Practice — Vocabulary") {
                featureRow(
                    icon: "rectangle.on.rectangle.angled", color: .purple,
                    title: "Vocab Flashcards",
                    details: [
                        "English meaning → recall the Spanish verb",
                        "Pool = verbs at your enabled Levels (the same Level set the Verbs tab filters by)",
                        "Session size set by Settings → Cards per session",
                        "Overdue verbs pulled first, then new verbs by frequency",
                        "Quiz mode: tap to reveal, rate Again/Hard/Good/Easy. Again/Hard requeue the card a few cards later; a second Good or an Easy graduates it. Graduation updates the long-term SRS schedule.",
                        "Learn mode (toolbar toggle): both sides shown at once, Next/Previous to browse, loops — no rating, no pressure",
                        "Example sentence generated on-device; speaker button reads the verb aloud",
                    ]
                )

                featureRow(
                    icon: "checklist", color: .purple,
                    title: "Vocab Multiple Choice",
                    details: [
                        "English meaning → pick the Spanish verb from 4 options",
                        "Distractors prefer the same part of speech",
                        "Same level-filtered pool and SRS session queue as Vocab Flashcards",
                    ]
                )

                featureRow(
                    icon: "rectangle.stack.fill", color: .teal,
                    title: "Vocab Review",
                    details: [
                        "Reviews course/textbook vocabulary cards that are due (SRS scheduled)",
                        "Cards become due after you study them in Course quizzes",
                        "Rate Again/Hard/Good/Easy to schedule the next review",
                        "Distinct from Vocab Flashcards — this is course vocab, not the verb table",
                    ]
                )
            }

            // MARK: Reading

            Section("Practice — Reading") {
                featureRow(
                    icon: "book.fill", color: .teal,
                    title: "Stories",
                    details: [
                        "AI-generated one-paragraph Spanish stories",
                        "Matched to your Level and Enabled Tenses",
                        "Every word is tappable for a definition",
                        "English translation hidden by default (toggle to reveal)",
                        "3-question comprehension quiz at the end",
                        "Requires an Apple Intelligence-capable device",
                    ]
                )

                featureRow(
                    icon: "books.vertical.fill", color: .indigo,
                    title: "Books",
                    details: [
                        "Full-length bilingual books bundled with the app",
                        "Chapter list; read a chapter paragraph by paragraph",
                        "Tap any word for a definition (offline dictionary, on-device AI fallback)",
                        "Toggle between Spanish and pre-translated English",
                        "Read-aloud: a voice reads the chapter with the current word highlighted; tap a word to pause and look it up",
                        "Voice and speed picker in the read-aloud controls",
                    ]
                )

                featureRow(
                    icon: "music.note.list", color: .pink,
                    title: "Lyrics",
                    details: [
                        "Search and save Spanish song lyrics",
                        "Side-by-side Spanish and English",
                        "Long-press a word for a definition",
                    ]
                )

                featureRow(
                    icon: "bubble.left.and.bubble.right.fill", color: .green,
                    title: "Conversation",
                    details: [
                        "AI chat partner using Apple Intelligence (on-device)",
                        "10 scenario types (restaurant, directions, etc.)",
                        "AI adapts vocabulary to your Level setting",
                        "Corrections provided inline when you make mistakes",
                        "Requires an Apple Intelligence-capable device",
                    ]
                )

                featureRow(
                    icon: "ear.fill", color: .blue,
                    title: "Listening",
                    details: [
                        "Listen & Type: hear a sentence, type what you heard",
                        "Pronunciation: read a sentence aloud, get scored on accuracy",
                        "Sentences pulled from course vocabulary examples",
                    ]
                )

                featureRow(
                    icon: "text.badge.minus", color: .indigo,
                    title: "Cloze Practice",
                    details: [
                        "Fill in the missing word in a Spanish sentence",
                        "4 multiple-choice options (1 correct + 3 distractors)",
                        "Sentences from course vocabulary examples",
                    ]
                )
            }

            // MARK: Guide

            Section("Guide") {
                featureRow(
                    icon: "book", color: .brown,
                    title: "Tense Guides",
                    details: [
                        "In-depth guide to each of the 20 verb tenses",
                        "Conjugation ending tables, common irregulars, mnemonics",
                        "Usage patterns, pitfalls, and contrast with neighbouring tenses",
                        "Essential tenses marked with an orange badge",
                    ]
                )

                featureRow(
                    icon: "doc.text", color: .brown,
                    title: "Grammar Notes",
                    details: [
                        "36 grammar topics (ser vs estar, por vs para, WEIRDO, etc.)",
                        "Each with a mnemonic, contrast examples, and common pitfalls",
                        "Interactive exercises available on selected topics",
                    ]
                )

                featureRow(
                    icon: "arrow.triangle.branch", color: .indigo,
                    title: "Cross-links",
                    details: [
                        "Tense guides show \"Related grammar\" chips that jump to the matching grammar note",
                        "Grammar notes show \"Used in tenses\" chips that jump back",
                    ]
                )
            }

            // MARK: Course

            Section("Course") {
                featureRow(
                    icon: "list.clipboard", color: .orange,
                    title: "Course Quizzes",
                    details: [
                        "Vocabulary from specific course weeks",
                        "Multiple quiz types: MC, typing, handwriting, cloze",
                        "Not filtered by Level (uses course structure)",
                    ]
                )

                featureRow(
                    icon: "star.circle.fill", color: .yellow,
                    title: "Extra Study",
                    details: [
                        "Star a card during a course flashcard session to mark it for extra study",
                        "Each week shows an \"Extra Study\" row when it has starred cards",
                        "Launches a session of just the starred cards for that week",
                        "Marks are iCloud-synced across devices",
                    ]
                )

                featureRow(
                    icon: "checkmark.seal", color: .orange,
                    title: "Checkpoint Exams",
                    details: [
                        "Cumulative review across multiple weeks",
                        "Cards sampled evenly across all covered weeks",
                        "Choose 25, 50, or 100 questions",
                    ]
                )
            }

            // MARK: Dashboard

            Section("Dashboard") {
                featureRow(
                    icon: "clock.fill", color: .mint,
                    title: "Study Time",
                    details: [
                        "Tracks time the app is in the foreground",
                        "Shows today's time and an all-time total",
                        "7-day bar chart of daily study time",
                    ]
                )
            }

            // MARK: Settings

            Section("Settings That Affect Practice") {
                settingRow(name: "Level", affects: "Conjugation practice, Vocab Flashcards & Multiple Choice, Stories, Conversation. Shared with the Verbs tab filter. Full Table ignores level.")
                settingRow(name: "Enabled Tenses", affects: "Conjugation practice, Full Table, Irregularity Drills, Stories")
                settingRow(name: "Include Vosotros", affects: "Conjugation practice, Full Table, Common Tenses")
                settingRow(name: "Cards per session", affects: "How many verbs a Vocab Flashcards / Multiple Choice session draws")
                settingRow(name: "Daily Goal", affects: "Dashboard progress tracking only")
            }
        }
        .navigationTitle("How Features Work")
        .navigationBarTitleDisplayMode(.inline)
    }

    // MARK: - Components

    @ViewBuilder
    private func featureRow(icon: String, color: Color, title: String, details: [String]) -> some View {
        VStack(alignment: .leading, spacing: 8) {
            HStack(spacing: 10) {
                Image(systemName: icon)
                    .font(.body)
                    .foregroundStyle(color)
                    .frame(width: 24)
                Text(title)
                    .font(.subheadline.weight(.semibold))
            }

            VStack(alignment: .leading, spacing: 4) {
                ForEach(details, id: \.self) { detail in
                    HStack(alignment: .top, spacing: 6) {
                        Text("•")
                            .font(.caption)
                            .foregroundStyle(.secondary)
                        Text(detail)
                            .font(.caption)
                            .foregroundStyle(.secondary)
                    }
                }
            }
            .padding(.leading, 34)
        }
        .padding(.vertical, 4)
    }

    @ViewBuilder
    private func settingRow(name: String, affects: String) -> some View {
        VStack(alignment: .leading, spacing: 2) {
            Text(name)
                .font(.subheadline.weight(.semibold))
            Text(affects)
                .font(.caption)
                .foregroundStyle(.secondary)
        }
        .padding(.vertical, 2)
    }
}
