import {
    IsInt
} from "class-validator";
import { Donation } from '../entities/Donation';
import { Runner } from '../entities/Runner';
import { RunnerOrganization } from '../entities/RunnerOrganization';
import { RunnerTeam } from '../entities/RunnerTeam';
import { Scan } from '../entities/Scan';
import { User } from '../entities/User';
import { ResponseObjectType } from '../enums/ResponseObjectType';
import { IResponse } from './IResponse';

/**
 * Defines the stats response.
 * The stats response calculates some basic stats for a dashboard or public display.
*/
export class ResponseStats implements IResponse {
    /**
    * The responseType.
    * This contains the type of class/entity this response contains.
    */
    responseType: ResponseObjectType = ResponseObjectType.STATS;

    /**
     * The amount of runners registered in the system.
     */
    @IsInt()
    total_runners: number;

    /**
     * The amount of teams registered in the system.
     */
    @IsInt()
    total_teams: number;

    /**
     * The amount of organizations registered in the system.
     */
    @IsInt()
    total_orgs: number;

    /**
     * The amount of users registered in the system.
     */
    @IsInt()
    total_users: number;

    /**
     * The amount of valid scans registered in the system.
     */
    @IsInt()
    total_scans: number;

    /**
     * The total distance that all runners ran combined.
     */
    @IsInt()
    total_distance: number;

    /**
     * The total donation amount.
     */
    @IsInt()
    total_donation: number;

    /**
     * The average distance ran per runner.
     */
    @IsInt()
    average_distance: number;

    /**
     * Creates a new stats response containing some basic statistics for a dashboard or public display.
     * @param runners Array containing all runners - the following relations have to be resolved: scans, scans.track
     * @param teams Array containing all teams - no relations have to be resolved.
     * @param orgs Array containing all orgs - no relations have to be resolved.
     * @param users Array containing all users - no relations have to be resolved.
     * @param scans Array containing all scans - no relations have to be resolved.
     * @param donations Array containing all donations - the following relations have to be resolved: runner, runner.scans, runner.scans.track
     */
    public constructor(runners: Runner[], teams: RunnerTeam[], orgs: RunnerOrganization[], users: User[], scans: Scan[], donations: Donation[]) {
        this.total_runners = runners.length;
        this.total_teams = teams.length;
        this.total_orgs = orgs.length;
        this.total_users = users.length;
        this.total_scans = scans.filter(scan => { scan.valid === true }).length;
        this.total_distance = runners.reduce((sum, current) => sum + current.distance, 0);
        this.total_donation = donations.reduce((sum, current) => sum + current.amount, 0);
        this.average_distance = this.total_distance / this.total_runners;
    }
}
