import { IsEmail, IsInt, IsNotEmpty, IsObject, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import { config } from '../../../config';
import { RunnerGroupNotFoundError } from '../../../errors/RunnerGroupErrors';
import { Address } from '../../entities/Address';
import { GroupContact } from '../../entities/GroupContact';
import { RunnerGroup } from '../../entities/RunnerGroup';


/**
 * This class is used to update a GroupContact entity (via put request).
 */
export class UpdateGroupContact {
    /**
     * The updated contact'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 contact's first name.
     */
    @IsNotEmpty()
    @IsString()
    firstname: string;

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

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

    /**
     * The updated contact's address.
     */
    @IsOptional()
    @IsObject()
    address?: Address;

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

    /**
     * The updated contact's email address.
     */
    @IsOptional()
    @IsEmail()
    email?: string;

    /**
     * The updated contacts's groups' ids.
     * You can provide either one groupId or an array of groupIDs.
     */
    @IsOptional()
    groups?: number[] | number


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

    /**
     * Updates a provided Donor entity based on this.
     * @param contact the contact you want to update.
     */
    public async update(contact: GroupContact): Promise<GroupContact> {
        contact.firstname = this.firstname; GroupContact
        contact.middlename = this.middlename;
        contact.lastname = this.lastname;
        contact.phone = this.phone;
        contact.email = this.email;
        if (!this.address) { contact.address.reset(); }
        else { contact.address = this.address; }
        Address.validate(contact.address);
        contact.groups = await this.getGroups();

        return contact;
    }
}