let bcrypt = require('bcryptjs')
let multer = require('multer')
let express = require('express')
let router = express.Router()
var Recaptcha = require('express-recaptcha').RecaptchaV3;
var recaptcha = new Recaptcha('6LdlbrwZAAAAAKvtcVQhVl_QaNOqmQ4PgyW3SKHy', '6LdlbrwZAAAAAMAWPVDrL8eNPxrws6AMDtLf1bgd');
var reCAPTCHASecret = "6LdlbrwZAAAAAKvtcVQhVl_QaNOqmQ4PgyW3SKHy";
const Errors = require('../lib/errors.js')
var format = require('date-format');
let {
    User, Post, ProfilePicture, StaffApplications, AdminToken, PassKey, Thread, Category, Sequelize, Ip, Ban, sequelize
} = require('../models')
let pagination = require('../lib/pagination.js')

router.all('*', (req, res, next) => {
    if(req.session.username) {
        next()
    } else {
        res.status(401)
        res.json({
            errors: [Errors.requestNotAuthorized]
        })
    }
})

router.put('/scrub', async (req, res, next) => {
    try {
        if(!req.session.admin) {
            throw Errors.requestNotAuthorized
        }
        await Ban.ReadOnlyMode(req.session.username)

        if(req.autosan.body.description === "descscram") {
            let user = await User.update({ description:  "Scrambled by Admin: " + Math.random().toString(36).substring(2)}, { where: {
                    username: req.autosan.body.user
                }})

            res.json({ success: true })

        } else if(req.autosan.body.username === "usernamescram") {
            let user = await User.update({username: Math.random().toString(36).substring(2)}, {
                where: {
                    username: req.autosan.body.user
                }
            })
            res.json({success: true})
        } else {
            res.json({ success: false })
        }
    } catch (e) { next(e) }
})

module.exports = router;
