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

use App\Models\ArtworkComment;
use App\Services\ContentSanitizer;

// Test on a single comment first
$c = ArtworkComment::find(175742);
if (!$c) { echo "Comment not found\n"; exit(1); }

echo "raw_content: " . $c->raw_content . PHP_EOL;
echo "old rendered: " . $c->rendered_content . PHP_EOL;

$new = ContentSanitizer::render($c->raw_content);
echo "new rendered: " . $new . PHP_EOL;

// Now re-render all
$count = 0;
$errors = 0;
ArtworkComment::whereNotNull('raw_content')
    ->where('raw_content', '!=', '')
    ->chunk(100, function ($comments) use (&$count, &$errors) {
        foreach ($comments as $c) {
            try {
                $c->rendered_content = ContentSanitizer::render($c->raw_content);
                $c->timestamps = false;
                $c->saveQuietly();
                $count++;
            } catch (\Throwable $e) {
                $errors++;
                echo "Error on comment #{$c->id}: " . $e->getMessage() . PHP_EOL;
            }
        }
    });

echo "Re-rendered {$count} comments ({$errors} errors)." . PHP_EOL;
