<?php
require_once __DIR__.'/../includes/pexels.php';
$query = 'delicious food dishes table top view';
$url = 'https://api.pexels.com/v1/search?query=' . urlencode($query) . '&orientation=portrait&per_page=5&page=1';
$data = pexels_get($url);
if ($data && !empty($data['photos'])) {
    // Pick a random one from the top 5 to get some variety if re-run
    $photo = $data['photos'][array_rand($data['photos'])];
    $src = $photo['src']['large2x'] ?? $photo['src']['large'];
    $target = __DIR__ . '/../assets/images/auth-bg-new.jpg';
    if (download_to($src, $target)) {
        echo "Success: Downloaded " . $target;
    } else {
        echo "Error: Failed to download image.";
    }
} else {
    echo "Error: Failed to fetch image info from Pexels.";
}
