import type { Handler } from 'hono';
import { updateUser } from 'officerdb';
import * as errors from '@@/custom-errors';

export const updateUserHandler: Handler = async function (ctx) {
  const { name, username, avatar } = ctx.get('body');
  const reqUser = ctx.get('user');

  if (typeof name !== 'string') throw errors.BAD_REQUEST('Name is required');

  await updateUser(reqUser.id, {
    name,
    username: typeof username === 'string' ? username : undefined,
    avatar: avatar ?? null,
  });

  return ctx.json({ ok: true });
};
