const sequelize = require("../config/dbconnection");
const Admin = require ('../models/admin');
const CallModel = require('../models/crmCallModels');
const { portalName } = require("../constants/constant");
const moment = require("moment");
let currentDate = Math.round(moment().valueOf() / 1000);

const getAllCalls = async (request,reply) => {
    try{
        const {start,end,from,to,userType} = request.body;
        if(start || end || from || to){
            let filterCriteria = {};
            if(start){
                // let startTimestamp  = Math.round(moment(start).valueOf() / 1000);
                let startTimestamp = moment(start).unix();
                filterCriteria['startTimeAfter'] = moment.unix(startTimestamp ).format('YYYY-MM-DD');
                // filterCriteria['startTimeAfter'] = new Date(start);
            }
            if(end){
                // let endTimestamp  = Math.round(moment(end).valueOf() / 1000);
                let endTimestamp  = moment(end).unix();
                filterCriteria['startTimeBefore'] = moment.unix(endTimestamp ).format('YYYY-MM-DD');
                // filterCriteria['startTimeBefore'] = new Date(end);
            }
            if(from){
                filterCriteria['from']=from;
            }
            if(to){
                filterCriteria['to']=to;
            }
            const twilioCalls = await Admin.fetchTwilioCallsByFiterCriteria(filterCriteria);
            return twilioCalls?.length > 0 ? twilioCalls : [];
        }else{
            let data={};
            // data["calls"]= await CallModel.getAllCalls(userType);
            data["twilio_calls"]= await Admin.fetchCompleteTwilioCalls();
            data["twilio_calls"] = data["twilio_calls"]?.length > 0 ? data["twilio_calls"] : [];
            return data
        }
    }
    catch(error){
        console.log(error)
        return []
    }
}

const getAllSms = async (request,reply) => {
    try{
        const {start,end,from,to,userType} = request.body;
        if(start || end || from || to){
            let filterCriteria = {};
            if(start){
                let startTimestamp = moment(start).unix();
                filterCriteria['dateSentAfter'] = moment.unix(startTimestamp ).format('YYYY-MM-DD');
            }
            if(end){
                let endTimestamp  = moment(end).unix();
                filterCriteria['dateSentBefore'] = moment.unix(endTimestamp ).format('YYYY-MM-DD');
            }
            if(from){
                filterCriteria['from']=from;
            }
            if(to){
                filterCriteria['to']=to;
            }
            let twilioMessages = await Admin.fetchTwilioMessagesByFilter(filterCriteria);
            return twilioMessages?.length > 0 ? twilioMessages : [];
        }else{
            let twilioMessages = await Admin.fetchCompleteTwilioMessages();
            return twilioMessages?.length > 0 ? twilioMessages : [];
        }
    }
    catch(error){
        console.log("getAllSms error",error)
        return [];
    }
}

module.exports = {getAllCalls,getAllSms}