import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Booking-owned markup config + markup_tax_amount (2026-10-14).
 *
 * Previously a booking edit sent the markup config but the service
 * dropped it, and the editor reloaded the QUOTE's markup — so the next
 * save silently reverted a booking-side markup and raised an
 * auto-refund. This adds the columns and backfills them:
 *
 *   - bookings: markup config, internal_comments, customer_remarks,
 *     markup_tax_amount.
 *   - query_quotes: markup_tax_amount.
 *   - Booking config copied from its source quote. Where the booking's
 *     selling_total no longer matches the quote's (markup edited on the
 *     booking), the config is re-expressed as an OVERALL / AMOUNT markup
 *     equal to the booking's actual markup so the editor reproduces the
 *     booking's own price.
 *
 * Schema + config only — no billed amounts change here.
 */
export class BookingMarkupAndMarkupTax1787900000000 implements MigrationInterface {
  name = 'BookingMarkupAndMarkupTax1787900000000';

  public async up(qr: QueryRunner): Promise<void> {
    await qr.query(`
      ALTER TABLE "bookings"
        ADD COLUMN IF NOT EXISTS "markup_tax_amount" numeric(12,2) NOT NULL DEFAULT 0,
        ADD COLUMN IF NOT EXISTS "pricing_strategy" varchar(30) NOT NULL DEFAULT 'overall',
        ADD COLUMN IF NOT EXISTS "markup_type" varchar(10) NOT NULL DEFAULT 'amount',
        ADD COLUMN IF NOT EXISTS "rounding" integer NOT NULL DEFAULT 1,
        ADD COLUMN IF NOT EXISTS "markup_amount" numeric(12,2) NOT NULL DEFAULT 0,
        ADD COLUMN IF NOT EXISTS "hotel_markup" numeric(12,2) NOT NULL DEFAULT 0,
        ADD COLUMN IF NOT EXISTS "transport_markup" numeric(12,2) NOT NULL DEFAULT 0,
        ADD COLUMN IF NOT EXISTS "activity_markup" numeric(12,2) NOT NULL DEFAULT 0,
        ADD COLUMN IF NOT EXISTS "special_markup" numeric(12,2) NOT NULL DEFAULT 0,
        ADD COLUMN IF NOT EXISTS "transport_excluded_pax" integer NOT NULL DEFAULT 0,
        ADD COLUMN IF NOT EXISTS "internal_comments" text NULL,
        ADD COLUMN IF NOT EXISTS "customer_remarks" text NULL
    `);
    await qr.query(`
      ALTER TABLE "query_quotes"
        ADD COLUMN IF NOT EXISTS "markup_tax_amount" numeric(12,2) NOT NULL DEFAULT 0
    `);

    await qr.query(`
      UPDATE "bookings" b SET
        pricing_strategy       = COALESCE(q.pricing_strategy, 'overall'),
        markup_type            = COALESCE(q.markup_type, 'amount'),
        rounding               = COALESCE(q.rounding, 1),
        markup_amount          = COALESCE(q.markup_amount, 0),
        hotel_markup           = COALESCE(q.hotel_markup, 0),
        transport_markup       = COALESCE(q.transport_markup, 0),
        activity_markup        = COALESCE(q.activity_markup, 0),
        special_markup         = COALESCE(q.special_markup, 0),
        transport_excluded_pax = COALESCE(q.transport_excluded_pax, 0),
        internal_comments      = q.internal_comments,
        customer_remarks       = q.customer_remarks
      FROM "query_quotes" q
      WHERE q.id = b.quote_id
    `);
    await qr.query(`
      UPDATE "bookings" b SET
        pricing_strategy = 'overall',
        markup_type      = 'amount',
        rounding         = 1,
        markup_amount    = ROUND(b.selling_total / NULLIF(b.exchange_rate, 0) - b.grand_total, 2),
        hotel_markup = 0, transport_markup = 0, activity_markup = 0, special_markup = 0
      FROM "query_quotes" q
      WHERE q.id = b.quote_id
        AND b.is_deleted = false
        AND b.selling_total > 0
        AND ABS(b.selling_total - q.selling_total) > 0.5
    `);
  }

  public async down(qr: QueryRunner): Promise<void> {
    await qr.query(`ALTER TABLE "query_quotes" DROP COLUMN IF EXISTS "markup_tax_amount"`);
    for (const col of [
      'markup_tax_amount',
      'pricing_strategy',
      'markup_type',
      'rounding',
      'markup_amount',
      'hotel_markup',
      'transport_markup',
      'activity_markup',
      'special_markup',
      'transport_excluded_pax',
      'internal_comments',
      'customer_remarks',
    ]) {
      await qr.query(`ALTER TABLE "bookings" DROP COLUMN IF EXISTS "${col}"`);
    }
  }
}
