
module.exports = {
  async up(queryInterface, Sequelize) {
    const createdAt = new Date();
    const updatedAt = new Date();

    const [publicRole] = await queryInterface.sequelize.query(
      `SELECT id FROM "roles" WHERE name = 'Public' LIMIT 1;`
    );

    if (publicRole && publicRole.length > 0) {
      const publicRoleId = publicRole[0].id;

      const permissionsToGrant = [
        'CREATE_LEADS',
        'READ_SERVICES',
        'READ_PROJECTS',
        'READ_TESTIMONIALS',
        'READ_MEDIA_ASSETS',
        'READ_FAQS'
      ];

      const [permissions] = await queryInterface.sequelize.query(
        `SELECT id FROM "permissions" WHERE name IN (${permissionsToGrant.map(p => `'${p}'`).join(',')});`
      );

      const rolesPermissions = permissions.map(permission => ({
        createdAt,
        updatedAt,
        roles_permissionsId: publicRoleId,
        permissionId: permission.id
      }));

      await queryInterface.bulkInsert('rolesPermissionsPermissions', rolesPermissions);
    }
  },

  async down(queryInterface, Sequelize) {
    // Optional: remove permissions from Public role
  }
};
