-- Per-user Do Better Norge MCP tokens
-- Run against dobetternorge_maindb / local DBNM database.

CREATE TABLE IF NOT EXISTS user_mcp_tokens (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT NOT NULL,
  token_hash CHAR(64) NOT NULL,
  token_prefix VARCHAR(32) NOT NULL,
  name VARCHAR(100) NOT NULL DEFAULT 'Default',
  scopes JSON NULL,
  is_active TINYINT(1) NOT NULL DEFAULT 1,
  last_used_at DATETIME NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  revoked_at DATETIME NULL,
  PRIMARY KEY (id),
  KEY idx_hash (token_hash),
  KEY idx_user_active (user_id, is_active, revoked_at),
  CONSTRAINT fk_user_mcp_tokens_user
    FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
