module.exports = {
  async up(queryInterface) {
    const createdAt = new Date();
    const updatedAt = new Date();
    
    const [roles] = await queryInterface.sequelize.query("SELECT id FROM roles WHERE name = 'Verified Business Owner' LIMIT 1;");
    const [permissions] = await queryInterface.sequelize.query("SELECT id FROM permissions WHERE name = 'CREATE_BUSINESSES' LIMIT 1;");
    
    if (roles && roles.length > 0 && permissions && permissions.length > 0) {
      const vboRoleId = roles[0].id;
      const createPermId = permissions[0].id;
      
      // Check if permission already exists for this role
      const [existing] = await queryInterface.sequelize.query(`SELECT * FROM "rolesPermissionsPermissions" WHERE "roles_permissionsId" = '${vboRoleId}' AND "permissionId" = '${createPermId}';`);
      
      if (existing.length === 0) {
        await queryInterface.bulkInsert("rolesPermissionsPermissions", [
          { createdAt, updatedAt, roles_permissionsId: vboRoleId, permissionId: createPermId }
        ]);
      }
    }
  },
  async down(queryInterface) {}
};
