import Foundation
import SwiftData

@Model
final class AppSettings {
    var mealWindows: String // "dinnerOnly", "lunchOnly", "both"
    var includeWeekends: Bool
    var language: String // "spanish", "english"

    var calendarId: String?
    var syncEnabled: Bool
    var syncMode: String? // "weekComplete", "manual"
    var lunchTime: Date
    var dinnerTime: Date
    var eventDuration: Int
    var eventPrefix: String
    var reminderMinutesBefore: Int?
    var iCloudSyncEnabled: Bool?

    var isPremium: Bool
    var onboardingCompleted: Bool

    init() {
        self.mealWindows = "dinnerOnly"
        self.includeWeekends = true
        self.language = "system"
        self.calendarId = nil
        self.syncEnabled = false
        self.syncMode = CalendarSyncMode.weekComplete.rawValue
        // Default lunch time 14:00
        var lunchComponents = DateComponents()
        lunchComponents.hour = 14
        lunchComponents.minute = 0
        self.lunchTime = Calendar.current.date(from: lunchComponents) ?? Date()
        // Default dinner time 21:00
        var dinnerComponents = DateComponents()
        dinnerComponents.hour = 21
        dinnerComponents.minute = 0
        self.dinnerTime = Calendar.current.date(from: dinnerComponents) ?? Date()
        self.eventDuration = 60
        self.eventPrefix = "🍽️"
        self.reminderMinutesBefore = nil
        self.iCloudSyncEnabled = true
        self.isPremium = false
        self.onboardingCompleted = false
    }

    var mealWindowsEnum: MealWindows {
        get { MealWindows(rawValue: mealWindows) ?? .dinnerOnly }
        set { mealWindows = newValue.rawValue }
    }

    var languageEnum: AppLanguage {
        get { AppLanguage(rawValue: language) ?? .system }
        set { language = newValue.rawValue }
    }

    var syncModeEnum: CalendarSyncMode {
        get { CalendarSyncMode(rawValue: syncMode ?? "") ?? .weekComplete }
        set { syncMode = newValue.rawValue }
    }

    var iCloudSyncEnabledResolved: Bool {
        get { iCloudSyncEnabled ?? true }
        set { iCloudSyncEnabled = newValue }
    }
}

enum MealWindows: String, CaseIterable {
    case dinnerOnly
    case lunchOnly
    case both

    var localizedKey: String {
        switch self {
        case .dinnerOnly: return "meal_windows_dinner_only"
        case .lunchOnly: return "meal_windows_lunch_only"
        case .both: return "meal_windows_both"
        }
    }

    var icon: String {
        switch self {
        case .dinnerOnly: return "moon.stars.fill"
        case .lunchOnly: return "sun.max.fill"
        case .both: return "sun.and.horizon.fill"
        }
    }
}

enum AppLanguage: String, CaseIterable {
    case system
    case spanish
    case english

    var displayName: String {
        switch self {
        case .system: return "System"
        case .spanish: return "Español"
        case .english: return "English"
        }
    }

    var localeIdentifier: String {
        switch self {
        case .system: return resolved().localeIdentifier
        case .spanish: return "es"
        case .english: return "en"
        }
    }

    func resolved() -> AppLanguage {
        if self != .system { return self }
        return Locale.current.identifier.lowercased().hasPrefix("es") ? .spanish : .english
    }
}

enum MealType: String, Codable, CaseIterable {
    case lunch
    case dinner

    var icon: String {
        switch self {
        case .lunch: return "sun.max.fill"
        case .dinner: return "moon.stars.fill"
        }
    }
}

enum CalendarSyncMode: String, CaseIterable {
    case weekComplete
    case manual

    var localizedKey: String {
        switch self {
        case .weekComplete: return "settings_sync_mode_week_complete"
        case .manual: return "settings_sync_mode_manual"
        }
    }
}
