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

module.exports = (sequelize) => {
  const DistributorBranch = sequelize.define(
    "distributorbranch",
    {
      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},
      email_address1: { type: DataTypes.TEXT, allowNull: true },
      email_address2: { type: DataTypes.TEXT, allowNull: true },

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

  /**
   * Associate the DistributorBranch model with other models.
   * @param {object} models - The models to associate with.
   */
  DistributorBranch.associate = (models) => {
    /**
     * Associates the DistributorBranch model with the Distributor model.
     * A DistributorBranch belongs to a Distributor.
     * @param {object} models - The models to associate with.
     */
    DistributorBranch.belongsTo(models.Distributor, { foreignKey: "distributor_id", as: "distributor" });
    DistributorBranch.hasMany(models.Udp, { foreignKey: "distributorbranch_id", as: "udps" });
  };

  
  return DistributorBranch;
};
