CREATE TABLE IF NOT EXISTS app_state (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  state_key VARCHAR(100) NOT NULL UNIQUE,
  state_json LONGTEXT NOT NULL,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO app_state (state_key, state_json)
VALUES ('main', '{}')
ON DUPLICATE KEY UPDATE state_json = state_json;
