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

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

      doc_no: { type: DataTypes.STRING(100), allowNull: false },
      from_date: { type: DataTypes.DATEONLY, allowNull: true },
      to_date: { type: DataTypes.DATEONLY, allowNull: true },
      year_id: { type: DataTypes.INTEGER, allowNull: true },
      doc_status: { type: DataTypes.INTEGER, allowNull: true },

      // fk
      company_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: "companies", key: "id" } },
      territory_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: "territories", key: "id" } },
      period_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: "periods", key: "id" } },
    },
    {
      tableName: "callplans",
      freezeTableName: true,
    }
  );

  /**
   * Associates the CallPlan model with other models.
   * @param {object} models - The models to associate with.
   */
  CallPlan.associate = (models) => {
    /**
     * A CallPlan belongs to a Company.
     * Defines a many-to-one relationship between CallPlan and Company.
     * @param {object} models - The models to associate with.
     */
    CallPlan.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });

    /**
     * A CallPlan belongs to a Territory.
     * Defines a many-to-one relationship between CallPlan and Territory.
     * @param {object} models - The models to associate with.
     */
    CallPlan.belongsTo(models.Territory, { foreignKey: "territory_id", as: "territory" });

    /**
     * A CallPlan belongs to a Period.
     * Defines a many-to-one relationship between CallPlan and Period.
     * @param {object} models - The models to associate with.
     */
    CallPlan.belongsTo(models.Period, { foreignKey: "period_id", as: "period" });

    /**
     * A CallPlan has many CallPlanDestinations.
     * Defines a one-to-many relationship between CallPlan and CallPlanDestination.
     * @param {object} models - The models to associate with.
     */
    CallPlan.hasMany(models.CallPlanDestination, { foreignKey: "callplan_id", as: "callplandestinations" });

    /**
     * A CallPlan has many CallPlanVisits.
     * Defines a one-to-many relationship between CallPlan and CallPlanVisit.
     * @param {object} models - The models to associate with.
     */
    CallPlan.hasMany(models.CallPlanVisit, { foreignKey: "callplan_id", as: "callplanvisits" });

    /**
     * A CallPlan has many CallPlanRealizations.
     * Defines a one-to-many relationship between CallPlan and CallPlanRealization.
     * @param {object} models - The models to associate with.
     */
    CallPlan.hasMany(models.CallPlanRealization, { foreignKey: "callplan_id", as: "callplanrealizations" });

    /**
     * A CallPlan has many UpCountries.
     * Defines a one-to-many relationship between CallPlan and UpCountry.
     * @param {object} models - The models to associate with.
     */
    CallPlan.hasMany(models.UpCountry, { foreignKey: "callplan_id", as: "upcountries" });
  };

  return CallPlan;
};
