'use strict';

/**
 * Remove Dashboard and ALL Material-related permissions from Production Manager role.
 * Production Manager should only have Production Planning and Production permissions.
 */
module.exports = {
  async up(queryInterface, Sequelize) {
    // Remove Dashboard and ALL Material permissions (MASTER.MATERIAL.%, MASTER.MATERIAL_TYPE.%, MASTER.MATERIAL_GROUP.%, etc.)
    // from ALL Production Manager roles (handles multiple companies)
    await queryInterface.sequelize.query(`
      DELETE rp FROM role_permission rp
      INNER JOIN role r ON r.id = rp.role_id
      INNER JOIN permission p ON p.id = rp.permission_id
      WHERE r.role_code = 'PROD_MGR' 
      AND (
        p.permission_code = 'DASHBOARD.VIEW'
        OR p.permission_code LIKE 'MASTER.MATERIAL.%'
        OR p.permission_code LIKE 'MASTER.MATERIAL_TYPE.%'
        OR p.permission_code LIKE 'MASTER.MATERIAL_GROUP.%'
        OR p.permission_code LIKE 'MASTER.MATERIAL_THICKNESS.%'
        OR p.permission_code LIKE 'MASTER.MATERIAL_GRADE.%'
        OR p.permission_code LIKE 'MASTER.MATERIAL_SHEET_TYPE.%'
        OR p.permission_code LIKE 'MASTER.MATERIAL_SUBSTITUTION.%'
      )
    `);
  },

  async down(queryInterface, Sequelize) {
    // Re-add permissions - would need to re-run role permission seed
    // Down migration intentionally no-op for safety
  }
};
