import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Single-control visibility (2026-10-10): the "Booking = Delete ⇒ see
 * everything" shortcut was removed, so the `view_all_bookings_queries`
 * flag is now the ONLY thing that grants cross-hierarchy visibility.
 *
 * Previously Admin (and others) got see-all implicitly via booking:delete,
 * so their flag was never set. Turn it ON for the system Admin + Accountant
 * roles — the only roles meant to see all data — so they keep full
 * visibility after the shortcut is gone. Everyone else stays scoped to
 * their own + reporting chain.
 */
export class ViewAllForAdminAccountant1787300000000 implements MigrationInterface {
  name = 'ViewAllForAdminAccountant1787300000000';

  public async up(qr: QueryRunner): Promise<void> {
    await qr.query(`
      UPDATE "roles"
      SET "view_all_bookings_queries" = true, "updated_at" = NOW()
      WHERE "is_deleted" = false
        AND "is_system" = true
        AND LOWER("name") IN ('admin', 'accountant')
    `);
  }

  public async down(qr: QueryRunner): Promise<void> {
    await qr.query(`
      UPDATE "roles"
      SET "view_all_bookings_queries" = false, "updated_at" = NOW()
      WHERE "is_deleted" = false
        AND "is_system" = true
        AND LOWER("name") IN ('admin', 'accountant')
    `);
  }
}
