CREATE TABLE IF NOT EXISTS users (
  `user_id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
  `chat_id` integer NOT NULL UNIQUE,
  `name` VARCHAR(64) NOT NULL,
  created_at datetime NOT NULL
);

CREATE TABLE IF NOT EXISTS parameters (
  `param_id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
  `user_id` integer NOT NULL,
  `key` VARCHAR(64) NOT NULL,
  `value` VARCHAR(64) NOT NULL,
  FOREIGN KEY(user_id)
    REFERENCES users(user_id)
      ON DELETE CASCADE
);

CREATE UNIQUE INDEX IF NOT EXISTS actions_action_id_user_id_idx 
  ON parameters (`user_id`, `key`);

CREATE TABLE IF NOT EXISTS actions (
  `action_id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,
  `user_id` integer NOT NULL,
  `name` VARCHAR(64) NOT NULL,
  FOREIGN KEY(user_id)
    REFERENCES users(user_id)
      ON DELETE CASCADE
);

-- drop index actions_action_id_user_id_idx;
-- drop table users;
-- drop table parameters;
-- drop table actions;
