<?php
require_once __DIR__ . '/../src/Helpers/ContentCleaner.php';

$j = json_decode(file_get_contents(__DIR__ . '/../data/articles.json'), 1);

// Test on 3 different articles
$tests = [0, 12, 45];
foreach ($tests as $idx) {
    $a = $j[$idx];
    echo "\n========== {$a['title']} ==========\n";
    $cleaned = App\Helpers\ContentCleaner::clean($a['content']);
    echo $cleaned . "\n";
    echo "--- Length: " . strlen($cleaned) . " chars ---\n";
}

// Check for issues across all articles
echo "\n\n========== ISSUE SCAN ==========\n";
$missingFiles = [];
$brokenUrls = 0;
$totalImgs = 0;
foreach ($j as $a) {
    $cleaned = App\Helpers\ContentCleaner::clean($a['content']);
    
    // Count images
    preg_match_all('/<img[^>]+src="([^"]+)"/i', $cleaned, $imgs);
    $totalImgs += count($imgs[1]);
    
    foreach ($imgs[1] as $src) {
        // Check for broken URLs
        if (strpos($src, '/cozummerkezi/https://') !== false) {
            echo "BROKEN URL in '{$a['title']}': $src\n";
            $brokenUrls++;
        }
        // Check if local file exists
        elseif (strpos($src, '/cozummerkezi/assets/images/') !== false) {
            $f = basename($src);
            $path = __DIR__ . '/../public/assets/images/' . $f;
            if (!file_exists($path)) {
                $missingFiles[$f] = $a['title'];
            }
        }
    }
    
    // Check empty content
    if (empty(trim($cleaned))) {
        echo "EMPTY CONTENT: {$a['title']}\n";
    }
}

echo "\nTotal images found: $totalImgs\n";
echo "Broken URLs: $brokenUrls\n";
echo "Missing files: " . count($missingFiles) . "\n";
foreach ($missingFiles as $f => $title) {
    echo "  $f (in: $title)\n";
}
