'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('bom_header', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      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'
      },
      bom_number: {
        type: Sequelize.STRING(40),
        allowNull: false
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      bom_type: {
        type: Sequelize.ENUM('PRODUCTION', 'ENGINEERING', 'SALES', 'PLANT_MAINTENANCE'),
        allowNull: false,
        defaultValue: 'PRODUCTION'
      },
      alternative: {
        type: Sequelize.STRING(2),
        allowNull: true,
        defaultValue: '01'
      },
      base_quantity: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: false,
        defaultValue: 1.000
      },
      base_uom_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'uom',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      valid_from: {
        type: Sequelize.DATEONLY,
        allowNull: false
      },
      valid_to: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE', 'LOCKED'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('bom_header', ['company_id', 'plant_id', 'bom_number', 'alternative'], { unique: true, name: 'bom_header_uniq_key' });
    await queryInterface.addIndex('bom_header', ['material_id']);
    await queryInterface.addIndex('bom_header', ['plant_id']);
    await queryInterface.addIndex('bom_header', ['status']);
    await queryInterface.addIndex('bom_header', ['valid_from', 'valid_to']);
  },

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

