import SwiftUI
import SharedModels
import SwiftData

struct SettingsView: View {
    @Environment(\.cloudModelContextProvider) private var cloudModelContextProvider
    @State private var progress: UserProgress?

    @State private var dailyGoal: Double = 50
    @State private var showVosotros: Bool = true
    @State private var autoFillStem: Bool = false

    private let levels = VerbLevel.allCases
    private let irregularCategories: [IrregularSpan.SpanCategory] = [
        .spelling, .stemChange, .uniqueIrregular
    ]
    private var cloudModelContext: ModelContext { cloudModelContextProvider() }

    var body: some View {
        NavigationStack {
            Form {
                Section("Practice") {
                    VStack(alignment: .leading) {
                        Text("Daily Goal: \(Int(dailyGoal)) cards")
                        Slider(value: $dailyGoal, in: 10...200, step: 10)
                    }
                    .onChange(of: dailyGoal) { _, newValue in
                        progress?.dailyGoal = Int(newValue)
                        saveProgress()
                    }

                    Toggle("Include vosotros", isOn: $showVosotros)
                        .onChange(of: showVosotros) { _, newValue in
                            progress?.showVosotros = newValue
                            saveProgress()
                        }

                    Toggle("Auto-fill verb stem (Full Table)", isOn: $autoFillStem)
                        .onChange(of: autoFillStem) { _, newValue in
                            progress?.autoFillStem = newValue
                            saveProgress()
                        }
                }

                Section {
                    ForEach(levels, id: \.self) { level in
                        Toggle(level.displayName, isOn: Binding(
                            get: {
                                progress?.selectedVerbLevels.contains(level) ?? false
                            },
                            set: { enabled in
                                guard let progress else { return }
                                progress.setLevelEnabled(level, enabled: enabled)
                                saveProgress()
                            }
                        ))
                    }
                } header: {
                    Text("Levels")
                } footer: {
                    Text("Practice pulls only from verbs whose level is enabled. Turn on multiple to mix.")
                }

                Section {
                    ForEach(TenseInfo.all) { tense in
                        Toggle(tense.english, isOn: Binding(
                            get: {
                                progress?.enabledTenseIDs.contains(tense.id) ?? false
                            },
                            set: { enabled in
                                guard let progress else { return }
                                progress.setTenseEnabled(tense.id, enabled: enabled)
                                saveProgress()
                            }
                        ))
                    }
                } header: {
                    Text("Tenses")
                }

                Section {
                    ForEach(irregularCategories, id: \.self) { category in
                        Toggle(category.rawValue, isOn: Binding(
                            get: {
                                progress?.enabledIrregularCategories.contains(category) ?? false
                            },
                            set: { enabled in
                                guard let progress else { return }
                                progress.setIrregularCategoryEnabled(category, enabled: enabled)
                                saveProgress()
                            }
                        ))
                    }
                } header: {
                    Text("Irregular Types")
                } footer: {
                    Text("Leave all off to include regular and irregular verbs. Enable any to restrict practice to those irregularity types.")
                }

                Section {
                    Toggle("Reflexive verbs only", isOn: Binding(
                        get: { progress?.showReflexiveVerbsOnly ?? false },
                        set: { enabled in
                            progress?.showReflexiveVerbsOnly = enabled
                            saveProgress()
                        }
                    ))
                } header: {
                    Text("Reflexive")
                } footer: {
                    Text("When on, practice pulls only from the curated list of common reflexive verbs.")
                }

                Section("Stats") {
                    if let progress {
                        LabeledContent("Total Reviewed", value: "\(progress.totalReviewed)")
                        LabeledContent("Current Streak", value: "\(progress.currentStreak) days")
                        LabeledContent("Longest Streak", value: "\(progress.longestStreak) days")
                    }
                }

                Section("Reference") {
                    NavigationLink("How Features Work") {
                        FeatureReferenceView()
                    }
                    NavigationLink("Downloaded Videos") {
                        DownloadedVideosView()
                    }
                }

                Section("About") {
                    LabeledContent("Version", value: "1.0.0")
                }
            }
            .navigationTitle("Settings")
            .onAppear(perform: loadProgress)
        }
    }

    private func loadProgress() {
        let resolved = ReviewStore.fetchOrCreateUserProgress(context: cloudModelContext)
        progress = resolved
        dailyGoal = Double(resolved.dailyGoal)
        showVosotros = resolved.showVosotros
        autoFillStem = resolved.autoFillStem
    }

    private func saveProgress() {
        try? cloudModelContext.save()
    }
}

#Preview {
    SettingsView()
        .modelContainer(for: UserProgress.self, inMemory: true)
}
