import SwiftUI
import SwiftData

@MainActor
final class TagViewModel: ObservableObject {
    @Published var editingTag: Tag?
    @Published var maxPerWeek: Int? = nil
    @Published var noConsecutive: Bool = false
    @Published var noDuplicateInDay: Bool = false
    @Published var mealTypeRestriction: String? = nil
    @Published var useMaxLimit: Bool = true

    func loadTag(_ tag: Tag) {
        editingTag = tag
        maxPerWeek = tag.maxPerWeek
        noConsecutive = tag.noConsecutive
        noDuplicateInDay = tag.noDuplicateInDay
        mealTypeRestriction = tag.mealTypeRestriction
        useMaxLimit = tag.maxPerWeek != nil
    }

    func save() {
        guard let tag = editingTag else { return }
        tag.maxPerWeek = useMaxLimit ? (maxPerWeek ?? 3) : nil
        tag.noConsecutive = noConsecutive
        tag.noDuplicateInDay = noDuplicateInDay
        tag.mealTypeRestriction = mealTypeRestriction
    }

    func reset() {
        editingTag = nil
    }
}
