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

const OnboardWorkFlowFields = sequelize.define('onboard_workflow_fields', {
    id: { type: Sequelize.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
    field_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 addOnboardworkFlowFields=async(data)=>{
    try {
        let result = await OnboardWorkFlowFields.create(data)
        if(!empty(result)){
            return result
        }else{
            return false
        }
    } catch (error) {
        console.log(error,'Error')
    }
}
const getfieldTemplatesbyStageId=async(stageid,status=1)=>{
    try {
        // console.log(stageid,status,'MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM')
      let result = await sequelize.query(`
      SELECT owf.id, owf.field_template_id
  FROM onboard_workflow_fields owf
  LEFT JOIN custom_field_templates cf ON owf.field_template_id = cf.id
  WHERE owf.status = ${status}
    AND owf.stage_id = ${stageid}
    AND cf.status = 1
  ORDER BY owf.id ASC;
  `)
 
  if (result && result[0].length > 0) {
    const data = result[0].map(row => row['field_template_id']);
    // console.log(data,'______________________________')
    return data;
  } else {
    return false;
  }
    } catch (error) {
      console.log(error)
    }
  }
  const getfieldTemplatesbyStageIdView=async(stageid,status=1)=>{
    try {
        console.log(stageid,status,'MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM')
      let result = await sequelize.query(`
      SELECT owf.id, owf.field_template_id
  FROM onboard_workflow_fields owf
  LEFT JOIN custom_field_templates cf ON owf.field_template_id = cf.id
  WHERE owf.status = ${status}
    AND owf.stage_id = ${stageid}
   
  ORDER BY owf.id ASC;
  `)
 
  if (result && result[0].length > 0) {
    const data = result[0].map(row => row['field_template_id']);
    console.log(data,'______________________________')
    return data;
  } else {
    return false;
  }
    } catch (error) {
      console.log(error)
    }
  }
module.exports={
    addOnboardworkFlowFields,
    getfieldTemplatesbyStageId,
    getfieldTemplatesbyStageIdView
}



