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

export const up: Migration = async ({ context }) => {
  await safeApplyColumnChanges(context, {
    checkout_sessions: [
      {
        name: 'discounts',
        field: {
          type: DataTypes.JSON,
          allowNull: true,
        },
      },
    ],
    discounts: [
      {
        name: 'verification_method',
        field: {
          type: DataTypes.STRING(50),
          allowNull: true,
        },
      },
      {
        name: 'verification_data',
        field: {
          type: DataTypes.JSON,
          allowNull: true,
        },
      },
      {
        name: 'metadata',
        field: {
          type: DataTypes.JSON,
          allowNull: true,
        },
      },
    ],
    promotion_codes: [
      {
        name: 'verification_type',
        field: {
          type: DataTypes.ENUM('code', 'nft', 'vc', 'user_restricted'),
          defaultValue: 'code',
          allowNull: false,
        },
      },
      {
        name: 'nft_config',
        field: {
          type: DataTypes.JSON,
          allowNull: true,
        },
      },
      {
        name: 'vc_config',
        field: {
          type: DataTypes.JSON,
          allowNull: true,
        },
      },
      {
        name: 'customer_dids',
        field: {
          type: DataTypes.JSON,
          allowNull: true,
        },
      },
      {
        name: 'metadata',
        field: {
          type: DataTypes.JSON,
          allowNull: true,
        },
      },
      {
        name: 'created_via',
        field: {
          type: DataTypes.ENUM('api', 'dashboard', 'portal'),
          allowNull: true,
        },
      },
      {
        name: 'locked',
        field: {
          type: DataTypes.BOOLEAN,
          allowNull: false,
          defaultValue: false,
        },
      },
    ],
    coupons: [
      {
        name: 'created_via',
        field: {
          type: DataTypes.ENUM('api', 'dashboard', 'portal'),
          allowNull: true,
        },
      },
      {
        name: 'locked',
        field: {
          type: DataTypes.BOOLEAN,
          allowNull: false,
          defaultValue: false,
        },
      },
    ],
  });

  await createIndexIfNotExists(context, 'discounts', ['customer_id'], 'idx_discounts_customer_id');
  await createIndexIfNotExists(
    context,
    'promotion_codes',
    ['verification_type', 'coupon_id'],
    'idx_promotion_codes_verification_type_coupon_id'
  );
};

export const down: Migration = async ({ context }) => {
  await context.removeIndex('discounts', 'idx_discounts_customer_id');
  await context.removeIndex('promotion_codes', 'idx_promotion_codes_verification_type_coupon_id');

  await context.removeColumn('checkout_sessions', 'discounts');
  await context.removeColumn('discounts', 'verification_method');
  await context.removeColumn('discounts', 'verification_data');
  await context.removeColumn('discounts', 'metadata');
  await context.removeColumn('promotion_codes', 'verification_type');
  await context.removeColumn('promotion_codes', 'nft_config');
  await context.removeColumn('promotion_codes', 'vc_config');
  await context.removeColumn('promotion_codes', 'customer_dids');
  await context.removeColumn('promotion_codes', 'metadata');
  await context.removeColumn('promotion_codes', 'created_via');
  await context.removeColumn('promotion_codes', 'locked');
  await context.removeColumn('coupons', 'created_via');
  await context.removeColumn('coupons', 'locked');
};
