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

module.exports = (sequelize) => {
  const SurveySellingIn = sequelize.define(
    "surveysellingin",
    {
      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: true, references: { model: "surveys", key: "id" } },
      outlet_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "outlets", key: "id" } },
      company_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: "companies", key: "id" } },
      sellingin_details: { type: DataTypes.JSON, allowNull: true },
    },
    {
      tableName: "surveysellingins",
      freezeTableName: true,
    }
  );

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