'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('material_plant', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      plant_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'plant',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      storage_location: {
        type: Sequelize.STRING(10),
        allowNull: true,
        comment: 'Storage location within plant'
      },
      mrp_type: {
        type: Sequelize.ENUM('ND', 'VB', 'PD', 'PK', 'VM'),
        allowNull: true,
        comment: 'MRP Type: ND=No Planning, VB=Manual Reorder Point, PD=MRP, PK=Consumption Based, VM=Forecast Based'
      },
      mrp_controller: {
        type: Sequelize.STRING(3),
        allowNull: true
      },
      reorder_point: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true,
        defaultValue: 0.000
      },
      safety_stock: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true,
        defaultValue: 0.000
      },
      max_stock: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true
      },
      procurement_type: {
        type: Sequelize.ENUM('E', 'F', 'X'),
        allowNull: true,
        comment: 'E=External Procurement, F=In-house Production, X=Both'
      },
      special_procurement: {
        type: Sequelize.STRING(2),
        allowNull: true
      },
      storage_bin: {
        type: Sequelize.STRING(10),
        allowNull: true
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE', 'BLOCKED'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      effective_from: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      effective_to: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      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('material_plant', ['material_id', 'plant_id'], { unique: true });
    await queryInterface.addIndex('material_plant', ['plant_id']);
    await queryInterface.addIndex('material_plant', ['status']);
    await queryInterface.addIndex('material_plant', ['effective_from', 'effective_to']);
  },

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

