module.exports = (sequelize, DataTypes) => {
  const NestingStoresRequest = sequelize.define(
    'NestingStoresRequest',
    {
      id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
      company_id: { type: DataTypes.INTEGER, allowNull: false },
      plant_id: { type: DataTypes.INTEGER, allowNull: false },
      fiscal_year_id: { type: DataTypes.INTEGER, allowNull: true },
      production_order_id: { type: DataTypes.INTEGER, allowNull: false },
      material_request_id: { type: DataTypes.INTEGER, allowNull: true },
      customer_po_id: { type: DataTypes.INTEGER, allowNull: true },
      customer_po_number: { type: DataTypes.STRING(100), allowNull: true },
      customer_name: { type: DataTypes.STRING(255), allowNull: true },
      production_order_number: { type: DataTypes.STRING(50), allowNull: true },
      order_quantity: { type: DataTypes.DECIMAL(18, 3), allowNull: true },
      released_quantity: { type: DataTypes.DECIMAL(18, 3), allowNull: true },
      material_request_number: { type: DataTypes.STRING(50), allowNull: true },
      required_date: { type: DataTypes.DATEONLY, allowNull: true },
      created_by: { type: DataTypes.INTEGER, allowNull: true },
      updated_by: { type: DataTypes.INTEGER, allowNull: true }
    },
    {
      tableName: 'nesting_stores_request',
      underscored: true
    }
  );

  NestingStoresRequest.associate = (models) => {
    NestingStoresRequest.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    NestingStoresRequest.belongsTo(models.MaterialRequest, { foreignKey: 'material_request_id', as: 'materialRequest' });
    NestingStoresRequest.hasMany(models.NestingStoresRequestLine, {
      foreignKey: 'nesting_stores_request_id',
      as: 'lines'
    });
    NestingStoresRequest.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
  };

  return NestingStoresRequest;
};
