import type { Handler } from 'hono';
import { getUserById, updateUser } from 'officerdb';
import argon2 from 'argon2';
import { sign } from '@@/jwt';
import * as errors from '@@/custom-errors';
import { validatePassword } from './validate-password';
import { IS_DEV_BUILD } from '@@/build-env';

export const changePasswordHandler: Handler = async function (ctx) {
  const { password, newPassword } = ctx.get('body');
  if (!IS_DEV_BUILD) validatePassword(newPassword);
  const reqUser = ctx.get('user');

  const dbUser = await getUserById(reqUser.id);

  if (!dbUser) throw errors.UNAUTHORIZED();

  const isCorrectOldPassword = await argon2.verify(dbUser.password!, password);
  if (!isCorrectOldPassword) {
    throw errors.createError(403, 'Incorrect old password');
  }

  const newPasswordHash = await argon2.hash(newPassword);
  const passwordChangedAt = new Date();
  await updateUser(reqUser.id, { password: newPasswordHash, passwordChangedAt });

  const { id, email, name, role } = reqUser;
  const username = dbUser.username ?? reqUser.username;
  const token = await sign({ id, email, name, username, role });

  return ctx.json({ token });
};
