'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('material_issue_receive', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'company', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'plant', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
      },
      fiscal_year_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'fiscal_year', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
      },
      material_issue_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'material_issue', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      receive_number: {
        type: Sequelize.STRING(50),
        allowNull: true,
      },
      receive_date: {
        type: Sequelize.DATEONLY,
        allowNull: true,
      },
      received_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' },
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true,
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' },
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' },
      },
    });

    await queryInterface.createTable('material_issue_receive_item', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      material_issue_receive_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'material_issue_receive', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      material_issue_item_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'material_issue_item', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'material', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
      },
      uom_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'uom', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
      },
      received_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0,
      },
      batch_number: {
        type: Sequelize.STRING(50),
        allowNull: true,
      },
      received_from_storage_location: {
        type: Sequelize.STRING(10),
        allowNull: true,
      },
      received_to_storage_location: {
        type: Sequelize.STRING(10),
        allowNull: true,
      },
      remarks: {
        type: Sequelize.TEXT,
        allowNull: true,
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' },
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'user', key: 'id' },
      },
    });

    await queryInterface.addIndex('material_issue_receive_item', ['material_issue_item_id'], {
      name: 'idx_mir_item_issue_item',
    });
    await queryInterface.addIndex('material_issue_receive_item', ['material_issue_receive_id'], {
      name: 'idx_mir_item_receive',
    });
  },

  async down(queryInterface) {
    await queryInterface.dropTable('material_issue_receive_item');
    await queryInterface.dropTable('material_issue_receive');
  },
};

