import SwiftUI
import Bedrock

/// Sheet for browsing and adding preset rituals
struct PresetLibrarySheet: View {
    @Bindable var store: RitualStore
    @Environment(\.dismiss) private var dismiss
    
    @State private var selectedCategory: PresetCategory = .health
    @State private var selectedPreset: RitualPreset?
    
    /// Track which presets have been added (by title match)
    private var addedPresetTitles: Set<String> {
        Set(store.rituals.map { $0.title })
    }
    
    var body: some View {
        NavigationStack {
            VStack(spacing: 0) {
                // Category picker
                categoryPicker
                
                // Presets list
                ScrollView(.vertical, showsIndicators: false) {
                    VStack(spacing: Design.Spacing.medium) {
                        ForEach(RitualPresetLibrary.presets(for: selectedCategory)) { preset in
                            presetCard(for: preset)
                        }
                    }
                    .padding(Design.Spacing.large)
                }
            }
            .background(AppSurface.primary)
            .navigationTitle(String(localized: "Preset Library"))
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .confirmationAction) {
                    Button(String(localized: "Done")) {
                        dismiss()
                    }
                    .foregroundStyle(AppAccent.primary)
                }
            }
            .sheet(item: $selectedPreset) { preset in
                PresetDetailSheet(store: store, preset: preset, isAlreadyAdded: addedPresetTitles.contains(preset.title))
            }
        }
        .presentationDetents([.large])
        .presentationDragIndicator(.visible)
    }
    
    // MARK: - Category Picker
    
    private var categoryPicker: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: Design.Spacing.small) {
                ForEach(PresetCategory.allCases, id: \.self) { category in
                    Button {
                        withAnimation(.easeInOut(duration: Design.Animation.quick)) {
                            selectedCategory = category
                        }
                    } label: {
                        Label(category.displayName, systemImage: category.symbolName)
                            .styled(.subheading, emphasis: .custom(selectedCategory == category ? .white : AppTextColors.primary))
                            .padding(.horizontal, Design.Spacing.medium)
                            .padding(.vertical, Design.Spacing.small)
                            .background(selectedCategory == category ? AppAccent.primary : AppSurface.card)
                            .clipShape(.capsule)
                    }
                    .buttonStyle(.plain)
                }
            }
            .padding(.horizontal, Design.Spacing.large)
            .padding(.vertical, Design.Spacing.medium)
        }
        .background(AppSurface.secondary)
    }
    
    // MARK: - Preset Card
    
    private func presetCard(for preset: RitualPreset) -> some View {
        let isAdded = addedPresetTitles.contains(preset.title)
        
        return Button {
            selectedPreset = preset
        } label: {
            VStack(alignment: .leading, spacing: Design.Spacing.medium) {
                HStack(spacing: Design.Spacing.medium) {
                    SymbolIcon(preset.iconName, size: .row, color: AppAccent.primary)
                        .frame(width: 40, height: 40)
                        .background(AppAccent.primary.opacity(0.1))
                        .clipShape(.rect(cornerRadius: Design.CornerRadius.medium))
                    
                    VStack(alignment: .leading, spacing: Design.Spacing.xSmall) {
                        HStack {
                            Text(preset.title).styled(.heading, emphasis: .primary)
                            
                            if isAdded {
                                SymbolIcon("checkmark.circle.fill", size: .badge, color: AppStatus.success)
                            }
                        }
                        
                        Text(preset.theme).styled(.subheading, emphasis: .secondary)
                    }
                    
                    Spacer()
                    
                    VStack(alignment: .trailing, spacing: Design.Spacing.xSmall) {
                        SymbolIcon(preset.timeOfDay.symbolName, size: .badge, color: AppTextColors.tertiary)
                        
                        Text(String(localized: "\(preset.habits.count) habits")).styled(.caption, emphasis: .tertiary)
                    }
                }
                
                // Habit preview
                HStack(spacing: Design.Spacing.small) {
                    ForEach(preset.habits.prefix(4)) { habit in
                        SymbolIcon(habit.symbolName, size: .badge, color: AppTextColors.tertiary)
                    }
                    
                    if preset.habits.count > 4 {
                        Text("+\(preset.habits.count - 4)").styled(.caption2, emphasis: .tertiary)
                    }
                }
            }
            .padding(Design.Spacing.large)
            .background(AppSurface.card)
            .clipShape(.rect(cornerRadius: Design.CornerRadius.large))
        }
        .buttonStyle(.plain)
    }
}

/// Detail sheet for a single preset with full info and add button
struct PresetDetailSheet: View {
    @Bindable var store: RitualStore
    let preset: RitualPreset
    let isAlreadyAdded: Bool
    
    @Environment(\.dismiss) private var dismiss
    @State private var hasBeenAdded = false
    
    var body: some View {
        NavigationStack {
            ScrollView(.vertical, showsIndicators: false) {
                VStack(alignment: .leading, spacing: Design.Spacing.xLarge) {
                    // Header
                    headerSection
                    
                    // Description
                    if !preset.notes.isEmpty {
                        descriptionSection
                    }
                    
                    // Habits
                    habitsSection
                    
                    // Add button
                    addButton
                    
                    Spacer(minLength: Design.Spacing.xxxLarge)
                }
                .padding(Design.Spacing.large)
            }
            .background(AppSurface.primary)
            .navigationTitle(preset.title)
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .confirmationAction) {
                    Button(String(localized: "Done")) {
                        dismiss()
                    }
                    .foregroundStyle(AppAccent.primary)
                }
            }
        }
        .presentationDetents([.large])
        .presentationDragIndicator(.visible)
    }
    
    private var headerSection: some View {
        VStack(spacing: Design.Spacing.medium) {
            SymbolIcon(preset.iconName, size: .hero, color: AppAccent.primary)
            
            Text(preset.theme)
                .typography(.body)
                .foregroundStyle(AppTextColors.secondary)
            
            HStack(spacing: Design.Spacing.large) {
                Label(preset.timeOfDay.displayName, systemImage: preset.timeOfDay.symbolName)
                Label(String(localized: "\(preset.durationDays) days"), systemImage: "calendar")
            }
            .typography(.caption)
            .foregroundStyle(AppTextColors.tertiary)
        }
        .frame(maxWidth: .infinity)
    }
    
    private var descriptionSection: some View {
        VStack(alignment: .leading, spacing: Design.Spacing.small) {
            Text(String(localized: "About")).styled(.heading, emphasis: .primary)
            
            Text(preset.notes).styled(.body, emphasis: .secondary)
        }
        .padding(Design.Spacing.large)
        .frame(maxWidth: .infinity, alignment: .leading)
        .background(AppSurface.card)
        .clipShape(.rect(cornerRadius: Design.CornerRadius.large))
    }
    
    private var habitsSection: some View {
        VStack(alignment: .leading, spacing: Design.Spacing.small) {
            Text(String(localized: "Habits")).styled(.heading, emphasis: .primary)
            
            VStack(spacing: Design.Spacing.xSmall) {
                ForEach(preset.habits) { habit in
                    HStack(spacing: Design.Spacing.medium) {
                        SymbolIcon(habit.symbolName, size: .inline, color: AppAccent.primary)
                            .frame(width: 24)
                        
                        Text(habit.title).styled(.subheading, emphasis: .primary)
                        
                        Spacer()
                    }
                    .padding(.vertical, Design.Spacing.small)
                    
                    if habit.id != preset.habits.last?.id {
                        Divider()
                            .background(AppBorder.subtle)
                    }
                }
            }
        }
        .padding(Design.Spacing.large)
        .frame(maxWidth: .infinity, alignment: .leading)
        .background(AppSurface.card)
        .clipShape(.rect(cornerRadius: Design.CornerRadius.large))
    }
    
    private var addButton: some View {
        Button {
            store.createRitualFromPreset(preset)
            hasBeenAdded = true
        } label: {
            Label(
                isAlreadyAdded || hasBeenAdded ? String(localized: "Added to My Rituals") : String(localized: "Add to My Rituals"),
                systemImage: isAlreadyAdded || hasBeenAdded ? "checkmark.circle.fill" : "plus.circle.fill"
            )
            .styled(.heading, emphasis: .custom(.white))
            .frame(maxWidth: .infinity)
            .padding(Design.Spacing.medium)
            .background(isAlreadyAdded || hasBeenAdded ? AppStatus.success : AppAccent.primary)
            .clipShape(.rect(cornerRadius: Design.CornerRadius.large))
        }
        .buttonStyle(.plain)
        .disabled(isAlreadyAdded || hasBeenAdded)
    }
}

#Preview {
    PresetLibrarySheet(store: RitualStore.preview)
}
