const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
  const ItemCompetitor = sequelize.define(
    "ItemCompetitor",
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      creation: { type: DataTypes.DATE, allowNull: false },
      modified: { type: DataTypes.DATE, allowNull: false },
      modified_by: { type: DataTypes.INTEGER, allowNull: false },
      owner: { type: DataTypes.INTEGER, allowNull: false },
      enabled: { type: DataTypes.BOOLEAN, allowNull: false },
      display_name: { type: DataTypes.STRING(255), allowNull: true },
      item_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "items",
          key: "id",
        },
      },
    },
    {
      tableName: "itemcompetitors",
    },
    {
      freezeTableName: true,
    }
  );

  /**
   * Associates the ItemCompetitor model with other models.
   * @param {object} models - The models to associate with.
   */
  ItemCompetitor.associate = (models) => {
    /**
     * A ItemCompetitor belongs to a Item.
     * Defines a many-to-one relationship between ItemCompetitor and Item.
     * The foreign key is item_id and the association is labeled as "item".
     * The onDelete option is set to "CASCADE" so that when an Item is deleted, all its associated ItemCompetitors are also deleted.
     */
    ItemCompetitor.belongsTo(models.Item, { foreignKey: "item_id", as: "item", onDelete: "CASCADE" });
  };


  return ItemCompetitor;
};
