import { Router } from 'express';
import { asyncHandler } from '../../middleware/asyncHandler';
import { authenticate } from '../../middleware/authenticate';
import * as ctrl from './auth.controller';

export const authRouter = Router();

authRouter.get('/signup-status', asyncHandler(ctrl.signupStatus)); // is bootstrap signup available?
authRouter.post('/signup', asyncHandler(ctrl.signup)); // bootstrap first SUPER_ADMIN
authRouter.post('/login', asyncHandler(ctrl.login));
// Second step of a two-step sign-in. Unauthenticated: the challenge id —
// handed out only after the password was checked — is the credential.
authRouter.post('/login/verify', asyncHandler(ctrl.verifyLogin));
authRouter.post('/login/resend', asyncHandler(ctrl.resendLogin));
authRouter.post('/login/verify-totp', asyncHandler(ctrl.verifyLoginTotp));
authRouter.post('/login/email-code', asyncHandler(ctrl.loginEmailCode));
authRouter.post('/refresh', asyncHandler(ctrl.refresh));
authRouter.post('/logout', asyncHandler(ctrl.logout));
authRouter.get('/me', authenticate, asyncHandler(ctrl.me));
// Own-profile management — every role, not just SUPER_ADMIN.
authRouter.patch('/me', authenticate, asyncHandler(ctrl.updateProfile));
authRouter.post('/change-password', authenticate, asyncHandler(ctrl.changePassword));
// Two-step verification, each user for their own account.
authRouter.post('/2fa/enable', authenticate, asyncHandler(ctrl.twoFactorEnableStart));
authRouter.post('/2fa/enable/verify', authenticate, asyncHandler(ctrl.twoFactorEnableConfirm));
authRouter.post('/2fa/disable', authenticate, asyncHandler(ctrl.twoFactorDisableStart));
authRouter.post('/2fa/disable/verify', authenticate, asyncHandler(ctrl.twoFactorDisableConfirm));
authRouter.post('/2fa/resend', authenticate, asyncHandler(ctrl.twoFactorResend));
// Authenticator app (TOTP).
authRouter.get('/2fa/totp', authenticate, asyncHandler(ctrl.totpStatus));
authRouter.post('/2fa/totp/setup', authenticate, asyncHandler(ctrl.totpSetup));
authRouter.post('/2fa/totp/enable', authenticate, asyncHandler(ctrl.totpEnable));
authRouter.post('/2fa/totp/disable', authenticate, asyncHandler(ctrl.totpDisable));
authRouter.post('/2fa/totp/recovery-codes', authenticate, asyncHandler(ctrl.totpRecoveryCodes));
