<?php
require __DIR__ . '/vendor/autoload.php';
use App\Pix\PixStatusStorage;

$dir = __DIR__ . '/storage';
$files = is_dir($dir) ? scandir($dir) : [];
$items = [];
foreach ($files as $file) {
    if ($file === '.' || $file === '..') {
        continue;
    }
    $path = $dir . '/' . $file;
    if (is_file($path)) {
        $items[] = [
            'file' => $file,
            'content' => json_decode(file_get_contents($path), true),
        ];
    }
}

$logFile = __DIR__ . '/storage/webhook-log.jsonl';
$logEntries = [];
if (is_file($logFile)) {
    $lines = file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach ($lines as $line) {
        $decoded = json_decode($line, true);
        if (is_array($decoded)) {
            $logEntries[] = $decoded;
        }
    }
}

header('Content-Type: application/json');
echo json_encode([
    'count' => count($items),
    'items' => $items,
    'webhook_log_count' => count($logEntries),
    'webhook_log' => array_slice($logEntries, -10),
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
