import { DataTypes } from 'sequelize';

import logger from '../../libs/logger';
import { Migration } from '../migrate';

export const up: Migration = async ({ context }) => {
  const [results] = await context.sequelize.query('PRAGMA table_info(payment_intents)');
  const columnInfo = (results as any[]).find((col: any) => col.name === 'quote_locked_at');

  if (columnInfo) {
    logger.info('quote_locked_at already exists on payment_intents, skipping migration');
    return;
  }

  await context.addColumn('payment_intents', 'quote_locked_at', {
    type: DataTypes.DATE,
    allowNull: true,
  });
};

export const down: Migration = async ({ context }) => {
  await context.removeColumn('payment_intents', 'quote_locked_at');
};
