import { type MigrationInterface, type QueryRunner } from 'typeorm';

/**
 * Retires the MARKUP module entirely (stakeholder decision 2026-05-13).
 *
 * Before this change, `markup:view` / `markup:edit` gated who could
 * see the selling-price breakdown on quotes/bookings. Sales Team and
 * Trip Coordinator had `markup:none` so the markup section was
 * hidden from them. Client feedback: every user with query/booking
 * access should see markup + selling totals; the separate gate adds
 * no value.
 *
 * This migration removes the leftover `role_permissions` rows for
 * the now-defunct module. The `module` column is varchar(100), not
 * an enum at the DB level, so orphaned rows would be harmless but
 * confusing in the admin UI's permissions matrix.
 *
 * Idempotent — re-running is a no-op.
 *
 * Code-side cleanup landed in the same commit:
 *   - ModuleName.MARKUP removed from the TS enum
 *   - Sales Team + Trip Coordinator SEED_ROLES no longer override MARKUP
 *   - BookingService.canViewBookingMarkup + BOOKING_MARKUP_FIELDS gone
 *   - QuoteService.canViewMarkup / canEditMarkup / stripMarkup gone
 *   - FE usePermission('markup') gates removed
 */
export class DropMarkupModule1778200000000 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    // Hard-delete (not soft-delete) — these rows have no business
    // meaning anymore. is_deleted=false rows would still appear in
    // the permissions admin UI.
    await queryRunner.query(`
      DELETE FROM role_permissions WHERE module = 'markup'
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // Down is intentionally a no-op. We don't have a faithful record
    // of which tenants × roles had which access_level for the markup
    // module before this migration. Restoring the rows would require
    // re-running the original seed logic, which would be wrong for
    // tenants where Admin had manually customised those rows.
    //
    // If you really need to revert, restore from a DB backup and
    // re-deploy the prior application version in lockstep.
    void queryRunner;
  }
}
