'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    const table = 'production_fg_inward';
    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' },
      fg_inward_number: { type: Sequelize.STRING(60), allowNull: false, unique: true },
      packing_id: { type: Sequelize.INTEGER, allowNull: false, references: { model: 'production_packing', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'RESTRICT' },
      route_card_id: { type: Sequelize.INTEGER, allowNull: true, references: { model: 'route_card', 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' },
      storage_location_id: { type: Sequelize.INTEGER, allowNull: false, references: { model: 'storage_location', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'RESTRICT' },
      production_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      accepted_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      packed_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      inward_entry_qty: { type: Sequelize.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      inward_entry_date: { type: Sequelize.DATE, allowNull: false },
      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_pfgi_ctx' });
    await queryInterface.addIndex(table, ['packing_id'], { name: 'idx_pfgi_packing_id' });
    await queryInterface.addIndex(table, ['route_card_id'], { name: 'idx_pfgi_route_card_id' });
    await queryInterface.addIndex(table, ['storage_location_id'], { name: 'idx_pfgi_storage_location_id' });
  },

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