text: const Router = require('express').Router()
  const Users = require('../SCHEMA/user')
  const authorization = require('../Authorization/authorization')
  const randomId = require('random-id')
  const authoriazation = require('../Authorization/authorization')
  const { find } = require('../SCHEMA/user')
  const notification = require('../SCHEMA/notification')
  const Talk = require('../SCHEMA/talk')
  const JoinTalk = require('../SCHEMA/jointalk')
  const { htmlToText } = require('html-to-text');
  
  
  //GET ALL TALKS BY USER
  Router.get('/',authoriazation,async(req,res)=>{
      try {
          const fetchAll = await JoinTalk.find({user:req.user})
          if(fetchAll){
             res.json({talks:fetchAll})
          }
      } catch (error) {
         console.log(error.message)
      }
  })
  
  //GET ALL Chat 
  Router.get('/:address',async(req,res)=>{
      try {
         const fetchtalk = await Talk.find({address:req.params.address})
         if(fetchtalk){
             res.json({result:fetchtalk})
         } 
      } catch (error) {
         console.log(error.message)
      }
  })
  //SAVE JOINTAlk
  Router.post('/jointalk/user',authoriazation,async(req,res)=>{
      const {feedid} = req.body
      const checkIfUserIntalk = await JoinTalk.findOne({user:req.user,address:feedid})
      if(checkIfUserIntalk){
          res.json({active:'active'})
      }else{
              const savetalk = await new JoinTalk({
              address:feedid,
              user:req.user
          }) 
          const newsave = await savetalk.save()
          if(newsave){
              res.json({success:'success'})
          }
      }
     
  })
  Router.post('/insertChat',authorization,async(req,res)=>{
      try {
          const {txt,address} = req.body
  
          const text = htmlToText(txt, {
              wordwrap: 130
          });
          if(txt){
              const sendMsg = await new Talk({
                  address: address,
                  user: req.user,
                  txt: text
              })
              const newSendMsg = await sendMsg.save()
              if (newSendMsg) {
                  res.json({ msg: 1 })
              } else {
                  res.json({ error: 0 })
              }
          }
      } catch (error) {
          console.log(error.message)
      }
  })
  Router.get('/Talks/Count/:address', async (req, res) => {
      try {
          const getCountForTalk = await JoinTalk.find({address:req.params.address}).countDocuments()
     
         res.json({count:getCountForTalk}) 
  
      
     } catch (error) {
         console.log(error.message)
     }
  })
  //DELETE user saveTalk
  Router.get('/delete/:address',authoriazation,async(req,res)=>{
      const deletetalk = await JoinTalk.deleteOne({user:req.user,address:req.params.address})
      if(deletetalk){
          res.json({deleted:'success'})
      }
  })
  module.exports = Router