<?php
/** تست login + acl/me برای melk */
declare(strict_types=1);
define('ACC_BASE_PATH', __DIR__);
require __DIR__ . '/standalone/bootstrap.php';

$base = 'http://localhost/acc-engine/api.php/acc/v1';
$login = $argv[1] ?? 'melk_lead_d1';
$pass  = $argv[2] ?? 'Melk@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]),
]);
$loginRes = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$loginJson = json_decode($loginRes, true);
$token = $loginJson['token'] ?? '';

$meRes = '';
if ($token) {
    $ch = curl_init("$base/acl/me");
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => ["Authorization: Bearer $token", 'Accept: application/json'],
    ]);
    $meRes = curl_exec($ch);
    curl_close($ch);
}

$overview = '';
if ($token) {
    $ch = curl_init("$base/melk/overview");
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => ["Authorization: Bearer $token", 'Accept: application/json'],
    ]);
    $overview = curl_exec($ch);
    curl_close($ch);
}

echo json_encode([
    'login_http' => $code,
    'login' => $loginJson,
    'acl_me' => json_decode($meRes, true),
    'overview' => json_decode($overview, true),
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
