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

module.exports = (sequelize) => {
  const DistributorProduct = sequelize.define(
    "distributorproduct",
    {
      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: true },
      owner: { type: DataTypes.INTEGER, allowNull: true },
      enabled: { type: DataTypes.BOOLEAN, allowNull: false },

      display_name: { type: DataTypes.STRING(100), allowNull: false },
      code: { type: DataTypes.STRING(100), allowNull: false},

      // fk
      item_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "items", key: "id" } },
      distributor_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: "distributors", key: "id" } },
    },
    {
      tableName: "distributorproducts",
      freezeTableName: true,
    }
  );

  /**
   * Associates the DistributorProduct model with other models.
   * @param {object} models - The models to associate with.
   */
  DistributorProduct.associate = (models) => {
    /**
     * Associates the DistributorProduct model with the Item model.
     * A DistributorProduct belongs to an Item.
     * @param {object} models - The models to associate with.
     */
    DistributorProduct.belongsTo(models.Item, { foreignKey: "item_id", as: "item" });

    /**
     * Associates the DistributorProduct model with the Distributor model.
     * A DistributorProduct belongs to a Distributor.
     * @param {object} models - The models to associate with.
     */
    DistributorProduct.belongsTo(models.Distributor, { foreignKey: "distributor_id", as: "distributor" });
  };

  
  return DistributorProduct;
};
