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

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

      activity: { type: DataTypes.STRING(100), allowNull: false },
      description: { type: DataTypes.STRING(250), allowNull: true },
      start_date: { type: DataTypes.DATE, allowNull: true },
      end_date: { type: DataTypes.DATE, allowNull: true },
      extend_date: { type: DataTypes.DATE, allowNull: true },
      extend_startdate: { type: DataTypes.DATE, allowNull: true },
      pic: { type: DataTypes.JSON, allowNull: true },
      status: { type: DataTypes.STRING(100), allowNull: true },
      progress: { type: DataTypes.STRING(100), allowNull: true },
      note: { type: DataTypes.STRING(250), allowNull: true },
      score: { type: DataTypes.INTEGER, allowNull: true },

      //fk
      workplanmonitor_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
          model: "workplanmonitors",
          key: "id",
        },
      },
    },
    {
      tableName: "workplanmonitorchilds",
      freezeTableName: true,
    }
  );

  /**
   * Associates the WorkplanMonitorChild model with other models.
   * @param {object} models - The models to associate with.
   * @description
   *   - A WorkplanMonitorChild belongs to a WorkplanMonitor.
   */
  WorkplanMonitorChild.associate = (models) => {
    /**
     * Associates the WorkplanMonitors model with the WorkplanMonitorChild model.
     * A DeliveryTrip has many DeliveryTripChilds.
     * @param {object} models - The models to associate with.
     */
    WorkplanMonitorChild.hasMany(models.WorkplanMonitorActivityChild, {
      foreignKey: "workplanmonitorchild_id",
      as: "workplanmonitoractivitychilds",
      onDelete: 'CASCADE'
    });

    /**
     * Associates the WorkplanMonitorChild model with the WorkplanMonitor model.
     * A WorkplanMonitorChild belongs to a WorkplanMonitor.
     * @param {object} models - The models to associate with.
     */
    WorkplanMonitorChild.belongsTo(models.WorkplanMonitor, {
      foreignKey: "workplanmonitor_id",
      as: "workplanmonitor",
    });
  };

  return WorkplanMonitorChild;
};
