import { register, activateUser, login, logout } from '../services/authService.js';

const registerUser = async (req, res, next) => {
  try {
    const { email, password } = req.body;
    const user = await register(email, password);
    res.status(201).json(user);
  } catch (error) {
    next(error);
  }
};

const activate = async (req, res, next) => {
  try {
    const { activationCode } = req.params;
    const user = await activateUser(activationCode);
    res.status(200).json(user);
  } catch (error) {
    next(error);
  }
};

const loginUser = async (req, res, next) => {
  console.log('login');
  try {
    const { email, password } = req.body;
    const result = await login(email, password);
    res.status(200).json(result);
  } catch (error) {
    next(error);
  }
};

const logoutUser = async(req, res) => {
  const { userid: userId, authtoken: authToken } = req.headers;
  try {
    logout(userId, authToken);
  } catch (error) {
    res.status(401).json({ msg: 'not found' });
  }
  res.status(200).json({ msg: 'loggedout' });
}

export { registerUser, activate, loginUser, logoutUser };
