'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Add department_id to role table
    await queryInterface.addColumn('role', 'department_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: {
        model: 'department',
        key: 'id'
      },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL',
      comment: 'Department this role belongs to',
      after: 'company_id'
    });

    // Add index on department_id
    await queryInterface.addIndex('role', ['department_id'], {
      name: 'idx_role_department'
    });
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.removeIndex('role', 'idx_role_department');
    await queryInterface.removeColumn('role', 'department_id');
  }
};

