<?php
declare(strict_types=1);

/**
 * 生成 /api/v1/authToken（或 /api/v1/authtoken）所需 signature
 *
 * 用法：
 * php scripts/gen_auth_token_signature.php --agent_id=1001 --secret=your_secret
 * php scripts/gen_auth_token_signature.php --agent_id=1001 --secret=your_secret --time=1713772800
 */

$options = getopt('', ['agent_id:', 'secret:', 'time::']);

$agentId = $options['agent_id'] ?? '202cb962ac59075b964b07152d234b70';
$secret = $options['secret'] ?? 'xF75oK91TQj13s0UmNIr1NBWMWGfflNO';
$time = $options['time'] ?? (string) time();

if ($agentId === '' || $secret === '' || $time === '') {
    echo "缺少参数。\n";
    echo "用法: php scripts/gen_auth_token_signature.php --agent_id=1001 --secret=your_secret [--time=unix_timestamp]\n";
    exit(1);
}

if (ctype_digit($time) === false) {
    echo "time 必须是 unix 时间戳（纯数字字符串）。\n";
    exit(1);
}

$signature = md5($agentId . $secret . $time);
$query = http_build_query([
    'agent_id' => $agentId,
    'secret' => $secret,
    'time' => $time,
    'signature' => $signature,
]);

echo "agent_id: {$agentId}\n";
echo "secret: {$secret}\n";
echo "time: {$time}\n";
echo "signature: {$signature}\n";
echo "query: {$query}\n";

