'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('production_qc_component_tracking', {
      id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
      production_qc_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'production_qc', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      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',
      },
      customer_po_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'customer_po', 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',
      },
      route_card_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'route_card', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      route_card_operation_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'route_card_operation', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      material_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'material', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT',
      },
      checked_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      rework_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      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: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') },
      updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') },
    });

    await queryInterface.addIndex(
      'production_qc_component_tracking',
      ['production_qc_id', 'material_id'],
      { unique: true, name: 'uq_pqc_component' }
    );
    await queryInterface.addIndex(
      'production_qc_component_tracking',
      ['route_card_id', 'route_card_operation_id'],
      { name: 'idx_pqc_component_rc_op' }
    );
  },

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

