'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('production_operator', {
      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',
      },
      operator_name: {
        type: Sequelize.STRING(100),
        allowNull: false,
      },
      operation_name_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'operation_name', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
      },
      work_center_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'work_center', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
      },
      designation: {
        type: Sequelize.STRING(100),
        allowNull: true,
      },
      mobile_no: {
        type: Sequelize.STRING(20),
        allowNull: true,
      },
      shift: {
        type: Sequelize.ENUM('SHIFT_A', 'SHIFT_B', 'SHIFT_C', 'GENERAL'),
        allowNull: false,
        defaultValue: 'GENERAL',
      },
      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('production_operator', ['company_id'], { name: 'idx_production_operator_company' });
    await queryInterface.addIndex('production_operator', ['operation_name_id'], { name: 'idx_production_operator_operation_name' });
    await queryInterface.addIndex('production_operator', ['work_center_id'], { name: 'idx_production_operator_work_center' });
    await queryInterface.addIndex('production_operator', ['status'], { name: 'idx_production_operator_status' });
  },

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

