<?php
require_once __DIR__ . '/../config/config.php';
$mediaId = $argv[1] ?? null;
if (!$mediaId) { echo "Usage: php get_media_direct.php <mediaId>\n"; exit(1); }
$token = getConfigFromDB('whatsapp_token', '');
$apiUrl = rtrim(getConfigFromDB('whatsapp_api_url', 'https://graph.facebook.com/v22.0/'), '/');
$url = "{$apiUrl}/{$mediaId}";
echo "Calling: $url\n";
$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [ 'Authorization: Bearer ' . $token ],
    CURLOPT_TIMEOUT => 10,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
    echo "CURL ERR: $err\n"; exit(1);
}
echo "HTTP Code: $httpCode\n";
echo "$response\n";
