<?php
require __DIR__ . '/../vendor/autoload.php';
$app = require_once __DIR__ . '/../bootstrap/app.php';
$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

$artworkId = 69478;

$stats = DB::table('artwork_stats')->where('artwork_id', $artworkId)->first();
echo "artwork_stats row: " . json_encode($stats) . PHP_EOL;

$events = DB::table('artwork_view_events')->where('artwork_id', $artworkId)->count();
echo "artwork_view_events for {$artworkId}: {$events}" . PHP_EOL;

$latest = DB::table('artwork_view_events')->latest('viewed_at')->take(5)->get(['artwork_id', 'viewed_at', 'session_hash']);
echo "Latest view events (any artwork): " . json_encode($latest) . PHP_EOL;

// Check Redis queue depth
try {
    $queueLen = Redis::llen('artwork_stats:deltas');
    echo "Redis artwork_stats:deltas queue length: {$queueLen}" . PHP_EOL;

    if ($queueLen > 0) {
        $peek = Redis::lrange('artwork_stats:deltas', 0, 2);
        echo "First entries: " . json_encode($peek) . PHP_EOL;
    }
} catch (\Exception $e) {
    echo "Redis error: " . $e->getMessage() . PHP_EOL;
}

// Check artwork exists
$artwork = DB::table('artworks')->where('id', $artworkId)->first(['id', 'title', 'status', 'user_id']);
echo "Artwork: " . json_encode($artwork) . PHP_EOL;
