<?php

declare(strict_types=1);

namespace App;

include_once 'vendor/autoload.php';

use GuzzleHttp\Client;
use League\Uri\UriTemplate;

$BASE_URI = 'https://qline.tugraz.at/QSYSTEM_TUG/';
$CLIENT_ID = "mobility_online_id";
$CLIENT_SECRET = "";

//---------------------------------------------------------

function getToken() {
    global $CLIENT_ID, $CLIENT_SECRET, $BASE_URI;

    $client = new Client(['base_uri' => $BASE_URI]);
    $response = $client->post('wbOAuth2.token', [
        'form_params' => [
            'client_id' => $CLIENT_ID,
            'client_secret' => $CLIENT_SECRET,
            'grant_type' => 'client_credentials',
        ],
    ]);

    $result = json_decode((String)$response->getBody(), true, 512, JSON_THROW_ON_ERROR);
    return $result['access_token'];
}

//---------------------------------------------------------

function dumpAllCards() {
    global $BASE_URI;

    $filters = [];
    //$filters[] = 'IDENT_NR_OBFUSCATED-eq=5E5332FCA667E1D7';

    $uriTemplate = new UriTemplate('pl/rest/brm.pm.extension.ucardfoto/{?%24filter,%24format,%24ctx,%24top}');
    $uri = (string) $uriTemplate->expand([
        '%24filter' => implode(';', $filters),
        '%24format' => 'json',
        '%24ctx' => 'lang=en',
        '%24top' => '-1',
    ]);

    $headers =  [
        'Authorization' => 'Bearer '.getToken(),
        'Accept' => 'application/json',
    ];
    $client = new Client(['base_uri' => $BASE_URI]);
    $response = $client->get($uri, ['headers' => $headers]);
    $json = json_decode((string)$response->getBody(), true, 512, JSON_THROW_ON_ERROR);
    
    foreach ($json['resource'] as $res) {
        $content = $res['content'];
        var_dump($content);
    }
    var_dump('Count: '.count($json['resource']));
}

//---------------------------------------------------------

function lookupPerson() {
    global $BASE_URI;

    $filters = [];
    $filters[] = 'VORNAME-eq=Christoph';
    $filters[] = 'NACHNAME-eq=Reiter';
    //$filters[] = 'NR_OBFUSCATED-eq=5E5332FCA667E1D7';
    //$filters[] = 'EMAIL_ADRESSE_1-eq=Reiter';
    //$filters[] = 'MATRIKELNUMMER-eq=Reiter';
    //$filters[] = 'ST_PERSON_NR-eq=Reiter';

    $uriTemplate = new UriTemplate('pl/rest/slc.v0.studierendendaten.student_ucRest/{?%24filter,%24format,%24ctx,%24top}');
    $uri = (string) $uriTemplate->expand([
        '%24filter' => implode(';', $filters),
        '%24format' => 'json',
        '%24ctx' => 'lang=en',
        '%24top' => '-1',
    ]);

    $headers =  [
        'Authorization' => 'Bearer '.getToken(),
        'Accept' => 'application/json',
    ];
    $client = new Client(['base_uri' => $BASE_URI]);
    $response = $client->get($uri, ['headers' => $headers]);
    $json = json_decode((string)$response->getBody(), true, 512, JSON_THROW_ON_ERROR);

    foreach ($json['resource'] as $res) {
        $content = $res['content'];
        var_dump($content);
    }
    var_dump('Count: '.count($json['resource']));
}

//dumpAllCards();
lookupPerson();