module.exports = (sequelize, DataTypes) => {
  const ProductionNestingConfirmationComponent = sequelize.define(
    'ProductionNestingConfirmationComponent',
    {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      company_id: {
        type: DataTypes.INTEGER,
        allowNull: false
      },
      production_order_id: {
        type: DataTypes.INTEGER,
        allowNull: false
      },
      confirmation_id: {
        type: DataTypes.INTEGER,
        allowNull: false
      },
      production_order_component_id: {
        type: DataTypes.INTEGER,
        allowNull: false
      },
      material_id: {
        type: DataTypes.INTEGER,
        allowNull: false
      }
    },
    {
      tableName: 'production_nesting_confirmation_component',
      indexes: [
        { fields: ['confirmation_id'] },
        { fields: ['production_order_id'] },
        { unique: true, fields: ['confirmation_id', 'production_order_component_id'] }
      ]
    }
  );

  ProductionNestingConfirmationComponent.associate = function(models) {
    ProductionNestingConfirmationComponent.belongsTo(models.ProductionNestingConfirmation, {
      foreignKey: 'confirmation_id',
      as: 'confirmation'
    });
    ProductionNestingConfirmationComponent.belongsTo(models.ProductionOrderComponent, {
      foreignKey: 'production_order_component_id',
      as: 'productionOrderComponent'
    });
    ProductionNestingConfirmationComponent.belongsTo(models.Material, {
      foreignKey: 'material_id',
      as: 'material'
    });
  };

  return ProductionNestingConfirmationComponent;
};

