import Foundation

struct RigCommand: Codable {
    let type: String
    var id: String?
    var key: String?
    var value: Double?
    var enabled: Bool?
    var name: String?

    var isValid: Bool {
        switch type {
        case "model":
            return valid(id)
        case "control":
            return valid(key) && value?.isFinite == true
        case "toggle":
            return valid(key) && enabled != nil
        case "preset", "background":
            return valid(name)
        case "reset":
            return true
        default:
            return false
        }
    }

    private func valid(_ text: String?, limit: Int = 64) -> Bool {
        guard let text, !text.isEmpty, text.count <= limit else { return false }
        return !text.unicodeScalars.contains { CharacterSet.controlCharacters.contains($0) }
    }
}
