-- Minimal schema for bot-factory (multi-tenant)
CREATE TABLE IF NOT EXISTS bots (
  id INT AUTO_INCREMENT PRIMARY KEY,
  owner_user_id BIGINT NULL,
  bot_username VARCHAR(64) NOT NULL,
  bot_token VARCHAR(120) NOT NULL,
  template VARCHAR(50) NOT NULL DEFAULT 'uploader',
  webhook_secret VARCHAR(80) NOT NULL,
  webhook_set TINYINT(1) NOT NULL DEFAULT 0,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uniq_username (bot_username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Example child tables shared by all bots (use bot_id for scoping)
CREATE TABLE IF NOT EXISTS bot_users (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  bot_id INT NOT NULL,
  tg_user_id BIGINT NOT NULL,
  first_name VARCHAR(255) NULL,
  username VARCHAR(255) NULL,
  lang VARCHAR(10) NULL,
  joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uniq_bot_user (bot_id, tg_user_id),
  CONSTRAINT fk_bot_users_bot FOREIGN KEY (bot_id) REFERENCES bots(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

