<?php

declare(strict_types=1);

use App\Services\Sitemaps\SitemapReleaseManager;
use Illuminate\Support\Facades\Storage;

it('reports the latest sitemap build timestamp', function (): void {
    Storage::fake('local');

    $releaseId = '20260511123000-manual-release';
    $builtAt = now()->subHours(2)->toAtomString();

    Storage::disk('local')->put(
        "sitemaps/releases/{$releaseId}/manifest.json",
        json_encode([
            'release_id' => $releaseId,
            'status' => 'published',
            'built_at' => $builtAt,
            'published_at' => now()->toAtomString(),
            'families' => [],
            'documents' => [],
            'totals' => [
                'families' => 0,
                'documents' => 0,
                'urls' => 0,
            ],
        ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
    );

    $releases = app(SitemapReleaseManager::class)->listReleases();

    expect($releases)
        ->toHaveCount(1)
        ->and($releases[0]['release_id'] ?? null)->toBe($releaseId)
        ->and($releases[0]['built_at'] ?? null)->toBe($builtAt);

    $this->artisan('health:check', ['--only' => 'sitemap', '--json' => true])
        ->assertSuccessful();
});