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

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

      is_saved: {type: DataTypes.TINYINT, allowNull: true},
      is_submitted: {type: DataTypes.TINYINT, allowNull: true},
      status: {type: DataTypes.INTEGER, allowNull: true},

      survey_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: 'surveys', key: 'id' } },
      outlet_id: { type: DataTypes.INTEGER, allowNull: false, references: { model: 'outlets', key: 'id' } },
      treatment_details: { type: DataTypes.JSON, allowNull: true },
    },
    {
      tableName: "surveytreatments",
      freezeTableName: true,
    }
  );

  SurveyTreatment.associate = (models) => {
    SurveyTreatment.belongsTo(models.Survey, { foreignKey: "survey_id", as: "survey", onDelete: 'CASCADE' });
    SurveyTreatment.belongsTo(models.Outlet, { foreignKey: "outlet_id", as: "outlet" });
  }

  return SurveyTreatment;
};
