CREATE TABLE IF NOT EXISTS users (
  user_id BIGINT PRIMARY KEY,
  first_name VARCHAR(64) NULL,
  username VARCHAR(64) NULL,
  joined_at DATETIME NOT NULL,
  last_seen DATETIME NOT NULL,
  is_blocked TINYINT NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS price_cache (
  id TINYINT PRIMARY KEY,
  json_data MEDIUMTEXT NOT NULL,
  updated_at INT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS broadcast_log (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  sent INT NOT NULL,
  failed INT NOT NULL,
  created_at DATETIME NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Alerts
CREATE TABLE IF NOT EXISTS alerts (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  user_id BIGINT NOT NULL,
  asset VARCHAR(16) NOT NULL,
  target DOUBLE NOT NULL,
  direction ENUM('above','below') NOT NULL DEFAULT 'above',
  is_active TINYINT NOT NULL DEFAULT 1,
  created_at DATETIME NOT NULL,
  triggered_at DATETIME NULL,
  INDEX(user_id),
  INDEX(asset),
  INDEX(is_active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- VIP
CREATE TABLE IF NOT EXISTS vip_users (
  user_id BIGINT PRIMARY KEY,
  expires_at DATETIME NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
