'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('material_reorder_level', {
      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'
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'material',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      storage_location: {
        type: Sequelize.STRING(10),
        allowNull: true,
        comment: 'Storage location specific reorder level (null = all locations)'
      },
      reorder_level: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0,
        comment: 'Reorder point - when stock reaches this level, reorder'
      },
      minimum_stock: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0,
        comment: 'Minimum stock level (safety stock)'
      },
      maximum_stock: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true,
        comment: 'Maximum stock level'
      },
      reorder_quantity: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: true,
        comment: 'Standard reorder quantity'
      },
      lead_time_days: {
        type: Sequelize.INTEGER,
        allowNull: true,
        defaultValue: 0,
        comment: 'Lead time in days for procurement'
      },
      last_reorder_date: {
        type: Sequelize.DATEONLY,
        allowNull: true,
        comment: 'Last date when reorder was triggered'
      },
      is_active: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      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'
        }
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });

    await queryInterface.addIndex('material_reorder_level', ['company_id']);
    await queryInterface.addIndex('material_reorder_level', ['plant_id']);
    await queryInterface.addIndex('material_reorder_level', ['material_id']);
    await queryInterface.addIndex('material_reorder_level', ['storage_location']);
    await queryInterface.addIndex('material_reorder_level', ['is_active']);
    await queryInterface.addIndex('material_reorder_level', ['status']);
    await queryInterface.addIndex('material_reorder_level', ['company_id', 'plant_id', 'material_id', 'storage_location'], {
      unique: true,
      name: 'unique_material_reorder_level'
    });
  },

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

