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

/**
 * This class is used to update a Track entity (via put request).
 */
export class UpdateTrack {
    /**
     * The updated track'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;

    @IsString()
    @IsNotEmpty()
    name: string;

    /**
     * The updated track's distance in meters (must be greater than 0).
     */
    @IsInt()
    @IsPositive()
    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;


    /**
     * Update a Track entity based on this.
     * @param track The track that shall be updated.
     */
    public async update(track: Track): Promise<Track> {
        track.name = this.name;
        track.distance = this.distance;
        track.minimumLapTime = this.minimumLapTime;
        if (this.minimumLapTime < 0) {
            throw new TrackLapTimeCantBeNegativeError();
        }

        return track;
    }
}