'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.addColumn('fg_dispatch', 'route_card_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: { model: 'route_card', key: 'id' },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL'
    });

    await queryInterface.addColumn('fg_dispatch', 'work_order_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: { model: 'work_order', key: 'id' },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL'
    });

    await queryInterface.addColumn('fg_dispatch', 'production_qty', {
      type: Sequelize.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    });

    await queryInterface.addColumn('fg_dispatch', 'accepted_qty', {
      type: Sequelize.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0
    });

    await queryInterface.addIndex('fg_dispatch', ['route_card_id'], { name: 'idx_fg_dispatch_route_card' });
    await queryInterface.addIndex('fg_dispatch', ['work_order_id'], { name: 'idx_fg_dispatch_work_order' });
  },

  async down(queryInterface) {
    await queryInterface.removeIndex('fg_dispatch', 'idx_fg_dispatch_route_card');
    await queryInterface.removeIndex('fg_dispatch', 'idx_fg_dispatch_work_order');
    await queryInterface.removeColumn('fg_dispatch', 'accepted_qty');
    await queryInterface.removeColumn('fg_dispatch', 'production_qty');
    await queryInterface.removeColumn('fg_dispatch', 'work_order_id');
    await queryInterface.removeColumn('fg_dispatch', 'route_card_id');
  }
};
