const sequelize = require("../config/dbconnection");
const { empty, arrayDiffAssoc } = require('../Helpers/Common');
const moment = require('moment')
const Admin = require("../models/admin")
const Onboard = require("../models/onboard")
const Dashboard=require("../models/dashboard")
const dashboard = async (req,reply) => {
    try{
        let { userId, userType } = req.body

        let userData = await Admin.getUserById(userId);
        let unsignedDocuments = 0
        let requestCounts = 0
        let userSkills=null
        if (userType == 3) {
            let partnerData = await Admin.getPartnerByUserId(userId)
            let stages = []
            let onboardCompleted = 0;
            console.log("$$$$$$$$$$",partnerData['workflows']);
            if (!empty(partnerData['workflows'])) {
                stages = await Admin.getAllWorkflowStages(partnerData['workflows'], "partner_stages ps", "ps.stage_id=ows.id and ps.partner_id=" + partnerData['id'], "coalesce(ps.status,0) as stage_status,coalesce(ps.stage_submit,0) as stage_submit");
            console.log("nushhhhhhhhhhhhhhhhh",stages);
            }
           
            let workflows = await Onboard.getOnboardingWorkflows(partnerData['id'], partnerData['workflows']);
            if (empty(partnerData['workflows']) || empty(workflows)) {
              onboardCompleted = 1;
            } else {
                onboardCompleted = 0;
            }
             unsignedDocuments = await Dashboard.getUnsignedDocumentsCount(partnerData['id']);
           requestCounts = await Dashboard.getNewrequestCount(partnerData['id']);
           userSkills = await Dashboard.getSkillById('partner_services', 'partner_id', partnerData['id']);
           reply.send({status:1,partnerData,stages,workflows,onboardCompleted,unsignedDocuments,requestCounts,userSkills})
        }
        else if(userType!=1 && userType!=24){
            let teamData=await Admin.getTeamByUserId(userId);
            console.log(teamData,'5555555555555555555')
            let stages = []
            let onboardCompleted = 0;
            if (!empty(teamData[0]['workflows'])) {
                stages = await Admin.getAllWorkflowStages(teamData[0]['workflows'], "team_stages ps", "ps.stage_id=ows.id and ps.team_id=" + teamData[0]['id'], "coalesce(ps.status,0) as stage_status,coalesce(ps.stage_submit,0) as stage_submit");
            }
            console.log(teamData,'5555555555555555555')
            console.log(teamData[0]['id'], teamData[0]['workflows'],'LLLLLLLLLLLLLLLLLLL')
            requestCounts = await Dashboard.getNewrequestCount(false);
            let workflows = await Onboard.getOnboardingWorkflows(teamData[0]['id'], teamData[0]['workflows'],1);
            if (empty(teamData[0]['workflows']) || empty(workflows)) {
              onboardCompleted = 1;
            } else {
                onboardCompleted = 0;
            }
           console.log(stages,'==========================stages')
           userSkills = await Dashboard.getSkillById('team_services', 'team_id', teamData[0]['id']);
           reply.send({status:1,stages,workflows,onboardCompleted,userSkills, requestCounts})
        }
        else if(userType == 1 || userType == 8){
            requestCounts = await Dashboard.getNewrequestCount(false);
            reply.send({status:1, requestCounts})
        }
    }
    catch(error){
        reply.status(500).send({ message: "Server error" +error});
    }

}

module.exports={dashboard}