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

export const up: Migration = async ({ context }) => {
  await safeApplyColumnChanges(context, {
    products: [
      {
        name: 'vendor_config',
        field: {
          type: DataTypes.JSON,
          allowNull: true,
        },
      },
    ],
    checkout_sessions: [
      {
        name: 'fulfillment_status',
        field: {
          type: DataTypes.STRING,
          allowNull: true,
        },
      },
      {
        name: 'vendor_info',
        field: {
          type: DataTypes.JSON,
          allowNull: true,
        },
      },
    ],
    payouts: [
      {
        name: 'vendor_info',
        field: {
          type: DataTypes.JSON,
          allowNull: true,
        },
      },
    ],
  });

  await createIndexIfNotExists(
    context,
    'checkout_sessions',
    ['fulfillment_status'],
    'idx_checkout_sessions_fulfillment_status'
  );
};

export const down: Migration = async ({ context }) => {
  // 移除所有供应商相关字段
  await context.removeColumn('products', 'vendor_config');
  await context.removeColumn('checkout_sessions', 'vendor_info');
  await context.removeColumn('checkout_sessions', 'fulfillment_status');
  await context.removeColumn('payouts', 'vendor_info');
};
