'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    const table = 'production_packing';
    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' },
      packing_number: { type: Sequelize.STRING(60), allowNull: false, unique: true },
      route_card_id: { type: Sequelize.INTEGER, allowNull: false, references: { model: 'route_card', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'CASCADE' },
      reference_packing_id: { type: Sequelize.INTEGER, allowNull: true, references: { model: 'production_packing', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'SET NULL' },
      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' },
      finished_material_id: { type: Sequelize.INTEGER, allowNull: true, references: { model: 'material', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'SET NULL' },
      accepted_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      packed_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      balance_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      packer_ids: { type: Sequelize.TEXT, allowNull: true },
      related_route_card_ids: { type: Sequelize.TEXT, allowNull: true },
      actual_start: { type: Sequelize.DATE, allowNull: true },
      actual_end: { type: Sequelize.DATE, allowNull: true },
      remarks: { type: Sequelize.TEXT, allowNull: true },
      status: { type: Sequelize.ENUM('PENDING', 'IN_PROGRESS', 'PARTIAL_COMPLETED', '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_pp_ctx' });
    await queryInterface.addIndex(table, ['route_card_id'], { name: 'idx_pp_route_card_id' });
    await queryInterface.addIndex(table, ['reference_packing_id'], { name: 'idx_pp_reference_packing_id' });
    await queryInterface.addIndex(table, ['status'], { name: 'idx_pp_status' });
  },

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

