const router = require("express").Router();
const fs = require('fs');
const userAg = require('useragent');
const logger = require('../helpers/logger');
const dbClickLogger = require("../helpers/dbClickLogger");
const dbClickDisplay = require("../helpers/dbClickDisplay");

logger.info('Index Loaded');


router.get("/", async (req, res, next) => {
    try {
        let data = {
            title: 'Emergency Locksmith Services - 24hrs Locksmith',
            query: req.query,
            url: req.url,
            location: "",
            userAgent: req.useragent,
        }
        // if (isVPN) {
        //   console.log('VPN detected');
        //   // Take appropriate action for VPN traffic
        // } else {
        //   console.log('No VPN detected');
        //   // Process non-VPN traffic
        // }
        res.render("./indexView", data);

    } catch (err) {
        // Pass the error to the error middleware
        next(err);
    }

});

router.post("/save-click", async (req, res) => {
    dbClickLogger(req);
});


router.get("/site-conv-clicks", async (req, res) => {
    const clickLogs = await dbClickDisplay(req);
    // console.log(clickLogs);
    let data = {
        title: 'SiteWide Conversion Clicks',
        url: req.url,
        clickLogs: clickLogs,
        location: ""
    }
    res.render("pages/convClicksView", data);
});

if (process.env.ONE_PAGE_ONLY === 'false') {

    router.get("/dublin", async (req, res) => {
        let data = {
            title: 'Dublin Locksmith',
            url: req.url,
            location: ""
        }
        res.render("pages/dublinView", data);
    });

    router.get("/emergency-locksmith", async (req, res) => {
        let data = {
            title: 'Emergency Locksmith',
            url: req.url,
            location: ""
        }
        res.render("pages/emergencyView", data);
    });

    router.get("/locksmith-near-me", async (req, res) => {
        let data = {
            title: 'Locksmith Near Me',
            url: req.url,
            location: ""
        }
        res.render("pages/nearMeView", data);
    });

    router.get("/london-locksmith", async (req, res) => {
        let data = {
            title: 'London Locksmith',
            url: req.url,
            location: ""
        }
        res.render("pages/londonView", data);
    });

    router.get("/upvc-door-repair", async (req, res) => {
        let data = {
            title: 'UPVC Door Repair Services',
            url: req.url,
            location: ""
        }
        res.render("pages/upvcRepairsView", data);
    });

    router.get("/24-7-locksmith", async (req, res) => {
        let data = {
            title: '24/7 Locksmith, Emergency Ready',
            url: req.url,
            location: ""
        }
        res.render("pages/24hrsView", data);
    });

    router.get("/lockout", async (req, res) => {
        let data = {
            title: 'Lockout 24/7 Help, Emergency Locksmith',
            url: req.url,
            location: ""
        }
        res.render("pages/lockoutView", data);
    });

    router.get("/services", async (req, res) => {
        let data = {
            title: 'Services - 24hrs Locksmith',
            url: req.url,
            location: ""
        }
        res.render("pages/servicesView", data);
    });


    // router.get("/blog", async(req, res) =>{
    //     let data = {
    //         title: 'Blog',
    //         url: req.url,
    //         location: ""
    //     }
    //     res.render("pages/blogView", data); 
    // });

    // router.get("/blog/article", async(req, res) =>{
    //     let data = {
    //         title: 'Blog',
    //         url: req.url,
    //         location: ""
    //     }
    //     res.render("pages/blogArticleView", data); 
    // });

    router.get("/about", async (req, res) => {
        let data = {
            title: 'About us',
            url: req.url,
            location: ""
        }
        res.render("pages/aboutView", data);
    });

    router.get("/contact", async (req, res) => {
        let data = {
            title: 'Contact us',
            url: req.url,
            location: ""
        }
        res.render("pages/contactView", data);
    });

    router.get("/join-our-team", async (req, res) => {
        let data = {
            title: 'Be Part of Our Team',
            url: req.url,
            location: ""
        }
        res.render("pages/joinOurTeamView", data);
    });

    router.get("/prices", async (req, res) => {
        let data = {
            title: 'Pricing',
            url: req.url,
            location: ""
        }
        res.render("pages/pricingView", data);
    });

    router.get("/areas-covered", async (req, res) => {
        let data = {
            title: 'Areas Covered',
            url: req.url,
            location: ""
        }
        res.render("pages/areasCoveredView", data);
    });

}

module.exports = router;