'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    const table = 'production_qc';
    const cols = await queryInterface.describeTable(table);

    if (!cols.checked_qty) {
      await queryInterface.addColumn(table, 'checked_qty', {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0,
        after: 'accepted_qty'
      });
    }

    if (!cols.rework_qty) {
      await queryInterface.addColumn(table, 'rework_qty', {
        type: Sequelize.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0,
        after: 'checked_qty'
      });
    }

    if (!cols.rework_remarks) {
      await queryInterface.addColumn(table, 'rework_remarks', {
        type: Sequelize.TEXT,
        allowNull: true,
        after: 'rework_qty'
      });
    }

    await queryInterface.changeColumn(table, 'status', {
      type: Sequelize.ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED', 'APPROVED', 'FAILED'),
      allowNull: false,
      defaultValue: 'PENDING'
    });
  },

  async down(queryInterface, Sequelize) {
    const table = 'production_qc';
    const cols = await queryInterface.describeTable(table);

    if (cols.rework_remarks) {
      await queryInterface.removeColumn(table, 'rework_remarks');
    }
    if (cols.rework_qty) {
      await queryInterface.removeColumn(table, 'rework_qty');
    }
    if (cols.checked_qty) {
      await queryInterface.removeColumn(table, 'checked_qty');
    }

    await queryInterface.changeColumn(table, 'status', {
      type: Sequelize.ENUM('PENDING', 'IN_PROGRESS', 'PASSED', 'FAILED'),
      allowNull: false,
      defaultValue: 'PENDING'
    });
  }
};
