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

export const up: Migration = async ({ context }) => {
  await safeApplyColumnChanges(context, {
    invoice_items: [
      {
        name: 'tax_rate_id',
        field: {
          type: DataTypes.STRING(30),
          allowNull: true,
        },
      },
    ],
  });
  await createIndexIfNotExists(context, 'invoice_items', ['tax_rate_id'], 'idx_invoice_items_tax_rate_id');
  await createIndexIfNotExists(context, 'invoice_items', ['invoice_id'], 'idx_invoice_items_invoice_id');
};

export const down: Migration = async ({ context }) => {
  await context.removeColumn('invoice_items', 'tax_rate_id');
  await context.removeIndex('invoice_items', 'idx_invoice_items_tax_rate_id');
  await context.removeIndex('invoice_items', 'idx_invoice_items_invoice_id');
};
