<?php
require_once dirname(__DIR__) . '/adapter/chairman_analysis_helper.php';

$raceDate = isset($_GET['race_date']) ? chairman_safe_date($_GET['race_date']) : date('Y-m-d');
$venue = isset($_GET['venue']) ? $_GET['venue'] : '';
$raceNum = isset($_GET['race_num']) ? intval($_GET['race_num']) : 0;

$payload = chairman_payload($raceDate,$venue,$raceNum);
$top = chairman_top($raceDate,$venue,$raceNum);

$out = array(
    'status'=>'OK',
    'adapter_version'=>'1.5',
    'race_date'=>$raceDate,
    'venue'=>$venue,
    'race_num'=>$raceNum,
    'top'=>$top,
    'counts'=>array(
        'summary'=>isset($payload['summary']) && is_array($payload['summary']) ? count($payload['summary']) : 0,
        'ranking'=>isset($payload['ranking']) && is_array($payload['ranking']) ? count($payload['ranking']) : 0
    ),
    'payload'=>$payload
);

if(isset($_GET['chairman_debug']) && $_GET['chairman_debug']=='1'){
    $out['debug']=chairman_debug();
}

chairman_json_response($out);
