<?php
class DecisionEngine implements DecisionInterface {
    public function decide(array $ranking) {
        $byRace = array();
        foreach($ranking as $r){
            $race = isset($r['race_num']) ? (string)$r['race_num'] : '0';
            if(!isset($byRace[$race])) $byRace[$race] = array();
            $byRace[$race][] = $r;
        }

        $out = array();
        foreach($byRace as $race=>$rows){
            usort($rows, function($a,$b){
                return chairman_n($a['calibrated_score']) < chairman_n($b['calibrated_score']) ? 1 : -1;
            });
            $top = count($rows) ? $rows[0] : null;
            $q = array_slice($rows,0,2);
            $trio = array_slice($rows,0,4);
            $out[] = array(
                'race_num'=>chairman_i($race),
                'top'=>$top,
                'q'=>$q,
                'trio'=>$trio,
                'horse_count'=>count($rows)
            );
        }
        return $out;
    }
}
