import { DataTypes } from 'sequelize';

import { Migration, safeApplyColumnChanges } from '../migrate';

export const up: Migration = async ({ context }) => {
  // 如果customer存在overdraft_protection，则删除
  const columns = await context.describeTable('customers');
  if (columns.overdraft_protection) {
    await context.removeColumn('customers', 'overdraft_protection');
  }
  await safeApplyColumnChanges(context, {
    subscriptions: [
      {
        name: 'overdraft_protection',
        field: {
          type: DataTypes.JSON,
          defaultValue: JSON.stringify({
            enabled: false,
            payment_method_id: null,
            payment_details: null,
          }),
        },
      },
    ],
  });
};

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