import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * One-shot force-sync of the Mellow Travels tenant's MOVEMENT
 * WhatsApp default templates to the local-dev canonical bodies
 * captured on 2026-07-02. Unlike the earlier idempotent resyncs
 * (1783500 / 1783600) this one overwrites unconditionally — the
 * intent is to push locally-perfected bodies onto the UAT tenant
 * regardless of whether an ops user has touched the row.
 *
 * Scope guard: keyed by company_name = 'Mellow Travels' so only
 * that specific tenant is affected. Other tenants (Flamingo Tours,
 * future onboarded tenants) stay on their current bodies.
 *
 * Safe to re-run — same overwrite each time. Rename the company
 * literal below if UAT ops changes it.
 */
export class CommunicationTemplatesMellowSync1783700000000
  implements MigrationInterface
{
  name = 'CommunicationTemplatesMellowSync1783700000000';

  public async up(qr: QueryRunner): Promise<void> {
    const supplierBody = `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 customerBody = `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 supplier_name}}*Supplier:* {{supplier_name}}{{#if supplier_phone}} — {{supplier_phone}}{{/if}}
{{/if}}{{#if driver_name}}*Driver:* {{driver_name}}{{#if driver_phone}} — {{driver_phone}}{{/if}}
{{/if}}{{#if remarks}}*Remarks:* {{remarks}}
{{/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 ct
          SET body_template = $1
         FROM tenant_settings ts
        WHERE ct.tenant_id = ts.tenant_id
          AND ts.company_name = 'Mellow Travels'
          AND ct.channel = 'WHATSAPP'
          AND ct.context = 'MOVEMENT_SUPPLIER'
          AND ct.is_default = true
          AND ct.is_deleted = false`,
      [supplierBody],
    );

    await qr.query(
      `UPDATE communication_templates ct
          SET body_template = $1
         FROM tenant_settings ts
        WHERE ct.tenant_id = ts.tenant_id
          AND ts.company_name = 'Mellow Travels'
          AND ct.channel = 'WHATSAPP'
          AND ct.context = 'MOVEMENT_CUSTOMER'
          AND ct.is_default = true
          AND ct.is_deleted = false`,
      [customerBody],
    );
  }

  public async down(): Promise<void> {
    // No revert — the previous state was tenant-specific and would
    // need to be captured per-row to restore. Ops can hand-edit the
    // template via Settings > Templates if they want to roll back.
  }
}
