CREATE TABLE refresh_tokens (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    tenant_id BIGINT UNSIGNED NOT NULL,
    user_id BIGINT UNSIGNED NOT NULL,
    token_hash CHAR(64) NOT NULL,
    metadata JSON NULL,
    expires_at DATETIME(6) NOT NULL,
    last_used_at DATETIME(6) NULL DEFAULT NULL,
    revoked_at DATETIME(6) NULL DEFAULT NULL,
    created_at DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6),
    updated_at DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    PRIMARY KEY (id),
    UNIQUE KEY refresh_tokens_token_hash_unique (token_hash),
    KEY refresh_tokens_tenant_user_index (tenant_id, user_id),
    CONSTRAINT refresh_tokens_tenant_id_foreign FOREIGN KEY (tenant_id) REFERENCES tenants (id),
    CONSTRAINT refresh_tokens_user_id_foreign FOREIGN KEY (user_id) REFERENCES users (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
