import { IsInt, IsObject, IsPositive, IsString } from "class-validator";
import { Address } from '../entities/Address';
import { GroupContact } from '../entities/GroupContact';
import { ResponseObjectType } from '../enums/ResponseObjectType';
import { IResponse } from './IResponse';
import { ResponseRunnerGroup } from './ResponseRunnerGroup';

/**
 * Defines the group contact response.
*/
export class ResponseGroupContact implements IResponse {
    /**
    * The responseType.
    * This contains the type of class/entity this response contains.
    */
    responseType: ResponseObjectType = ResponseObjectType.GROUPCONTACT;

    /**
     * The contact's id.
     */
    @IsInt()
    id: number;

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

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

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

    /**
     * The contact's phone number.
     */
    @IsString()
    phone?: string;

    /**
     * The contact's e-mail address.
     */
    @IsString()
    email?: string;

    /**
     * The contact's associated runner groups.
     */
    @IsObject()
    groups: ResponseRunnerGroup[];

    /**
     * The contact's address.
     * This is a address object to prevent any formatting differences.
     */
    @IsObject()
    address?: Address;

    @IsInt()
    @IsPositive()
    created_at: number;

    @IsInt()
    @IsPositive()
    updated_at: number;

    /**
     * Creates a ResponseGroupContact object from a contact.
     * @param contact The contact the response shall be build for.
     */
    public constructor(contact: GroupContact) {
        this.id = contact.id;
        this.firstname = contact.firstname;
        this.middlename = contact.middlename;
        this.lastname = contact.lastname;
        this.phone = contact.phone;
        this.email = contact.email;
        this.address = contact.address;
        this.groups = new Array<ResponseRunnerGroup>();
        if (contact.groups) {
            for (let group of contact.groups) {
                this.groups.push(group.toResponse());
            }
        }
        this.created_at = contact.created_at;
        this.updated_at = contact.updated_at;
    }
}
