const express = require('express');
const { audit } = require('../db');
const { getProfile, savePreferences } = require('../services/profile');

const router = express.Router();

router.get('/profile', (req, res) => {
  res.json(getProfile(req.user));
});

router.put('/profile', (req, res) => {
  const preferences = savePreferences(req.user.id, req.body.preferences || {});
  audit(req.user.id, 'update', 'profile_preferences', req.user.id, null, { preferences });
  res.json({ user: getProfile(req.user).user, preferences });
});

module.exports = router;
