import { QueryTypes } from 'sequelize';
import { Migration } from '../migrate';

export const up: Migration = async ({ context }) => {
  try {
    await context.sequelize.query(
      "UPDATE payment_intents SET description = 'Thanks for your tip' WHERE description = 'Thanks for for your tip'",
      { type: QueryTypes.UPDATE }
    );

    await context.sequelize.query(
      `UPDATE payment_intents SET description = 'Thanks for your tip' 
       WHERE description = 'Thanks for your support'        
       AND id IN (
         SELECT payment_intent_id FROM checkout_sessions 
         WHERE submit_type = 'donate' AND payment_intent_id IS NOT NULL
       )`,
      { type: QueryTypes.UPDATE }
    );

    await context.sequelize.query(
      `UPDATE invoices SET description = 'Thanks for your tip' 
       WHERE (description = 'Thanks for your support' OR description = 'Thanks for for your tip')
       AND payment_intent_id IN (
         SELECT payment_intent_id FROM checkout_sessions 
         WHERE submit_type = 'donate' AND payment_intent_id IS NOT NULL
       )`,
      { type: QueryTypes.UPDATE }
    );

    await context.sequelize.query(
      `UPDATE payouts SET description = 'Thanks for your tip' 
       WHERE (description = 'Thanks for your support' OR description = 'Thanks for for your tip')
       AND payment_intent_id IN (
         SELECT payment_intent_id FROM checkout_sessions 
         WHERE submit_type = 'donate' AND payment_intent_id IS NOT NULL
       )`,
      { type: QueryTypes.UPDATE }
    );
  } catch (error) {
    console.error('Failed to update donation invoice descriptions', error);
  }
};

export const down = async () => {};
