import { DataTypes } from 'sequelize';

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

export const up: Migration = async ({ context }) => {
  // Check if column already allows null
  const [results] = await context.sequelize.query('PRAGMA table_info(payment_intents)');
  const columnInfo = (results as any[]).find((col: any) => col.name === 'setup_future_usage');

  // If column doesn't exist or already allows null (notnull = 0), skip migration
  if (!columnInfo || columnInfo.notnull === 0) {
    logger.info('setup_future_usage already allows null, skipping migration');
    return;
  }

  // Change setup_future_usage column to allow null
  await context.changeColumn('payment_intents', 'setup_future_usage', {
    type: DataTypes.ENUM('on_session', 'off_session'),
    allowNull: true,
  });
};

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