/**
 * scripts/rename-user.cjs
 * 将"冯周充"改为"詹老师"（users + skills + audit_logs）
 * 用法：node scripts/rename-user.cjs
 */
const { DatabaseSync } = require('node:sqlite')
const path = require('path')

const DB_PATH = process.env.DB_PATH || path.join(__dirname, '../data/aipro.db')
console.log('[rename] DB:', DB_PATH)

const db = new DatabaseSync(DB_PATH)
db.exec('PRAGMA journal_mode = WAL')

const OLD_NAME = '冯周充'
const NEW_NAME = '詹老师'

// 1. users.name
const r1 = db.prepare('UPDATE users SET name = ? WHERE name = ?').run(NEW_NAME, OLD_NAME)
console.log(`[users.name] 更新 ${r1.changes} 条`)

// 2. skills.creator_name
const r2 = db.prepare('UPDATE skills SET creator_name = ? WHERE creator_name = ?').run(NEW_NAME, OLD_NAME)
console.log(`[skills.creator_name] 更新 ${r2.changes} 条`)

// 3. audit_logs.actor_name
const r3 = db.prepare('UPDATE audit_logs SET actor_name = ? WHERE actor_name = ?').run(NEW_NAME, OLD_NAME)
console.log(`[audit_logs.actor_name] 更新 ${r3.changes} 条`)

db.close()
console.log(`✅ 改名完成: "${OLD_NAME}" → "${NEW_NAME}"`)
