'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('rejection_rework_record', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'company', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      report_no: { type: Sequelize.STRING(50), allowNull: true },
      record_date: { type: Sequelize.DATEONLY, allowNull: true },
      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: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    }, { charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci' });

    await queryInterface.createTable('rejection_rework_record_detail', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      record_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'rejection_rework_record', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE'
      },
      line_no: { type: Sequelize.INTEGER, allowNull: false },
      date: { type: Sequelize.DATEONLY, allowNull: true },
      machine_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: { model: 'machine', key: 'id' },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      machine_name: { type: Sequelize.STRING(200), allowNull: true },
      operator: { type: Sequelize.STRING(200), allowNull: true },
      part_description: { type: Sequelize.STRING(500), allowNull: true },
      drg_no: { type: Sequelize.STRING(100), allowNull: true },
      qty_prod: { type: Sequelize.DECIMAL(15, 3), allowNull: true },
      qty_rej: { type: Sequelize.DECIMAL(15, 3), allowNull: true },
      qty_rew: { type: Sequelize.DECIMAL(15, 3), allowNull: true },
      reason_for_rej: { type: Sequelize.TEXT, allowNull: true },
      disposal_action: { type: Sequelize.STRING(200), allowNull: true },
      signature: { type: Sequelize.STRING(200), allowNull: true },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    }, { charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci' });

    await queryInterface.addIndex('rejection_rework_record', ['company_id'], { name: 'idx_rrr_company_id' });
    await queryInterface.addIndex('rejection_rework_record', ['report_no'], { name: 'idx_rrr_report_no' });
    await queryInterface.addIndex('rejection_rework_record', ['record_date'], { name: 'idx_rrr_record_date' });
    await queryInterface.addIndex('rejection_rework_record_detail', ['record_id'], { name: 'idx_rrr_detail_record_id' });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('rejection_rework_record_detail');
    await queryInterface.dropTable('rejection_rework_record');
  }
};
