
import {
    getUser,
    getUserMFA
} from "../../lib/mysql.mjs";

import {
    generateOTPQRCode
} from "../../lib/otp.mjs";


export const get = async function(request, response) {
    if (!request.isLoginCompleted()) {
        response.redirect('/login');
        return;
    }

    let userData = await getUser(request.session.userid);
    let mfaData = await getUserMFA(request.session.userid);

    switch (request.params.page) {
        case 'personal':
            response.render(`ui/profile.njk`, {
                page: 'profile/personal',
                data: {
                    firstName: userData.givenname,
                    lastName: userData.sn,
                    mail: userData.mail,
                }
            });
            break;
        case 'security':
            response.render(`ui/profile.njk`, {
                page: 'profile/security',
                otp: {
                    active: mfaData.otpsecret != '' ? true : false,
                    qrcode: await generateOTPQRCode(userData.mail, mfaData.otpsecret)
                }
            });
            break;
        case 'createOTPSecret':
            response.render(`ui/profile.njk`, {
                page: 'profile/createOTPSecret',
                otp: {
                    active: mfaData.otpsecret != '' ? true : false,
                    qrcode: await generateOTPQRCode(userData.mail, mfaData.otpsecret)
                }
            });
            break;
        default:
            response.redirect('/page/personal');
            break;
    }
}

export const post = async function(request, response) {
    console.log(request.body);
    response.redirect("/login");
}
