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

$j = json_decode(file_get_contents(__DIR__ . '/../data/articles.json'), 1);
$errors = [];
$totalImgs = 0; $externalImgs = 0; $localImgs = 0; $broken = 0; $missing = 0;

foreach ($j as $a) {
    $c = App\Helpers\ContentCleaner::clean($a['content']);

    if (empty(trim($c))) {
        $errors[] = "EMPTY: {$a['title']}";
        continue;
    }

    // Check for remaining WYSIWYG artifacts
    if (preg_match('/wysiwyg-canvas|position:\s*absolute|left:\s*\d+px/', $c)) {
        $errors[] = "WYSIWYG residue: {$a['title']}";
    }

    // Check images
    preg_match_all('/<img[^>]+src="([^"]+)"/i', $c, $imgs);
    $totalImgs += count($imgs[1]);
    foreach ($imgs[1] as $src) {
        if (strpos($src, '/cozummerkezi/https://') !== false) {
            $errors[] = "BROKEN: {$a['title']} - $src";
            $broken++;
        } elseif (strpos($src, 'https://printerservis.net/') !== false) {
            $externalImgs++;
        } elseif (strpos($src, '/cozummerkezi/assets/images/') !== false) {
            $localImgs++;
            $f = basename($src);
            if (!file_exists(__DIR__ . '/../public/assets/images/' . $f)) {
                $errors[] = "MISSING: {$a['title']} - $f";
                $missing++;
            }
        }
    }

    // Check for leftover <picture> tags
    if (preg_match('/<picture/i', $c)) {
        $errors[] = "PICTURE TAG: {$a['title']}";
    }

    // Check for empty <a> tags
    if (preg_match('/<a[^>]*>\s*<\/a>/i', $c)) {
        $errors[] = "EMPTY LINK: {$a['title']}";
    }
}

echo "=== COMPREHENSIVE TEST ===\n";
echo "Articles: " . count($j) . "\n";
echo "Total images: $totalImgs\n";
echo "  Local: $localImgs\n";
echo "  External: $externalImgs\n";
echo "  Broken URLs: $broken\n";
echo "  Missing files: $missing\n";
echo "Errors: " . count($errors) . "\n";

if ($errors) {
    echo "\nIssues:\n";
    foreach ($errors as $e) echo "  $e\n";
} else {
    echo "\nAll clean!\n";
}

// Image count on disk
$diskFiles = glob(__DIR__ . '/../public/assets/images/*.webp');
echo "\nImages on disk: " . count($diskFiles) . "\n";
