<?php
/** تست login EduBrain */
declare(strict_types=1);
define( 'ACC_BASE_PATH', __DIR__ );
require __DIR__ . '/standalone/bootstrap.php';

$base  = 'http://127.0.0.1/acc-engine/api.php/acc/v1';
$login = $argv[1] ?? 'edu_student_konkur';
$pass  = $argv[2] ?? 'Edu@1404';

$ch = curl_init( "$base/public/auth/login" );
curl_setopt_array( $ch, [
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => [ 'Content-Type: application/json' ],
    CURLOPT_POSTFIELDS     => json_encode( [ 'username' => $login, 'password' => $pass ] ),
    CURLOPT_TIMEOUT        => 15,
] );
$loginJson = json_decode( (string) curl_exec( $ch ), true );
$code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
$token = $loginJson['token'] ?? '';

$out = [ 'login_http' => $code, 'login' => $loginJson ];

if ( $token ) {
    foreach ( [ '/education/me', '/education/dashboard', '/education/gis/map' ] as $path ) {
        $ch = curl_init( $base . $path );
        curl_setopt_array( $ch, [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER     => [ "Authorization: Bearer $token", 'Accept: application/json' ],
            CURLOPT_TIMEOUT        => 15,
        ] );
        $out[ trim( $path, '/' ) ] = json_decode( (string) curl_exec( $ch ), true );
        curl_close( $ch );
    }
}

echo json_encode( $out, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );
