const {queryDb} = require("./db");
const logger = require('./logger');
const getURLParameters = require('./urlParameters');
const moment = require('moment');

module.exports = async function dbClickDisplay(req, isVpn) {

    const urlParams = req.url.split('?')[1];

    const today = moment().format('YYYY-M-D');
    let whereClause = `WHERE dateOnly = '` + today + `'`;

    let clickLogsQuery = `SELECT * FROM ClickLogs ORDER BY id DESC LIMIT 500`;

    // let whereClause = 'WHERE dateOnly = 2023-08-25';
    // let whereClause = '';

    if(typeof urlParams !== 'undefined' && urlParams){
        const parameters = await getURLParameters('https://24hrslocksmith.co.uk' + req.url);
        console.log('parameters: ', parameters);
        dateParam = parameters.find(param => param.name === 'date').value;
        // campaignParam = parameters.find(param => param.name === 'campaign').value;
        // console.log('campaign: ', campaignParam);
        whereClause = `WHERE dateOnly = '` + dateParam + `'`;
        clickLogsQuery = `SELECT * FROM ClickLogs ` + whereClause + ` ORDER BY id DESC LIMIT 500`;
    } else {

    }

    try {
        const clickLogs = await queryDb(clickLogsQuery);
        // console.log(clickLogs);
        return clickLogs;
    } catch (error) {
        // logger.error('Error querying data:', error);
        console.error('Error querying data:', error);
    }
};