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

module.exports = (sequelize) => {
  const AddressChild = sequelize.define(
    "addresschild",
    {
      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 },

      type: { type: DataTypes.STRING(100), allowNull: false },
      address: { type: DataTypes.STRING(250), allowNull: true },

      //fk
      customer_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "customerlists",
          key: "id",
        },
      },

    },
    {
      tableName: "addresschilds",
      freezeTableName: true,
    }
  );

  /**
   * Associates the AddressChild model with other models.
   * @param {object} models - The models to associate with.
   * @description
   *   - A AddressChild belongs to a CustomerList.
   */
  AddressChild.associate = (models) => {
    /**
     * Associates the AddressChild model with the CustomerList model.
     * A AddressChild belongs to a CustomerList.
     * @param {object} models - The models to associate with.
     */
   AddressChild.belongsTo(models.CustomerList, {
      foreignKey: "customer_id",
      as: "customerlist",
    });
  };

  return AddressChild;
};
