'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('material_issue_item', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      issue_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material_issue',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      production_order_component_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'production_order_component',
          key: 'id'
        }
      },
      line_number: {
        type: Sequelize.INTEGER,
        allowNull: false
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        }
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: true
      },
      quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false
      },
      uom_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'uom',
          key: 'id'
        }
      },
      unit_price: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: false,
        defaultValue: 0
      },
      total_amount: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: false,
        defaultValue: 0
      },
      batch_number: {
        type: Sequelize.STRING(50),
        allowNull: true
      },
      storage_location: {
        type: Sequelize.STRING(10),
        allowNull: true
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });

    await queryInterface.addIndex('material_issue_item', ['issue_id']);
    await queryInterface.addIndex('material_issue_item', ['material_id']);
    await queryInterface.addIndex('material_issue_item', ['production_order_component_id']);
  },

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

