import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Bolds the left-side field labels on the two MOVEMENT WhatsApp
 * wrappers using WhatsApp's inline `*bold*` marker. Ops asked for
 * "PICKUP TIME:" (and siblings) to stand out on the recipient's
 * device.
 *
 * Also swaps the customer body's hardcoded "Good Afternoon" for the
 * destination-timezone-aware `{{greeting_time_of_day}}` token — some
 * tenants seeded via the master seed (1783400) missed the greeting
 * resync (1783300) because their body already matched the greeting-
 * migration OLD-body verbatim only if it started with "Tomorrow";
 * master-seed tenants got "On ({{date_upper}})" so the earlier
 * resync UPDATE was a no-op on them.
 *
 * Idempotent: only overwrites rows whose current body matches the
 * exact prior master-seed body. Tenant customisations survive.
 */
export class CommunicationTemplatesMovementBoldLabels1783500000000
  implements MigrationInterface
{
  name = 'CommunicationTemplatesMovementBoldLabels1783500000000';

  public async up(qr: QueryRunner): Promise<void> {
    const oldSupplierBodyA = `TRANSFER'S SCHEDULE FOR {{date_upper}}

GUEST NAME: MR. {{guest_name_upper}}
SERVICE: {{service_upper}}

PAX: {{pax_line}}

PICKUP LOCATION: {{pickup_location}}
DROP LOCATION: {{drop_location}}
PICKUP TIME: {{pickup_time_12h}}
RETURN PICKUP TIME: {{return_time_12h}}
TRIP COORDINATOR: {{trip_coordinator.label}}
TRANSFER TYPE: {{transfer_type}}
{{#if service_body}}

{{service_body}}
{{/if}}

REMARKS: {{remarks}}

{{footer_line}}`;

    // Hybrid state — first four labels bolded (top of the block),
    // remaining labels unbold. Observed on tenants where an earlier
    // partial edit landed in the master-seed pass.
    const oldSupplierBodyB = `TRANSFER'S SCHEDULE FOR {{date_upper}}

*GUEST NAME:* MR. {{guest_name_upper}}
*SERVICE:* {{service_upper}}

*PAX:* {{pax_line}}

*PICKUP LOCATION:* {{pickup_location}}
DROP LOCATION: {{drop_location}}
PICKUP TIME: {{pickup_time_12h}}
RETURN PICKUP TIME: {{return_time_12h}}
TRIP COORDINATOR: {{trip_coordinator.label}}
TRANSFER TYPE: {{transfer_type}}
{{#if service_body}}

{{service_body}}
{{/if}}

REMARKS: {{remarks}}

{{footer_line}}`;

    const newSupplierBody = `TRANSFER'S SCHEDULE FOR {{date_upper}}

*GUEST NAME:* MR. {{guest_name_upper}}
*SERVICE:* {{service_upper}}

*PAX:* {{pax_line}}

*PICKUP LOCATION:* {{pickup_location}}
*DROP LOCATION:* {{drop_location}}
*PICKUP TIME:* {{pickup_time_12h}}
*RETURN PICKUP TIME:* {{return_time_12h}}
*TRIP COORDINATOR:* {{trip_coordinator.label}}
*TRANSFER TYPE:* {{transfer_type}}
{{#if service_body}}

{{service_body}}
{{/if}}

*REMARKS:* {{remarks}}

{{footer_line}}`;

    await qr.query(
      `UPDATE communication_templates
          SET body_template = $1
        WHERE channel = 'WHATSAPP'
          AND context = 'MOVEMENT_SUPPLIER'
          AND is_default = true
          AND is_deleted = false
          AND body_template IN ($2, $3)`,
      [newSupplierBody, oldSupplierBodyA, oldSupplierBodyB],
    );

    // Customer wrapper — two possible current bodies to catch:
    //   (a) Master-seed baseline with hardcoded "Good Afternoon" + "On ..."
    //   (b) Post-greeting-resync body with {{greeting_time_of_day}} + "On ..."
    // Both collapse to the same new bold body.
    const oldCustomerBodyA = `Hello! Good Afternoon, {{guest_name_titled}},

On ({{date_upper}}) we would like to remind you that your {{service_upper}} is scheduled.

Pickup Time: {{pickup_time_12h}}
{{#if pickup_location}}Pickup Location: {{pickup_location}}
{{/if}}{{#if service_body}}

{{service_body}}
{{/if}}

‼️ Please ensure your phone's 📱 data connection is active 📶 or that you have good Wi-Fi at the time of pickup. If you have an alternative contact number, kindly share it with us so we can serve you better. ‼️

Thank you for choosing us. We hope you have a pleasant experience!

{{footer_line}}`;

    const oldCustomerBodyB = `Hello! Good {{greeting_time_of_day}}, {{guest_name_titled}},

On ({{date_upper}}) we would like to remind you that your {{service_upper}} is scheduled.

*Pickup Time:* {{pickup_time_12h}}
{{#if pickup_location}}Pickup Location: {{pickup_location}}
{{/if}}{{#if service_body}}

{{service_body}}
{{/if}}

‼️ Please ensure your phone's 📱 data connection is active 📶 or that you have good Wi-Fi at the time of pickup. If you have an alternative contact number, kindly share it with us so we can serve you better. ‼️

Thank you for choosing us. We hope you have a pleasant experience!

{{footer_line}}`;

    const newCustomerBody = `Hello! Good {{greeting_time_of_day}}, {{guest_name_titled}},

On ({{date_upper}}) we would like to remind you that your {{service_upper}} is scheduled.

*Pickup Time:* {{pickup_time_12h}}
{{#if pickup_location}}*Pickup Location:* {{pickup_location}}
{{/if}}{{#if service_body}}

{{service_body}}
{{/if}}

‼️ Please ensure your phone's 📱 data connection is active 📶 or that you have good Wi-Fi at the time of pickup. If you have an alternative contact number, kindly share it with us so we can serve you better. ‼️

Thank you for choosing us. We hope you have a pleasant experience!

{{footer_line}}`;

    await qr.query(
      `UPDATE communication_templates
          SET body_template = $1
        WHERE channel = 'WHATSAPP'
          AND context = 'MOVEMENT_CUSTOMER'
          AND is_default = true
          AND is_deleted = false
          AND body_template IN ($2, $3)`,
      [newCustomerBody, oldCustomerBodyA, oldCustomerBodyB],
    );
  }

  public async down(): Promise<void> {
    // No revert — dropping bold markers would be a deliberate
    // regression on the ops-requested formatting.
  }
}
