'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('production_order_component', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      production_order_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'production_order',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      bom_component_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'bom_component',
          key: 'id'
        }
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        }
      },
      required_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false
      },
      issued_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      backflushed_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      uom_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'uom',
          key: 'id'
        }
      },
      issue_type: {
        type: Sequelize.ENUM('MANUAL', 'BACKFLUSH', 'AUTO'),
        allowNull: false,
        defaultValue: 'MANUAL'
      },
      storage_location: {
        type: Sequelize.STRING(10),
        allowNull: true
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });

    await queryInterface.addIndex('production_order_component', ['production_order_id']);
    await queryInterface.addIndex('production_order_component', ['material_id']);
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('production_order_component');
  }
};

