CREATE TABLE IF NOT EXISTS users (
 id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(120) NOT NULL, email VARCHAR(190) NOT NULL UNIQUE,
 password_hash VARCHAR(255) NOT NULL, role ENUM('admin','user') NOT NULL DEFAULT 'user',
 active TINYINT(1) NOT NULL DEFAULT 1, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS settings (
 setting_key VARCHAR(100) PRIMARY KEY, setting_value MEDIUMTEXT NULL,
 updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS appliances (
 id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, said VARCHAR(100) NOT NULL UNIQUE,
 name VARCHAR(190) NOT NULL, model_number VARCHAR(120) NULL, serial_number VARCHAR(120) NULL,
 data_model VARCHAR(190) NULL, last_state MEDIUMTEXT NULL, updated_at TIMESTAMP NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS audit_log (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, user_id INT UNSIGNED NULL,
 action VARCHAR(100) NOT NULL, status VARCHAR(20) NOT NULL, details MEDIUMTEXT NULL,
 ip_address VARCHAR(45) NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 INDEX(created_at), INDEX(user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
