import {
    IsInt
} from "class-validator";
import { Donation } from '../entities/Donation';
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 via selfservice.
     */
    @IsInt()
    runnersViaSelfservice: number;

    /**
     * The amount of runners registered via kiosk.
     */
    @IsInt()
    runnersViaKiosk: number;

    /**
     * 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 total donation count (cent).
     */
    @IsInt()
    total_donations: number;

    /**
     * The total donor count.
     */
    @IsInt()
    total_donors: number;

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

    /**
     * The average donation per distance (cent).
     */
    @IsInt()
    average_donation: number;

    /**
     * Creates a new stats response containing some basic statistics for a dashboard or public display.
     * @param runnersViaSelfservice number of runners registered via selfservice
     * @param runners number of runners
     * @param teams number of teams - no relations have to be resolved.
     * @param orgs number of orgs - no relations have to be resolved.
     * @param users number of users - no relations have to be resolved.
     * @param scans number of 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(runnersViaSelfservice: number, runners: number, teams: number, orgs: number, users: number, scans: number, donations: Donation[], distance: number, donors: number, runnersViaKiosk: number) {
        this.runnersViaSelfservice = runnersViaSelfservice;
        this.total_runners = runners;
        this.total_teams = teams;
        this.total_orgs = orgs;
        this.total_users = users;
        this.total_scans = scans;
        this.total_distance = distance;
        this.total_donation = donations.reduce((sum, current) => sum + current.amount, 0);
        this.total_donations = donations.length;
        this.average_donation = this.total_donation / this.total_donations
        this.total_donors = donors;
        this.average_distance = this.total_distance / this.total_runners;
        this.runnersViaKiosk = runnersViaKiosk;
    }
}
