/**
 * One-time helper script to add role_permissions column to organizations table.
 * Run with: node src/scripts/add_role_permissions_column.js
 */
const { pool } = require('../config/database')

async function ensureRolePermissionsColumn () {
  const checkSql = `
    SELECT COUNT(*) AS cnt 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = DATABASE() 
      AND TABLE_NAME = 'organizations' 
      AND COLUMN_NAME = 'role_permissions'
  `
  const alterSql = `
    ALTER TABLE organizations 
    ADD COLUMN role_permissions TEXT NULL AFTER theme
  `
  try {
    const [rows] = await pool.query(checkSql)
    const exists = rows && rows[0] && Number(rows[0].cnt) > 0
    if (exists) {
      console.log('✅ Column role_permissions already exists. Nothing to do.')
      process.exit(0)
    }
    await pool.query(alterSql)
    console.log('✅ Added role_permissions TEXT column to organizations table.')
    process.exit(0)
  } catch (err) {
    console.error('❌ Failed to add role_permissions column:', err)
    process.exit(1)
  }
}

ensureRolePermissionsColumn()

