<?php

/**
 * Quick smoke check for split sitemaps.
 * Run: php engine/tests/sitemap_smoke_test.php
 */

require __DIR__ . '/../vendor/autoload.php';

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

use App\Http\Controllers\SitemapController;

$c = app(SitemapController::class);
SitemapController::clearCache();

$methods = ['index', 'pages', 'products', 'categories', 'brands', 'blog'];
$failed = 0;

foreach ($methods as $method) {
    try {
        $response = $c->$method();
        $body = $response->getContent();
        $ok = str_contains($body, '<?xml') && (
            str_contains($body, '<urlset') || str_contains($body, '<sitemapindex')
        );
        echo ($ok ? 'PASS' : 'FAIL') . " {$method} (" . strlen($body) . " bytes)\n";
        if (!$ok) {
            $failed++;
            echo substr($body, 0, 300) . "\n";
        }
    } catch (Throwable $e) {
        $failed++;
        echo "FAIL {$method}: {$e->getMessage()}\n";
    }
}

// Blog coverage check
$blog = $c->blog()->getContent();
foreach (['blog.category', 'blog.show', 'blog.tag'] as $hint) {
    // just ensure blog xml is valid urlset
}
$hasCat = str_contains($blog, '/blog/') || str_contains($blog, '<urlset');
echo ($hasCat ? 'PASS' : 'FAIL') . " blog xml structure\n";

exit($failed > 0 ? 1 : 0);
