<?php
header("content-type: application/json");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// connect to the mongo database
$m = new MongoDB\Driver\Manager("mongodb://root:oebb@mongo:27017");
//radsatznummer should be given as a search parameter
//if wrong throw back an error
#$radsatz = '023977AA#1';
$radsatz = $_GET['radsatz'];


$filter = ['overhauls.radsatznummer' => $radsatz];


//retrieve stationsnummer and taetigkeiten if the exist
$options_taetigkeiten = ['projection' => ['_id' => 0, 'overhauls.process.schadcodes' =>1, 'overhauls.final_state.ist_schrott' => 1, 'overhauls.radsatznummer' => 1, 'overhauls.final_state.station_path' => 1, 'overhauls.process.stationsname' => 1, 'overhauls.process.stationsnummer' => 1,   'overhauls.process.prediction' => 1, 'overhauls.process.taetigkeiten.name' => 1 ]];

$query_taetigkeiten = new MongoDB\Driver\Query($filter,$options_taetigkeiten);

$cursor_taetigkeiten = $m->executeQuery('oebb.process_instances',$query_taetigkeiten);

$res = array();
$codes = array();

foreach ($cursor_taetigkeiten as $document) {
  foreach ($document as $overhauls) {
    foreach ($overhauls as $overhaul) {
      if($overhaul->radsatznummer == $radsatz){
        foreach($overhaul->process as $process) {
          array_push($res,$process->stationsname);
          $proc = $process->taetigkeiten[0];
          // retrieve schrott from process instance
          if (property_exists($process, 'prediction') && $process->stationsnummer == 110) {
            $schrott = $process->prediction;
            $result['schrott'] = array('station' => $process->stationsnummer, 'tatsächlicher Status' => $overhaul->final_state->ist_schrott,'schrott'=> $schrott->schrott_probability, 'Vorhersage' => $schrott->schrott, 'Wahrscheinlickeitsgrenze' => $schrott->probability_limit);
            foreach($process->schadcodes as $schadcode) {
              $code = $schadcode->code;
              array_push($codes, $code);
            }
          }
          if(!empty((array)$proc)){
            array_push($res,$process->taetigkeiten[0]->name);
          }
        }
        //retrieve the stations path of wheelset
        $result['path'] = json_encode($overhaul->final_state->station_path);
      }
    }
  }
}
$result['schadcodes'] = $codes;

$result['taetigkeiten'] = $res;

//retrieve components
$radsatz = explode("#",$radsatz)[0];
$filter = ['radsatznummer' => $radsatz];

$tauschgruppe = array();

//check for radbremsscheiben_vorhanden || bremsscheibe
$options_components = ['projection' => ['_id' => 0, 'radbremsscheiben_vorhanden' => 1, 'bremsscheibe' => 1, 'lager.typ' => 1]];
$query_components = new MongoDB\Driver\Query($filter,$options_components);
$cursor_components = $m->executeQuery('oebb.components',$query_components);

//check for bremsscheibe && Lagertyp
foreach ($cursor_components as $document) {
  if(property_exists($document, 'radbremsscheiben_vorhanden') or property_exists($document, 'bremsscheibe')) {
    $result['tauschgruppe'] = array("bremsscheiben" => true);
    // array_push($tauschgruppe, array("bremsscheibe" => true));
  }
  else {
    $result['tauschgruppe'] = array("bremsscheiben" => false);
    // array_push($tauschgruppe, array("bremsscheibe" => false));
  }
  $result['lagerbauart'] = $document->lager->typ;
}

//check for innenring
$filter_ir = ['radsatznummer' => $radsatz, 'innenring' => ['$exists' =>  true]];
$query_ir = new MongoDB\Driver\Query($filter_ir);

$cursor_ir = $m->executeQuery('oebb.components',$query_ir);

  if(iterator_to_array($cursor_ir)) {
    $result['innenring'] = true;
  }
  else {
    $result['innenring'] = false;
  }

$options_wheelsets = ['projection' => ['_id' => 0, 'bearbeitungstyp' => 1, 'hochregallager_oder_massenlager' => 1, 'wellentyp' => 1]];
$query_wheelsets = new MongoDB\Driver\Query($filter, $options_wheelsets);
$cursor_wheelsets = $m->executeQuery('oebb.wheelsets', $query_wheelsets);

//check whether it is a Gueter- or personenzug
foreach($cursor_wheelsets as $document) {
  if($document->hochregallager_oder_massenlager == 'massenlager') {
    #array_push($tauschgruppe, 'gueterzug');
    $result['zug'] = 'gueterzug';
  } else {
    $result['zug'] = 'personenzug';
    #array_push($tauschgruppe, array('zug' => 'personenzug'));
  }
  $result['bearbeitungsstufe'] = $document->bearbeitungstyp;
  $result['wellentyp'] = $document->wellentyp;
}

#$result['tauschgruppe'] = $tauschgruppe;

echo json_encode($result);
