import Foundation
import SwiftUI

/// Represents a category for organizing rituals.
/// Categories can be global presets (non-deletable) or user-created.
struct Category: Identifiable, Codable, Equatable, Hashable {
    let id: UUID
    var name: String
    var colorName: String
    let isPreset: Bool
    
    /// Available colors for categories
    static let availableColors: [(name: String, color: Color)] = [
        ("red", .red),
        ("orange", .orange),
        ("yellow", .yellow),
        ("green", .green),
        ("mint", .mint),
        ("teal", .teal),
        ("cyan", .cyan),
        ("blue", .blue),
        ("indigo", .indigo),
        ("purple", .purple),
        ("pink", .pink),
        ("brown", .brown),
        ("gray", .gray)
    ]
    
    /// The SwiftUI Color for this category
    var color: Color {
        Self.color(for: colorName)
    }
    
    /// Convert color name to SwiftUI Color
    static func color(for name: String) -> Color {
        availableColors.first { $0.name == name }?.color ?? .gray
    }
    
    /// Convert SwiftUI Color to storable name
    static func colorName(for color: Color) -> String {
        availableColors.first { $0.color == color }?.name ?? "gray"
    }
    
    // MARK: - Presets
    
    /// Fixed UUIDs for presets to ensure consistency across app launches
    private static let healthID = UUID(uuidString: "00000000-0000-0000-0000-000000000001")!
    private static let productivityID = UUID(uuidString: "00000000-0000-0000-0000-000000000002")!
    private static let mindfulnessID = UUID(uuidString: "00000000-0000-0000-0000-000000000003")!
    private static let selfCareID = UUID(uuidString: "00000000-0000-0000-0000-000000000004")!
    
    /// Default preset categories
    static let defaultPresets: [Category] = [
        Category(id: healthID, name: "Health", colorName: "red", isPreset: true),
        Category(id: productivityID, name: "Productivity", colorName: "blue", isPreset: true),
        Category(id: mindfulnessID, name: "Mindfulness", colorName: "purple", isPreset: true),
        Category(id: selfCareID, name: "Self-Care", colorName: "pink", isPreset: true)
    ]
    
    /// Create a new user category
    static func create(name: String, colorName: String) -> Category {
        Category(id: UUID(), name: name, colorName: colorName, isPreset: false)
    }
}
