const { empty } = require("../Helpers/Common");
const sequelize = require("../config/dbconnection");
const Sequelize = require('sequelize')

const OnboardWorkFlowQuestionaires = sequelize.define('onboard_workflow_questionnaires', {
    id: { type: Sequelize.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
    qns_template_id:{type:Sequelize.INTEGER,allowNull:false,defaultValue:false},
    stage_id:{type:Sequelize.INTEGER,allowNull:false,defaultValue:false},
    added_by:{ type: Sequelize.INTEGER, allowNull: false,defaultValue:false  },
    timestamp: { type: Sequelize.INTEGER, allowNull: false },
    updated_on: { type: Sequelize.INTEGER, allowNull: false },
    status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1 },
},{timestamps:false})

const addOnboardQuestions=async(data)=>{
    try {
        let result = await OnboardWorkFlowQuestionaires.create(data)
        if(!empty(result)){
            return result
        }else{
            return false
        }
    } catch (error) {
        console.log(error,'Error in adding questions')
    }
}
const getQuestionairebyStageId=async(stageid,status=1)=>{
    try {
       
        let result = await sequelize.query(`
        SELECT owq.id, owq.qns_template_id
        FROM onboard_workflow_questionnaires owq
        LEFT JOIN questionnaire_templates cf ON owq.qns_template_id = cf.id
        WHERE owq.status = ${status}
          AND owq.stage_id = ${stageid}
          AND cf.status = 1
        ORDER BY owq.id ASC;
        `)
        if(result){
          
            const data = result[0].map((v)=>v.qns_template_id)
            return data
        }else{
            return false
        }
    } catch (error) {
        console.log(error)
    }
}

module.exports={
    addOnboardQuestions,
    getQuestionairebyStageId,
}