import Foundation
import SwiftUI

/// User goals for onboarding that map to preset rituals.
enum OnboardingGoal: String, CaseIterable, Identifiable {
    case health
    case productivity
    case mindfulness
    case selfCare
    
    var id: String { rawValue }
    
    var displayName: String {
        switch self {
        case .health: return String(localized: "Health")
        case .productivity: return String(localized: "Focus")
        case .mindfulness: return String(localized: "Mindfulness")
        case .selfCare: return String(localized: "Self-Care")
        }
    }
    
    var subtitle: String {
        switch self {
        case .health: return String(localized: "Feel better each day")
        case .productivity: return String(localized: "Get more done")
        case .mindfulness: return String(localized: "Find your calm")
        case .selfCare: return String(localized: "Nurture yourself")
        }
    }
    
    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"
        }
    }
    
    /// Maps to PresetCategory for filtering presets.
    var presetCategory: PresetCategory {
        switch self {
        case .health: return .health
        case .productivity: return .productivity
        case .mindfulness: return .mindfulness
        case .selfCare: return .selfCare
        }
    }
}

/// User's preferred time for building habits.
enum OnboardingTimePreference: String, CaseIterable, Identifiable {
    case morning
    case evening
    case both
    
    var id: String { rawValue }
    
    var displayName: String {
        switch self {
        case .morning: return String(localized: "Morning")
        case .evening: return String(localized: "Evening")
        case .both: return String(localized: "Both")
        }
    }
    
    var subtitle: String {
        switch self {
        case .morning: return String(localized: "Start your day right")
        case .evening: return String(localized: "Wind down with intention")
        case .both: return String(localized: "Bookend your day")
        }
    }
    
    var symbolName: String {
        switch self {
        case .morning: return "sunrise.fill"
        case .evening: return "moon.fill"
        case .both: return "circle.righthalf.filled"
        }
    }
    
    /// Returns the TimeOfDay values to filter presets by.
    var timeOfDayFilters: [TimeOfDay] {
        switch self {
        case .morning: return [.morning]
        case .evening: return [.evening]
        case .both: return [.morning, .evening]
        }
    }
}

/// Service for recommending ritual presets based on onboarding selections.
enum OnboardingPresetRecommender {
    
    /// Returns the recommended preset for a given goal and time preference.
    /// - Parameters:
    ///   - goal: The user's selected goal
    ///   - time: The user's preferred time
    /// - Returns: A ritual preset, or nil if none matches
    static func recommendedPreset(for goal: OnboardingGoal, time: OnboardingTimePreference) -> RitualPreset? {
        let categoryPresets = RitualPresetLibrary.presets(for: goal.presetCategory)
        
        // Filter by time of day preference
        let timeFilters = time.timeOfDayFilters
        let matchingPresets = categoryPresets.filter { preset in
            timeFilters.contains(preset.timeOfDay)
        }
        
        // Return first matching preset, or first category preset as fallback
        return matchingPresets.first ?? categoryPresets.first
    }
    
    /// Returns all recommended presets when user selects "Both" for time.
    /// - Parameter goal: The user's selected goal
    /// - Returns: Morning and evening presets for the goal
    static func recommendedPresets(for goal: OnboardingGoal) -> (morning: RitualPreset?, evening: RitualPreset?) {
        let categoryPresets = RitualPresetLibrary.presets(for: goal.presetCategory)
        
        let morningPreset = categoryPresets.first { $0.timeOfDay == .morning }
        let eveningPreset = categoryPresets.first { $0.timeOfDay == .evening }
        
        return (morningPreset, eveningPreset)
    }
}
