'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    const table = 'production_final_inspection';
    const tables = await queryInterface.showAllTables();
    if (tables.includes(table)) return;

    await queryInterface.createTable(table, {
      id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
      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, unique: true, references: { model: 'route_card', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'CASCADE' },
      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' },
      customer_id: { type: Sequelize.INTEGER, allowNull: true, references: { model: 'business_partner', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'SET NULL' },
      order_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      accepted_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      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 },
      qc_by: { type: Sequelize.INTEGER, allowNull: true, references: { model: 'user', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'SET NULL' },
      qc_by_ids: { type: Sequelize.TEXT, allowNull: true },
      started_at: { type: Sequelize.DATE, allowNull: true },
      completed_at: { type: Sequelize.DATE, allowNull: true },
      status: { type: Sequelize.ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED'), allowNull: false, defaultValue: 'PENDING' },
      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') }
    });

    await queryInterface.addIndex(table, ['company_id', 'plant_id', 'fiscal_year_id'], { name: 'idx_pfi_ctx' });
    await queryInterface.addIndex(table, ['status'], { name: 'idx_pfi_status' });
  },

  async down(queryInterface) {
    const table = 'production_final_inspection';
    const tables = await queryInterface.showAllTables();
    if (!tables.includes(table)) return;
    await queryInterface.dropTable(table);
  }
};
