import SwiftUI
import Combine

enum AppTheme: String, CaseIterable, Identifiable, Codable {
    case dark
    case light
    case lightbulb
    
    var id: String { rawValue }
    
    var displayName: String {
        switch self {
        case .dark: return "Default (Dark)"
        case .light: return "Light"
        case .lightbulb: return "Lightbulb"
        }
    }
}

class ThemeManager: ObservableObject {
    @Published var currentTheme: AppTheme {
        didSet {
            saveTheme()
        }
    }
    
    init() {
        if let data = UserDefaults.standard.data(forKey: "selectedTheme"),
           let theme = try? JSONDecoder().decode(AppTheme.self, from: data) {
            self.currentTheme = theme
        } else {
            self.currentTheme = .dark
        }
    }
    
    private func saveTheme() {
        if let data = try? JSONEncoder().encode(currentTheme) {
            UserDefaults.standard.set(data, forKey: "selectedTheme")
        }
    }
    
    // MARK: - Color Accessors
    
    var backgroundColor: Color {
        switch currentTheme {
        case .dark: return .black
        case .light: return .white
        case .lightbulb: return .black
        }
    }
    
    var textColor: Color {
        switch currentTheme {
        case .dark: return .white
        case .light: return .black
        case .lightbulb: return .yellow
        }
    }
    
    var secondaryTextColor: Color {
        switch currentTheme {
        case .dark: return .white.opacity(0.25)
        case .light: return .black.opacity(0.25)
        case .lightbulb: return .yellow.opacity(0.5)
        }
    }
    
    var menuBackgroundColor: Color {
        // Keeping menu dark for now as per usual side menu patterns, or should it match?
        // User asked for "main screen and voice screen" colors.
        // Let's make the menu adaptive too for consistency, or keep it dark?
        // "Theme Colors ... for the main screen and the voice screen"
        // Let's assume Side Menu should probably at least not clash.
        // For now, let's keep side menu dark to distinguish it, or maybe match?
        // Let's stick to modifying main and voice screens primarily as requested.
        // But the text in side menu is white. If we make background white, text needs to be black.
        return .black // Keeping side menu consistently dark for this iteration unless genericized.
    }
}
