import Foundation import SwiftData // CloudKit-compatible (2.0): no unique constraints, inline defaults everywhere. @Model final class Dish { var id: UUID = UUID() var name: String = "" var descriptionText: String? var tagIds: [UUID] = [] var createdAt: Date = Date() var isPriority: Bool = false /// Optional ingredient lines (one per entry, e.g. "200g spaghetti"). /// Populated lazily — manually or via on-device generation — and only for /// the dishes a user actually shops for. Never required to create a dish. var ingredients: [String] = [] /// Optional dish photo. Stored outside the main store to keep it light and /// CloudKit-friendly. @Attribute(.externalStorage) var photoData: Data? /// Fixed-slot rule ("tortilla every Friday dinner"): when both are set, /// autocomplete pins this dish to that weekday+meal before filling the /// rest of the week. 0=Monday … 6=Sunday, matching `MealSlot.dayOfWeek`. var fixedDayOfWeek: Int? var fixedMealType: String? init( id: UUID = UUID(), name: String, descriptionText: String? = nil, tagIds: [UUID] = [], createdAt: Date = Date(), isPriority: Bool = false, ingredients: [String] = [], photoData: Data? = nil, fixedDayOfWeek: Int? = nil, fixedMealType: String? = nil ) { self.id = id self.name = name self.descriptionText = descriptionText self.tagIds = tagIds self.createdAt = createdAt self.isPriority = isPriority self.ingredients = ingredients self.photoData = photoData self.fixedDayOfWeek = fixedDayOfWeek self.fixedMealType = fixedMealType } } extension Dish { static func stableSortedForDisplay(_ dishes: [Dish]) -> [Dish] { dishes.sorted { lhs, rhs in if lhs.createdAt != rhs.createdAt { return lhs.createdAt > rhs.createdAt } return lhs.id.uuidString < rhs.id.uuidString } } }