<?php
require_once __DIR__ . '/includes/pexels.php';

$queries = [
    'hero' => 'party friends beer',
    'beer1' => 'beer mug',
    'party1' => 'night club party',
    'grill1' => 'bbq grill meat'
];

$images = [];

foreach ($queries as $key => $query) {
    echo "Fetching image for: $query...\n";
    $url = 'https://api.pexels.com/v1/search?query=' . urlencode($query) . '&per_page=1&page=1';
    $data = pexels_get($url);
    if ($data && !empty($data['photos'])) {
        $photo = $data['photos'][0];
        $src = $photo['src']['large2x'] ?? $photo['src']['large'];
        $dest = __DIR__ . '/assets/images/' . $key . '.jpg';
        if (download_to($src, $dest)) {
            $images[$key] = 'assets/images/' . $key . '.jpg';
            echo "Downloaded to $dest\n";
        } else {
            echo "Failed to download $src\n";
        }
    } else {
        echo "No photos found for $query\n";
    }
}

file_put_contents(__DIR__ . '/assets/images/manifest.json', json_encode($images, JSON_PRETTY_PRINT));

