const crypto = require('crypto')
const { query } = require('../config/database')

function md5(str) {
  return crypto.createHash('md5').update(str).digest('hex')
}

async function registerStaff() {
  try {
    const phone = '12370109282'
    const name = '董海洋'
    const password = md5('123456')
    
    const existing = await query('SELECT * FROM users WHERE phone = ?', [phone])
    
    if (existing.length > 0) {
      console.log('⚠️ 该手机号已注册')
      
      const user = existing[0]
      if (user.role === 1) {
        console.log('该账号已经是店员身份')
      } else {
        console.log('该账号是普通用户，正在升级为店员...')
        await query('UPDATE users SET role = 1 WHERE phone = ?', [phone])
        console.log('✅ 已升级为店员身份')
      }
      return
    }
    
    const result = await query(
      'INSERT INTO users (phone, password, name, avatar, points, role, status) VALUES (?, ?, ?, ?, ?, ?, ?)',
      [phone, password, name, '', 0, 1, 1]
    )
    
    console.log('✅ 店员注册成功！')
    console.log('姓名: ' + name)
    console.log('手机号: ' + phone)
    console.log('密码: 123456')
    
    process.exit(0)
  } catch (error) {
    console.error('注册失败:', error)
    process.exit(1)
  }
}

registerStaff()