import type { User } from "./userEntity";

type CreateUserProps = {
  username: User["username"];
  firstname: User["firstname"];
  lastname: User["lastname"];
  email: User["email"];
  twoFactorEnabled: User["twoFactorEnabled"];
};

type UpdateUserArtifacts = {
  user: User;
  diff: Partial<User>;
};

const create = (props: CreateUserProps): User => ({
  email: props.email,
  firstname: props.firstname,
  lastname: props.lastname,
  username: props.username,
  twoFactorEnabled: props.twoFactorEnabled,
});

const updateUser = (
  user: User,
  payload: Partial<User>
): UpdateUserArtifacts => ({
  user: Object.assign({}, user, payload),
  diff: payload,
});

const changeTwoFactor = (user: User, payload: boolean): UpdateUserArtifacts =>
  updateUser(user, { twoFactorEnabled: payload });

const changeUsername = (user: User, payload: string): UpdateUserArtifacts =>
  updateUser(user, { username: payload });
const changeEmail = (user: User, payload: string): UpdateUserArtifacts =>
  updateUser(user, { email: payload });
  
export type { UpdateUserArtifacts };
export { create, updateUser, changeTwoFactor, changeUsername, changeEmail };
