<?php
/**
 * Debug script to check Flickr URL formats and file sizes
 */
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/classes/FlickrAPI.php';
// Try config.php first, fall back to config.example.php for testing
$configFile = __DIR__ . '/config.php';
$config = require $configFile;
if (empty($config['flickr']['api_key']) && file_exists(__DIR__ . '/config.example.php')) {
    $config = require __DIR__ . '/config.example.php';
}

if (empty($config['flickr']['api_key'])) {
    die("Flickr API key not configured\n");
}

// Helper to check URL status and file size
function checkUrl($url) {
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_NOBODY => true, // HEAD request
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_TIMEOUT => 10,
        CURLOPT_USERAGENT => 'Mozilla/5.0',
        CURLOPT_HTTPHEADER => ['Referer: https://www.flickr.com/'],
    ]);
    curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    curl_close($ch);

    $sizeStr = $size > 0 ? formatSize($size) : '?';
    return ['status' => $httpCode, 'size' => $sizeStr, 'bytes' => $size];
}

function formatSize($bytes) {
    if ($bytes >= 1048576) return round($bytes / 1048576, 1) . ' MB';
    if ($bytes >= 1024) return round($bytes / 1024) . ' KB';
    return $bytes . ' B';
}

$flickr = new FlickrAPI(
    $config['flickr']['api_key'],
    $config['flickr']['api_secret'] ?? '',
    $config['flickr_user_id'] ?? ''
);

echo "=== Testing Flickr URL Formats ===\n\n";

// Get one album
$albums = $flickr->getPhotosets(1, 1);
if (empty($albums)) {
    die("No albums found\n");
}

$albumId = $albums[0]['id'];
$albumTitle = $albums[0]['title']['_content'] ?? 'Unknown';
echo "Album: $albumTitle (ID: $albumId)\n\n";

// Get photos from album
$result = $flickr->getPhotosetPhotos($albumId, 1, 3);

if (empty($result['photos'])) {
    die("No photos in album\n");
}

foreach ($result['photos'] as $i => $photo) {
    echo "--- Photo " . ($i + 1) . " ---\n";
    echo "ID: " . $photo['id'] . "\n";
    echo "Title: " . $photo['title'] . "\n";
    echo "Server: " . $photo['server'] . "\n";
    echo "Secret: " . $photo['secret'] . "\n";
    echo "Original Secret: " . $photo['original_secret'] . "\n";
    echo "Original Format: " . $photo['original_format'] . "\n";

    echo "\nURLs (with sizes):\n";

    // Test each URL size
    $sizes = ['medium640', 'large', 'large2048', 'original'];
    foreach ($sizes as $size) {
        $url = $photo['urls'][$size] ?? null;
        if ($url) {
            $info = checkUrl($url);
            $status = $info['status'] == 200 ? '✓' : '✗ ' . $info['status'];
            echo "  $size: $status - {$info['size']}\n";
            echo "    $url\n";
        } else {
            echo "  $size: (not available)\n";
        }
    }

    // If original secret differs from secret, note it
    if ($photo['original_secret'] !== $photo['secret']) {
        echo "\n  *** Original secret is DIFFERENT - originals should be accessible ***\n";
    } else {
        echo "\n  Note: Original secret = Secret (originals may not be enabled in Flickr settings)\n";
    }

    echo "\n";
}

echo "=== Summary ===\n";
echo "If 'original' shows ✗ 403 or ✗ 404:\n";
echo "  -> Enable original downloads in Flickr: Settings > Privacy > Allow downloads\n";
echo "If 'original' shows ✓ with large size:\n";
echo "  -> Downloads should work at full quality!\n";
