import Foundation
import SwiftUI

/// A template for a habit within a preset ritual
struct HabitPreset: Identifiable {
    let id = UUID()
    let title: String
    let symbolName: String
}

/// A template ritual that users can add to their collection
struct RitualPreset: Identifiable {
    let id = UUID()
    let title: String
    let theme: String
    let notes: String
    let durationDays: Int
    let timeOfDay: TimeOfDay
    let iconName: String
    let category: String
    let habits: [HabitPreset]
}

/// Categories for organizing presets in the preset library
enum PresetCategory: String, CaseIterable {
    case health = "Health"
    case productivity = "Productivity"
    case mindfulness = "Mindfulness"
    case selfCare = "Self-Care"
    
    var displayName: String {
        rawValue
    }
    
    var symbolName: String {
        switch self {
        case .health: return "heart.fill"
        case .productivity: return "bolt.fill"
        case .mindfulness: return "brain.head.profile"
        case .selfCare: return "sparkles"
        }
    }
}

/// Library of preset rituals organized by category
enum RitualPresetLibrary {
    
    static var allPresets: [RitualPreset] {
        healthPresets + productivityPresets + mindfulnessPresets + selfCarePresets
    }
    
    static func presets(for category: PresetCategory) -> [RitualPreset] {
        switch category {
        case .health: return healthPresets
        case .productivity: return productivityPresets
        case .mindfulness: return mindfulnessPresets
        case .selfCare: return selfCarePresets
        }
    }
    
    // MARK: - Health Presets
    
    static let healthPresets: [RitualPreset] = [
        RitualPreset(
            title: String(localized: "Morning Hydration"),
            theme: String(localized: "Start your day refreshed"),
            notes: String(localized: "Build the habit of hydrating first thing in the morning."),
            durationDays: 21,
            timeOfDay: .morning,
            iconName: "drop.fill",
            category: PresetCategory.health.rawValue,
            habits: [
                HabitPreset(title: String(localized: "Drink a glass of water"), symbolName: "drop.fill"),
                HabitPreset(title: String(localized: "Take vitamins"), symbolName: "pill.fill"),
                HabitPreset(title: String(localized: "Eat a healthy breakfast"), symbolName: "fork.knife")
            ]
        ),
        RitualPreset(
            title: String(localized: "Midday Movement"),
            theme: String(localized: "Break up your day"),
            notes: String(localized: "Combat sedentary habits with midday activity."),
            durationDays: 28,
            timeOfDay: .midday,
            iconName: "figure.walk",
            category: PresetCategory.health.rawValue,
            habits: [
                HabitPreset(title: String(localized: "Take a 10-minute walk"), symbolName: "figure.walk"),
                HabitPreset(title: String(localized: "Stretch at your desk"), symbolName: "figure.flexibility"),
                HabitPreset(title: String(localized: "Drink water"), symbolName: "drop.fill")
            ]
        ),
        RitualPreset(
            title: String(localized: "Sleep Preparation"),
            theme: String(localized: "Rest better tonight"),
            notes: String(localized: "Wind down with habits that promote quality sleep."),
            durationDays: 28,
            timeOfDay: .evening,
            iconName: "moon.zzz.fill",
            category: PresetCategory.health.rawValue,
            habits: [
                HabitPreset(title: String(localized: "No caffeine after 2pm"), symbolName: "cup.and.saucer.fill"),
                HabitPreset(title: String(localized: "Dim lights 1 hour before bed"), symbolName: "lightbulb.fill"),
                HabitPreset(title: String(localized: "Set consistent bedtime"), symbolName: "bed.double.fill"),
                HabitPreset(title: String(localized: "Keep room cool"), symbolName: "thermometer.snowflake")
            ]
        )
    ]
    
    // MARK: - Productivity Presets
    
    static let productivityPresets: [RitualPreset] = [
        RitualPreset(
            title: String(localized: "Deep Work Prep"),
            theme: String(localized: "Set up for focus"),
            notes: String(localized: "Create the conditions for uninterrupted deep work."),
            durationDays: 21,
            timeOfDay: .morning,
            iconName: "brain",
            category: PresetCategory.productivity.rawValue,
            habits: [
                HabitPreset(title: String(localized: "Clear your desk"), symbolName: "tray.full.fill"),
                HabitPreset(title: String(localized: "Set phone to Do Not Disturb"), symbolName: "bell.slash.fill"),
                HabitPreset(title: String(localized: "Define your top 3 priorities"), symbolName: "list.number"),
                HabitPreset(title: String(localized: "Close unnecessary tabs"), symbolName: "xmark.square.fill")
            ]
        ),
        RitualPreset(
            title: String(localized: "End-of-Day Review"),
            theme: String(localized: "Close loops, plan ahead"),
            notes: String(localized: "Review your day and set up tomorrow for success."),
            durationDays: 28,
            timeOfDay: .evening,
            iconName: "checkmark.circle.fill",
            category: PresetCategory.productivity.rawValue,
            habits: [
                HabitPreset(title: String(localized: "Review completed tasks"), symbolName: "checkmark.square.fill"),
                HabitPreset(title: String(localized: "Clear your inbox"), symbolName: "envelope.fill"),
                HabitPreset(title: String(localized: "Plan tomorrow's priorities"), symbolName: "calendar.badge.plus"),
                HabitPreset(title: String(localized: "Tidy workspace"), symbolName: "sparkles")
            ]
        ),
        RitualPreset(
            title: String(localized: "Focus Reset"),
            theme: String(localized: "Regain clarity"),
            notes: String(localized: "When you feel scattered, use this to refocus."),
            durationDays: 14,
            timeOfDay: .anytime,
            iconName: "target",
            category: PresetCategory.productivity.rawValue,
            habits: [
                HabitPreset(title: String(localized: "Take 5 deep breaths"), symbolName: "wind"),
                HabitPreset(title: String(localized: "Write down what's on your mind"), symbolName: "pencil.and.list.clipboard"),
                HabitPreset(title: String(localized: "Choose ONE thing to focus on"), symbolName: "scope")
            ]
        )
    ]
    
    // MARK: - Mindfulness Presets
    
    static let mindfulnessPresets: [RitualPreset] = [
        RitualPreset(
            title: String(localized: "Morning Meditation"),
            theme: String(localized: "Start with stillness"),
            notes: String(localized: "A calm mind sets the tone for a calm day."),
            durationDays: 30,
            timeOfDay: .morning,
            iconName: "figure.mind.and.body",
            category: PresetCategory.mindfulness.rawValue,
            habits: [
                HabitPreset(title: String(localized: "5-minute meditation"), symbolName: "figure.mind.and.body"),
                HabitPreset(title: String(localized: "Set an intention for the day"), symbolName: "star.fill"),
                HabitPreset(title: String(localized: "Practice gratitude"), symbolName: "heart.fill")
            ]
        ),
        RitualPreset(
            title: String(localized: "Gratitude Practice"),
            theme: String(localized: "Find the good"),
            notes: String(localized: "Shift your focus to what's going well."),
            durationDays: 21,
            timeOfDay: .evening,
            iconName: "heart.text.square.fill",
            category: PresetCategory.mindfulness.rawValue,
            habits: [
                HabitPreset(title: String(localized: "Write 3 things you're grateful for"), symbolName: "list.bullet.clipboard.fill"),
                HabitPreset(title: String(localized: "Thank someone"), symbolName: "person.wave.2.fill"),
                HabitPreset(title: String(localized: "Reflect on a positive moment"), symbolName: "sun.max.fill")
            ]
        ),
        RitualPreset(
            title: String(localized: "Breathwork"),
            theme: String(localized: "Calm your nervous system"),
            notes: String(localized: "Use breath to reduce stress and increase focus."),
            durationDays: 14,
            timeOfDay: .anytime,
            iconName: "wind",
            category: PresetCategory.mindfulness.rawValue,
            habits: [
                HabitPreset(title: String(localized: "Box breathing (4-4-4-4)"), symbolName: "square"),
                HabitPreset(title: String(localized: "Body scan for tension"), symbolName: "figure.stand"),
                HabitPreset(title: String(localized: "Release shoulder tension"), symbolName: "arrow.down.circle.fill")
            ]
        ),
        RitualPreset(
            title: String(localized: "Evening Reflection"),
            theme: String(localized: "Process your day"),
            notes: String(localized: "Close the day with awareness and intention."),
            durationDays: 28,
            timeOfDay: .evening,
            iconName: "moon.stars.fill",
            category: PresetCategory.mindfulness.rawValue,
            habits: [
                HabitPreset(title: String(localized: "Journal for 5 minutes"), symbolName: "book.fill"),
                HabitPreset(title: String(localized: "What went well today?"), symbolName: "hand.thumbsup.fill"),
                HabitPreset(title: String(localized: "What could be better?"), symbolName: "lightbulb.fill"),
                HabitPreset(title: String(localized: "Let go of the day"), symbolName: "leaf.fill")
            ]
        )
    ]
    
    // MARK: - Self-Care Presets
    
    static let selfCarePresets: [RitualPreset] = [
        RitualPreset(
            title: String(localized: "Morning Skincare"),
            theme: String(localized: "Care for yourself"),
            notes: String(localized: "A consistent skincare routine for healthy skin."),
            durationDays: 28,
            timeOfDay: .morning,
            iconName: "drop.triangle.fill",
            category: PresetCategory.selfCare.rawValue,
            habits: [
                HabitPreset(title: String(localized: "Cleanse face"), symbolName: "drop.fill"),
                HabitPreset(title: String(localized: "Apply moisturizer"), symbolName: "humidity.fill"),
                HabitPreset(title: String(localized: "Apply sunscreen"), symbolName: "sun.max.fill")
            ]
        ),
        RitualPreset(
            title: String(localized: "Digital Detox"),
            theme: String(localized: "Disconnect to reconnect"),
            notes: String(localized: "Give your mind a break from screens."),
            durationDays: 21,
            timeOfDay: .evening,
            iconName: "iphone.slash",
            category: PresetCategory.selfCare.rawValue,
            habits: [
                HabitPreset(title: String(localized: "No screens 1 hour before bed"), symbolName: "iphone.slash"),
                HabitPreset(title: String(localized: "Read a physical book"), symbolName: "book.closed.fill"),
                HabitPreset(title: String(localized: "Have a real conversation"), symbolName: "person.2.fill")
            ]
        ),
        RitualPreset(
            title: String(localized: "Evening Wind-Down"),
            theme: String(localized: "Transition to rest"),
            notes: String(localized: "Create a buffer between your day and sleep."),
            durationDays: 28,
            timeOfDay: .evening,
            iconName: "moon.fill",
            category: PresetCategory.selfCare.rawValue,
            habits: [
                HabitPreset(title: String(localized: "Change into comfortable clothes"), symbolName: "tshirt.fill"),
                HabitPreset(title: String(localized: "Light a candle or dim lights"), symbolName: "flame.fill"),
                HabitPreset(title: String(localized: "Gentle stretching"), symbolName: "figure.flexibility"),
                HabitPreset(title: String(localized: "Herbal tea"), symbolName: "cup.and.saucer.fill")
            ]
        ),
        RitualPreset(
            title: String(localized: "Weekly Reset"),
            theme: String(localized: "Prepare for a fresh week"),
            notes: String(localized: "Sunday evening ritual to start Monday strong."),
            durationDays: 12,
            timeOfDay: .evening,
            iconName: "arrow.counterclockwise.circle.fill",
            category: PresetCategory.selfCare.rawValue,
            habits: [
                HabitPreset(title: String(localized: "Review last week"), symbolName: "calendar"),
                HabitPreset(title: String(localized: "Plan the week ahead"), symbolName: "calendar.badge.plus"),
                HabitPreset(title: String(localized: "Prepare clothes for Monday"), symbolName: "tshirt.fill"),
                HabitPreset(title: String(localized: "Tidy your space"), symbolName: "sparkles"),
                HabitPreset(title: String(localized: "Early bedtime"), symbolName: "bed.double.fill")
            ]
        )
    ]
}
