import { IsInt, IsOptional, IsPositive, IsString } from "class-validator";
import { TrackLapTimeCantBeNegativeError } from '../../errors/TrackErrors';
import { Track } from '../entities/Track';
import { ResponseObjectType } from '../enums/ResponseObjectType';
import { IResponse } from './IResponse';

/**
 * Defines the track response.
*/
export class ResponseTrack implements IResponse {
    /**
    * The responseType.
    * This contains the type of class/entity this response contains.
    */
    responseType: ResponseObjectType = ResponseObjectType.TRACK;

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

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

    /**
     * The track's length/distance in meters.
     */
    @IsInt()
    distance: number;

    /**
     * The minimum time a runner should take to run a lap on this track (in seconds).
     * Will be used for fraud detection.
     */
    @IsInt()
    @IsOptional()
    minimumLapTime?: number;

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

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

    /**
     * Creates a ResponseTrack object from a track.
     * @param track The track the response shall be build for.
     */
    public constructor(track: Track) {
        this.id = track.id;
        this.name = track.name;
        this.distance = track.distance;
        this.minimumLapTime = track.minimumLapTime;
        if (this.minimumLapTime < 0) {
            throw new TrackLapTimeCantBeNegativeError();
        }
        this.created_at = track.created_at;
        this.updated_at = track.updated_at;
    }
}
