const db = require('../utils/db.js')
const axios = require('axios')
// check status then send message.
// fetch message information.

const attandanceMiddleware = async(req, res, next)=>{
    try {
     const {status, phone} = req.body
      if(status=='0'){
        const sql = `SELECT * FROM sms WHERE id = ? `;
        db.query(sql, ['1'], (err, result)=>{
            if(err) return res.status(400).json({message:err})
            const message = result[0].att_sms
            const apikey = result[0].api_key
            // send message
            const greenwebsms = new URLSearchParams();
            greenwebsms.append('token', apikey);
            greenwebsms.append('to', phone);
            greenwebsms.append('message', message);
            axios.post('https://api.bdbulksms.net/api.php', greenwebsms).then(response => {
              console.log(response.data);
              next()
            });
            // send message.
            
        })
      }else{
        next()
      }  
    } catch (error) {
        res.status(400).json({message:'somthing went wrong!'})
    }

}

module.exports = attandanceMiddleware