import * as argon2 from "argon2";
import { IsBoolean, IsEmail, IsInt, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import { config } from '../../config';
import { UsernameOrEmailNeededError } from '../../errors/AuthError';
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 at least one of: {email, username}.
     */
    @IsOptional()
    @IsString()
    username?: string;

    /**
     * The updated user's email address.
     * You have to provide at least one of: {email, username}.
     */
    @IsEmail()
    @IsString()
    @IsOptional()
    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()
    enabled: boolean = true;

    /**
     * The updated user's groups.
     * This just has to contain the group's id - everything else won't be changed.
     */
    @IsOptional()
    groups?: UserGroup[]

    /**
     * Updates a provided User entity based on this.
     */
    public async updateUser(user: User): Promise<User> {
        user.email = this.email;
        user.username = this.username;
        if ((user.email === undefined || user.email === null) && (user.username === undefined || user.username === null)) {
            throw new UsernameOrEmailNeededError();
        }
        if (this.password) {
            user.password = await argon2.hash(this.password + user.uuid);
            user.refreshTokenCount = user.refreshTokenCount + 1;
        }

        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();
        //TODO: ProfilePics

        return user;
    }

    /**
     * Loads the updated user's groups based on their ids.
     */
    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.id });
            if (!found) { throw new UserGroupNotFoundError(); }
            groups.push(found);
        }
        return groups;
    }
}