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

module.exports = (sequelize) => {
  const CallPlanVisit = sequelize.define(
    "callplanvisit",
    {
      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" } },
      visit_date: { type: DataTypes.DATEONLY, allowNull: false },
    },
    {
      tableName: "callplanvisits",
      freezeTableName: true,
    }
  );

  /**
   * Associates the CallPlanVisit model with other models.
   * @param {object} models - The models to associate with.
   * @description
   *   - A CallPlanVisit belongs to one CallPlan.
   *   - A CallPlanVisit belongs to one Client.
   *   - A CallPlanVisit belongs to one Outlet.
   */
  CallPlanVisit.associate = (models) => {
    /**
     * Associates the CallPlanVisit model with the CallPlan model.
     * A CallPlanVisit belongs to one CallPlan.
     * @param {object} models - The models to associate with.
     */
    CallPlanVisit.belongsTo(models.CallPlan, { foreignKey: "callplan_id", as: "callplan", onDelete: "CASCADE" });

    /**
     * Associates the CallPlanVisit model with the Client model.
     * A CallPlanVisit belongs to one Client.
     * @param {object} models - The models to associate with.
     */
    CallPlanVisit.belongsTo(models.Client, { foreignKey: "client_id", as: "client", onDelete: "CASCADE" });

    /**
     * Associates the CallPlanVisit model with the Outlet model.
     * A CallPlanVisit belongs to one Outlet.
     * @param {object} models - The models to associate with.
     */
    CallPlanVisit.belongsTo(models.Outlet, { foreignKey: "outlet_id",  as: "outlet", onDelete: "CASCADE" });
  };
    
  return CallPlanVisit;
};
