import { MigrationInterface, QueryRunner } from 'typeorm';

export class OwnerScopeValuesAll1780930000000 implements MigrationInterface {
  name = 'OwnerScopeValuesAll1780930000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      UPDATE role_permissions rp
      INNER JOIN access_roles ar ON ar.id = rp.role_id
      SET rp.permission_value = 'all'
      WHERE ar.slug = 'owner'
        AND rp.permission_value IN ('own_and_all', 'own')
        AND rp.permission_key IN (
          'dashboard.view',
          'members.view',
          'members.update',
          'members.delete',
          'attendance.view',
          'leave.view',
          'leave.create',
          'leave.update',
          'leave.delete',
          'reports.view',
          'calendar.view',
          'holidays.view',
          'holidays.create',
          'holidays.update',
          'holidays.delete',
          'notice.view',
          'notice.create',
          'notice.update',
          'notice.delete'
        )
    `);
  }

  public async down(_queryRunner: QueryRunner): Promise<void> {
    // Cannot reliably restore prior permission_value values.
  }
}
