const { empty } = require("../Helpers/Common")
const ContactTags = require('../models/contactTag')
const moment = require('moment')

let currentDate = Math.round(moment().valueOf() / 1000);
const addContactTags =async(req,res)=>{
    try {
        let {values,userId,userType}=req.body
        if(!empty(values)){
           let data={}
           data['name'] = values.name 
           data['color'] = values.color 
           data['variant'] = values.variant 
           data['status'] = 1
           data['added_by'] = userId
           data['timestamp'] = currentDate
           let addedRes = await ContactTags.addContactTags(data)

           if(addedRes){
            res.send({
                status:1,message:`Contact Tag "${data['name']}" added successfully`
            })
           }else{
            res.send({
                status:0,message:`Failed to add the Contact Tag`
            })
           }
        }
    } catch (error) {
        console.log(error)
    }
}
const getAllContactTags=async(req,res)=>{
    let {search,page,rowsPerPage} = req.body

    let result = await ContactTags.getAllTags(search,page,rowsPerPage)
    if(result){
        res.send({
            status:1,data:result
        })
    }else{
        res.send({
            status:0,
        })
    }
}

const updateTagsByID=async(req,res)=>{
    try {
        let {id,values} = req.body
        let resData = await ContactTags.getTagByid(id)
        if(!empty(values)){
            let data={}
            data['name'] =values.name
            data['color'] = values.color 
            data['variant'] = values.variant 
            let result =await ContactTags.updateTags(data,id)
            if(result===true){
                res.send({
                    status:1,message:`${resData[0].name} tag updated successfully`
                })
            }else{
                res.send({
                    status:0,message:`Failed to update tag `
                })
            }
        }
    } catch (error) {
        console.log(error)
    }
}
const getTagByID=async(req,res)=>{
    try {
        let {id} = req.body  
        let result = await ContactTags.getTagByid(id)
        
        if(result.length>0){
            res.send({
                status:1,data:result
            })
        }else{
            res.send({
                status:0
            })
        }
    } catch (error) {
        console.log(error)
    }
}
const deleteTag=async(req,res)=>{
    try {
        let {id} = req.body
        let data={}
        let resData = await ContactTags.getTagByid(id)
       
        data['status'] = 2
        let result =await ContactTags.updateTags(data,id)
        if(result===true){
            res.send({
                status:1,message:`${resData[0].name} tag deleted successfully`
            })
        }else{
            res.send({
                status:0,message:`Failed to delete tag `
            })
        }
    } catch (error) {
        console.log(error)
    }
}
module.exports={
    addContactTags,
    getAllContactTags,
    getTagByID,
    deleteTag,
    updateTagsByID,
}