import { DataTypes } from 'sequelize';

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

export const up: Migration = async ({ context }) => {
  await safeApplyColumnChanges(context, {
    checkout_sessions: [
      {
        name: 'enable_subscription_grouping',
        field: {
          type: DataTypes.BOOLEAN,
          defaultValue: false,
        },
      },
      {
        name: 'subscription_groups',
        field: {
          type: DataTypes.JSON,
          allowNull: true,
        },
      },
    ],
    payment_links: [
      {
        name: 'enable_subscription_grouping',
        field: {
          type: DataTypes.BOOLEAN,
          defaultValue: false,
        },
      },
    ],
  });
};

export const down: Migration = async ({ context }) => {
  await context.removeColumn('checkout_sessions', 'enable_subscription_grouping');
  await context.removeColumn('checkout_sessions', 'subscription_groups');
  await context.removeColumn('payment_links', 'enable_subscription_grouping');
};
