'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // Add customer_po_id column
    await queryInterface.addColumn('production_order', 'customer_po_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: {
        model: 'customer_po',
        key: 'id'
      },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL',
      after: 'fiscal_year_id'
    });

    // Add customer_po_item_id column
    await queryInterface.addColumn('production_order', 'customer_po_item_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: {
        model: 'customer_po_item',
        key: 'id'
      },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL',
      after: 'customer_po_id'
    });

    // Add indexes for better query performance
    await queryInterface.addIndex('production_order', ['customer_po_id'], {
      name: 'idx_production_order_customer_po_id'
    });

    await queryInterface.addIndex('production_order', ['customer_po_item_id'], {
      name: 'idx_production_order_customer_po_item_id'
    });
  },

  async down(queryInterface, Sequelize) {
    // Remove indexes
    await queryInterface.removeIndex('production_order', 'idx_production_order_customer_po_item_id');
    await queryInterface.removeIndex('production_order', 'idx_production_order_customer_po_id');

    // Remove columns
    await queryInterface.removeColumn('production_order', 'customer_po_item_id');
    await queryInterface.removeColumn('production_order', 'customer_po_id');
  }
};

