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


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

    let dbUser = await request.getUser();

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

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