import { DataTypes } from 'sequelize';
import { AutoRechargeConfig } from '../models/auto-recharge-config';
import { createIndexIfNotExists, Migration, safeApplyColumnChanges } from '../migrate';

export const up: Migration = async ({ context }) => {
  await context.createTable('auto_recharge_configs', AutoRechargeConfig.GENESIS_ATTRIBUTES);

  await createIndexIfNotExists(
    context,
    'auto_recharge_configs',
    ['customer_id'],
    'idx_auto_recharge_configs_customer_id'
  );

  await createIndexIfNotExists(
    context,
    'auto_recharge_configs',
    ['currency_id'],
    'idx_auto_recharge_configs_currency_id'
  );

  await safeApplyColumnChanges(context, {
    payment_currencies: [
      {
        name: 'recharge_config',
        field: {
          type: DataTypes.JSON,
          allowNull: true,
        },
      },
    ],
  });
};

export const down: Migration = async ({ context }) => {
  await context.dropTable('auto_recharge_configs');
  await context.removeColumn('payment_currencies', 'recharge_config');
};
