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

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

      callplan_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: 'callplans', key: 'id' } },
      client_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: 'clients', key: 'id' } },
      outlet_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: 'outlets', key: 'id' } },

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

  /**
   * Associates the CallPlanClient model with other models.
   * @param {object} models - The models to associate with.
   */
  CallPlanDestination.associate = (models) => {
    /**
     * A CallPlanDestination belongs to a CallPlan.
     * Defines a many-to-one relationship between CallPlanDestination and CallPlan.
     * @param {object} models - The models to associate with.
     */
    CallPlanDestination.belongsTo(models.CallPlan, { foreignKey: 'callplan_id', as: 'callplan', onDelete: 'CASCADE' });

    
    /**
     * A CallPlanDestination belongs to a Client.
     * Defines a many-to-one relationship between CallPlanDestination and Client.
     * @param {object} models - The models to associate with.
     */
    CallPlanDestination.belongsTo(models.Client, { foreignKey: 'client_id', as: 'client' });

    /**
     * A CallPlanDestination belongs to an Outlet.
     * Defines a many-to-one relationship between CallPlanDestination and Outlet.
     * @param {object} models - The models to associate with.
     */
    CallPlanDestination.belongsTo(models.Outlet, { foreignKey: 'outlet_id', as: 'outlet' });
  }


  return CallPlanDestination;
};
