package org.intellimate.server.rest;

import org.apache.commons.validator.routines.EmailValidator;
import org.intellimate.server.BadRequestException;
import org.intellimate.server.database.model.tables.records.UserRecord;
import org.intellimate.server.database.operations.UserOperations;
import org.intellimate.server.proto.User;
import org.springframework.security.crypto.bcrypt.BCrypt;

import java.util.Objects;

/**
 * handles all the users requests
 * @author LeanderK
 * @version 1.0
 */
public class Users {
    private final UserOperations userOperations;

    public Users(UserOperations userOperations) {
        this.userOperations = userOperations;
    }

    /**
     * adds an user to the database
     * @param username the chosen username, must be at least 4 characters long
     * @param email the email
     * @param password the chosen password, must be at least 4 characters long
     * @throws BadRequestException if one of the parameters is invalid
     */
    public User addUser(String username, String email, String password) {
        Objects.nonNull(username);
        Objects.nonNull(email);
        Objects.nonNull(password);
        if (username.length() < 4) {
            throw new BadRequestException("invalid username length, must be at least 4: " + username);
        }
        if (password.length() < 4) {
            throw new BadRequestException("invalid password length, must be at least 4");
        }
        if (!EmailValidator.getInstance(false).isValid(email)) {
            throw new BadRequestException("invalid email: " + email);
        }
        String hashpw = BCrypt.hashpw(password, BCrypt.gensalt());
        UserRecord userRecord = new UserRecord(null, email, hashpw, username);
        int id = userOperations.insertUser(userRecord);
        return User.newBuilder()
                .setEmail(email)
                .setId(id)
                .setUsername(username)
                .build();
    }

    public void removeUser(int id) {

    }
}

@@ -143,16 +143,118 @@
 eption;%0A
+import org.intellimate.server.NotFoundException;%0Aimport org.intellimate.server.UnauthorizedException;%0A
 import o
@@ -383,16 +383,108 @@
 ations;%0A
+import org.intellimate.server.jwt.JWTokenPassed;%0Aimport org.intellimate.server.jwt.Subject;%0A
 import o
@@ -2149,41 +2149,662 @@
     
-public void removeUser(int id) %7B%0A
+/**%0A     * removes the user from the database%0A     * @param id the user to remove%0A     * @param jwTokenPassed the jwt passed%0A     */%0A    public void removeUser(int id, JWTokenPassed jwTokenPassed) %7B%0A        if (jwTokenPassed.getSubject() != Subject.USER) %7B%0A            throw new BadRequestException(%22method only allowed for users%22);%0A        %7D%0A        if (id != jwTokenPassed.getId()) %7B%0A            throw new UnauthorizedException(%22not allowed to delete other user%22);%0A        %7D%0A        boolean existed = userOperations.deleteUser(id);%0A        if (!existed) %7B%0A            throw new NotFoundException(String.format(%22user %25d already deleted%22, id));%0A        %7D
 %0A   

finished users