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

module.exports = (sequelize) => {
  const SurveyExpense = sequelize.define(
    "surveyexpense",
    {
      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' } },
      expense_details: { type: DataTypes.JSON, allowNull: true },
    },
    {
      tableName: "surveyexpenses",
      freezeTableName: true,
    }
  );

  SurveyExpense.associate = (models) => {
    SurveyExpense.belongsTo(models.Survey, { foreignKey: "survey_id", as: "survey", onDelete: 'CASCADE' });
    SurveyExpense.hasMany(models.AccountEntries, { foreignKey: "surveyexpense_id", as: "accountentries", onDelete: 'CASCADE' });
  }


  return SurveyExpense;
};
