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

export const up: Migration = async ({ context }) => {
  await context.createTable('product_vendors', {
    id: {
      type: 'STRING',
      primaryKey: true,
      allowNull: false,
      field: 'id',
    },
    vendor_key: {
      type: 'STRING',
      allowNull: false,
      unique: true,
      field: 'vendor_key',
    },
    name: {
      type: 'STRING',
      allowNull: false,
      field: 'name',
    },
    description: {
      type: 'TEXT',
      allowNull: true,
      field: 'description',
    },
    app_url: {
      type: 'STRING',
      allowNull: false,
      field: 'app_url',
    },
    app_pid: {
      type: 'STRING',
      allowNull: false,
      field: 'app_pid',
    },
    app_logo: {
      type: 'STRING',
      allowNull: true,
      field: 'app_logo',
    },
    webhook_path: {
      type: 'STRING',
      allowNull: true,
      field: 'webhook_path',
    },
    default_commission_rate: {
      type: 'DECIMAL',
      allowNull: false,
      field: 'default_commission_rate',
    },
    default_commission_type: {
      type: 'STRING',
      allowNull: false,
      field: 'default_commission_type',
    },
    order_create_params: {
      type: 'JSON',
      allowNull: true,
      defaultValue: '{}',
      field: 'order_create_params',
    },
    status: {
      type: 'STRING',
      allowNull: false,
      defaultValue: 'active',
      field: 'status',
    },
    metadata: {
      type: 'JSON',
      allowNull: true,
      defaultValue: '{}',
      field: 'metadata',
    },
    created_by: {
      type: 'STRING',
      allowNull: true,
      field: 'created_by',
    },
    created_at: {
      type: 'DATE',
      defaultValue: 'CURRENT_TIMESTAMP',
      allowNull: false,
      field: 'created_at',
    },
    updated_at: {
      type: 'DATE',
      defaultValue: 'CURRENT_TIMESTAMP',
      allowNull: false,
      field: 'updated_at',
    },
  });

  // 添加索引
  await createIndexIfNotExists(context, 'product_vendors', ['vendor_key'], 'idx_product_vendors_vendor_key');
  await createIndexIfNotExists(context, 'product_vendors', ['status'], 'idx_product_vendors_status');
  await createIndexIfNotExists(context, 'product_vendors', ['created_at'], 'idx_product_vendors_created_at');
};

export const down: Migration = async ({ context }) => {
  await context.dropTable('product_vendors');
};
