'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('production_qc', {
      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'
      },
      fiscal_year_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'fiscal_year', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      route_card_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'route_card', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      route_card_operation_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        unique: true,
        references: { model: 'route_card_operation', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      work_order_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'work_order', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      production_order_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'production_order', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      operation_sequence_no: {
        type: Sequelize.INTEGER,
        allowNull: false
      },
      operation_code: {
        type: Sequelize.STRING(100),
        allowNull: true
      },
      operation_name: {
        type: Sequelize.STRING(500),
        allowNull: true
      },
      total_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      accepted_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      fr_rejected_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      mr_rejected_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      rejected_qty: {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      rejection_remarks: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      status: {
        type: Sequelize.ENUM('PENDING', 'IN_PROGRESS', 'PASSED', 'FAILED'),
        allowNull: false,
        defaultValue: 'PENDING'
      },
      moved_to_qc_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      moved_to_qc_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_qc', ['company_id', 'plant_id', 'fiscal_year_id'], {
      name: 'idx_production_qc_company_plant_fy'
    });
    await queryInterface.addIndex('production_qc', ['route_card_id'], {
      name: 'idx_production_qc_route_card'
    });
    await queryInterface.addIndex('production_qc', ['status'], {
      name: 'idx_production_qc_status'
    });
    await queryInterface.addIndex('production_qc', ['route_card_operation_id'], {
      unique: true,
      name: 'uniq_production_qc_operation'
    });
  },

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