
const { res } = require("pino-std-serializers");
const { empty } = require("../Helpers/Common");
const sequelize = require("../config/dbconnection");
const Sequelize = require('sequelize')

const OnboardWorkFlowDocumets = sequelize.define('onboard_workflow_documents', {
    id: { type: Sequelize.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
    document_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 addOnboardworkFlowDocuments=async(data)=>{
    try {
        let result = await OnboardWorkFlowDocumets.create(data)
        if(!empty(result)){
            return result
        }else{
            return false
        }
    } catch (error) {
        console.log(error,'Error ')
    }
}
const getDocumenetsByStageId=async(stageid,status=1)=>{
    try {
        let result = await sequelize.query(`
        select owd.id,owd.document_id from onboard_workflow_documents as owd where owd.stage_id = ${stageid}
        and owd.status =${status} ORDER BY owd.id ASC;
        `)
        if(result){
            const data = result[0].map((v)=>v.document_id)
            return data
        }else{
            return false
        }
    } catch (error) {
        console.log(error)
    }
}
const getallRequestDocuments=async(documenttype=0,ids=0,partnerid=0,teamId=0,rfqstage)=>{
    try {
        let whereCondition=""
        let select=""
        let join=""
        if(!empty(documenttype)){
			
            whereCondition+=" and d.document_type="+documenttype
		}
		if(!empty(partnerid)){
            select=" ,pd.id as partner_document_id,pd.file_name as partner_document_value,pd.issue_date as partner_document_issue_date,pd.expiry_date as partner_document_expiry_date"
            join=` left join partner_documents pd on pd.document_id=d.id and pd.partner_id=${partnerid} and pd.onboarding=1`
            if(rfqstage==1){
                join=` left join rfq_documents pd on pd.document_id=d.id and pd.partner_id=${partnerid} and pd.onboarding=1`
            }
        }
		if(!empty(teamId)){
			select=" and pd.id as partner_document_id,pd.file_name as partner_document_value,pd.issue_date as partner_document_issue_date,pd.expiry_date as partner_document_expiry_date"
			join=` left join team_documents pd on pd.document_id=d.id and pd.team_id=${teamId} and pd.onboarding=1`
		}
        let result = await sequelize.query(` SELECT d.*${select} from documents d`
       +` ${join}`
       +` WHERE d.status = 1  AND d.id IN (${ids}) ${whereCondition} ORDER BY FIELD(d.id, ${ids})`)
if(result){
    const data = result[0].map((row)=>(
        row
    ))
    return data
}else{
    return false
}
    } catch (error) {
        console.log(error)
    }
}

const getDocumentsData=async(stage_id)=>{
    try {
       let result = await sequelize.query(` SELECT *  FROM documents WHERE id IN (SELECT document_id FROM onboard_workflow_documents 
           WHERE stage_id IN (
               SELECT id 
               FROM onboard_workflow_stages 
               WHERE stage_id = ${stage_id}
           )
       )
       `)
       if(result[0].length>0){
        return result[0]
       }else{
         return []
       }
    } catch (error) {
        console.log(error)
    }
}
module.exports={
    addOnboardworkFlowDocuments,
    getDocumenetsByStageId,
    getallRequestDocuments,
    getDocumentsData
}

