import { generateSecret, generateURI, verify } from 'otplib';
import qrcode from 'qrcode';
export default class MfaController {
    async setup({ auth, response }) {
        try {
            const user = auth.user;
            if (user.isTwoFactorEnabled) {
                return response.badRequest({ message: '2FA is already enabled on this account.' });
            }
            const secret = generateSecret();
            user.twoFactorSecret = secret;
            await user.save();
            const otpauthUrl = generateURI({
                secret,
                label: user.email,
                issuer: 'Moneylot Plus',
            });
            const qrCodeDataUrl = await qrcode.toDataURL(otpauthUrl);
            return response.ok({
                secret,
                qrCode: qrCodeDataUrl,
            });
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'An unknown error occurred';
            return response.internalServerError({
                message: 'Could not generate 2FA configuration.',
                error: errorMessage,
            });
        }
    }
    async verifyAndEnable({ auth, request, response }) {
        try {
            const user = auth.user;
            const { code } = request.only(['code']);
            if (!code) {
                return response.badRequest({ message: 'The 6-digit verification code is required.' });
            }
            if (!user.twoFactorSecret) {
                return response.badRequest({ message: '2FA setup has not been initiated yet.' });
            }
            const result = verify({
                token: code,
                secret: user.twoFactorSecret,
            });
            if (!result) {
                return response.badRequest({ message: 'Invalid verification code. Please try again.' });
            }
            user.isTwoFactorEnabled = true;
            await user.save();
            return response.ok({
                message: 'Two-Factor Authentication enabled successfully!',
            });
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'An unknown error occurred';
            return response.internalServerError({
                message: 'Verification failed due to a server error.',
                error: errorMessage,
            });
        }
    }
}
//# sourceMappingURL=mfas_controller.js.map