import { hash } from '@node-rs/argon2';
import { passwordStrength } from "check-password-strength";
import { IsBoolean, IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString, IsUrl } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import { config } from '../../../config';
import { PasswordMustContainLowercaseLetterError, PasswordMustContainNumberError, PasswordMustContainUppercaseLetterError, PasswordTooShortError, UserEmailNeededError, UsernameContainsIllegalCharacterError } from '../../../errors/UserErrors';
import { UserGroupNotFoundError } from '../../../errors/UserGroupErrors';
import { User } from '../../entities/User';
import { UserGroup } from '../../entities/UserGroup';


/**
 * This class is used to update a User entity (via put request).
 */
export class UpdateUser {

    /**
     * The updated user's id.
     * This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to).
     */
    @IsInt()
    id: number;

    /**
     * The updated user's first name.
     */
    @IsString()
    firstname: string;

    /**
     * The updated user's middle name.
     */
    @IsString()
    @IsOptional()
    middlename?: string;

    /**
     * The updated user's last name.
     */
    @IsString()
    lastname: string;

    /**
     * The updated user's username.
     * You have to provide a email addres, so this is optional.
     */
    @IsOptional()
    @IsString()
    username?: string;

    /**
     * The updated user's email address.
     */
    @IsEmail()
    @IsString()
    @IsNotEmpty()
    email: string;

    /**
     * The updated user's phone number.
     * This will be validated against the configured country phone numer syntax (default: international).
     */
    @IsPhoneNumber(config.phone_validation_countrycode)
    @IsOptional()
    phone?: string;

    /**
     * The new updated's password. 
     * Only provide it if you want it updated.
     * Changeing the password will invalidate all of the user's jwts.
     * This will of course not be saved in plaintext :)
     */
    @IsString()
    @IsOptional()
    password?: string;

    /**
     * Should the user be enabled?
     */
    @IsBoolean()
    @IsOptional()
    enabled: boolean = true;

    /**
     * The updated user's groups' ids.
     */
    @IsOptional()
    groups?: number | number[]

    /**
    * The user's profile pic (or rather a url pointing to it).
    */
    @IsString()
    @IsUrl()
    @IsOptional()
    profilePic?: string;

    /**
     * Updates a user entity based on this.
     * @param user The user that shall be updated.
     */
    public async update(user: User): Promise<User> {
        if (!this.email) {
            throw new UserEmailNeededError();
        }
        if (this.username.includes("@")) { throw new UsernameContainsIllegalCharacterError(); }

        if (this.password) {
            let password_strength = passwordStrength(this.password);
            if (!password_strength.contains.includes("uppercase")) { throw new PasswordMustContainUppercaseLetterError(); }
            if (!password_strength.contains.includes("lowercase")) { throw new PasswordMustContainLowercaseLetterError(); }
            if (!password_strength.contains.includes("number")) { throw new PasswordMustContainNumberError(); }
            if (!(password_strength.length > 9)) { throw new PasswordTooShortError(); }
            user.password = await hash(this.password + user.uuid);
            user.refreshTokenCount = user.refreshTokenCount + 1;
        }

        user.email = this.email;
        user.username = this.username;
        user.enabled = this.enabled;
        user.firstname = this.firstname
        user.middlename = this.middlename
        user.lastname = this.lastname
        user.phone = this.phone;
        user.groups = await this.getGroups();

        if (!this.profilePic) { user.profilePic = `https://lauf-fuer-kaya.de/lfk-logo.png`; }
        else { user.profilePic = this.profilePic; }

        return user;
    }

    /**
     * Get's all groups for this user by their id's;
     */
    public async getGroups() {
        if (!this.groups) { return null; }
        let groups = new Array<UserGroup>();
        if (!Array.isArray(this.groups)) {
            this.groups = [this.groups]
        }
        for (let group of this.groups) {
            let found = await getConnectionManager().get().getRepository(UserGroup).findOne({ id: group });
            if (!found) { throw new UserGroupNotFoundError(); }
            groups.push(found);
        }
        return groups;
    }
}