'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('routing_operation', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      routing_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'routing',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      operation_number: {
        type: Sequelize.INTEGER,
        allowNull: false
      },
      work_center_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'work_center',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      operation_description: {
        type: Sequelize.STRING(500),
        allowNull: true
      },
      standard_text_key: {
        type: Sequelize.STRING(8),
        allowNull: true
      },
      control_key: {
        type: Sequelize.STRING(4),
        allowNull: true,
        comment: 'Operation control (e.g., PP01 for production operation)'
      },
      setup_time: {
        type: Sequelize.INTEGER,
        allowNull: true,
        defaultValue: 0,
        comment: 'Setup time in minutes'
      },
      machine_time: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: true,
        defaultValue: 0.00,
        comment: 'Machine time per unit'
      },
      labor_time: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: true,
        defaultValue: 0.00,
        comment: 'Labor time per unit'
      },
      base_quantity: {
        type: Sequelize.DECIMAL(15, 3),
        allowNull: true,
        defaultValue: 1.000,
        comment: 'Base quantity for time calculation'
      },
      queue_time: {
        type: Sequelize.INTEGER,
        allowNull: true,
        defaultValue: 0,
        comment: 'Queue time in minutes'
      },
      move_time: {
        type: Sequelize.INTEGER,
        allowNull: true,
        defaultValue: 0,
        comment: 'Move time in minutes'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
        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('routing_operation', ['routing_id', 'operation_number'], { unique: true });
    await queryInterface.addIndex('routing_operation', ['work_center_id']);
    await queryInterface.addIndex('routing_operation', ['status']);
  },

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

