import { IsInt, IsNotEmpty, IsNumber, IsObject, IsOptional, IsPositive, IsString } from "class-validator";
import { RunnerGroup } from '../entities/RunnerGroup';
import { ResponseObjectType } from '../enums/ResponseObjectType';
import { IResponse } from './IResponse';
import { ResponseGroupContact } from './ResponseGroupContact';

/**
 * Defines the runnerGroup response.
*/
export abstract class ResponseRunnerGroup implements IResponse {
    /**
    * The responseType.
    * This contains the type of class/entity this response contains.
    */
    responseType: ResponseObjectType = ResponseObjectType.RUNNERGROUP;

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

    /**
     * The runnerGroup's name.
     */
    @IsString()
    @IsNotEmpty()
    name: string;


    /**
     * The runnerGroup's contact.
     */
    @IsObject()
    @IsOptional()
    contact?: ResponseGroupContact;

    @IsOptional()
    @IsNumber()
    total_distance: number

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

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

    /**
     * Creates a ResponseRunnerGroup object from a runnerGroup.
     * @param group The runnerGroup the response shall be build for.
     */
    public constructor(group: RunnerGroup) {
        this.id = group.id;
        this.name = group.name;
        if (group.contact) { this.contact = group.contact.toResponse(); };
        if (group.runners) { this.total_distance = group.runners.reduce((p, c) => p + c.distance, 0) }
        this.created_at = group.created_at;
        this.updated_at = group.updated_at;
    }
}
