const router = require('express').Router();
const ctrl = require('../controllers/user.controller');
const { authenticate, authorize } = require('../middlewares/auth');
const { uploadAvatar } = require('../controllers/auth.controller');
const { validate } = require('../middlewares/validate');
const { userSchemas, paramsSchemas } = require('../validators');

router.use(authenticate);

// Self-profile routes (any authenticated user)
router.put(
	'/profile/me',
	uploadAvatar.single('avatar'),
	validate({ body: userSchemas.updateProfile }),
	ctrl.updateProfile
);
router.patch(
	'/profile/password',
	validate({ body: userSchemas.changePassword }),
	ctrl.changeOwnPassword
);
router.patch(
	'/profile/pin',
	validate({ body: userSchemas.updatePin }),
	ctrl.updateOwnPin
);

router.get('/', ctrl.getAll);
router.get('/:id', ctrl.getById);
router.put(
	'/:id',
	authorize('admin', 'manager'),
	uploadAvatar.single('avatar'),
	validate({ params: paramsSchemas.idParam, body: userSchemas.updateUser }),
	ctrl.update
);
router.patch(
	'/:id/password',
	authorize('admin', 'manager'),
	validate({ params: paramsSchemas.idParam, body: userSchemas.resetPassword }),
	ctrl.resetPassword
);
router.patch(
	'/:id/pin',
	authorize('admin', 'manager'),
	validate({ params: paramsSchemas.idParam, body: userSchemas.updatePin }),
	ctrl.updatePin
);
router.post(
	'/:id/mfa/enable',
	authorize('admin', 'manager'),
	validate({ params: paramsSchemas.idParam }),
	ctrl.enableMfa
);
router.post(
	'/:id/mfa/verify-setup',
	authorize('admin', 'manager'),
	validate({ params: paramsSchemas.idParam, body: userSchemas.verifyMfaSetup }),
	ctrl.verifyMfaSetup
);
router.post(
	'/:id/mfa/disable',
	authorize('admin', 'manager'),
	validate({ params: paramsSchemas.idParam }),
	ctrl.disableMfa
);
router.delete(
	'/:id',
	authorize('admin', 'manager'),
	validate({ params: paramsSchemas.idParam }),
	ctrl.remove
);

module.exports = router;
