<?php

declare(strict_types=1);

namespace App\Services\Sitemaps\Builders;

use App\Models\WorldWebStory;
use App\Services\Sitemaps\AbstractSitemapBuilder;
use App\Services\Sitemaps\SitemapUrlBuilder;
use DateTimeInterface;

final class WorldWebStoriesSitemapBuilder extends AbstractSitemapBuilder
{
    public function __construct(private readonly SitemapUrlBuilder $urls)
    {
    }

    public function name(): string
    {
        return 'web-stories';
    }

    public function items(): array
    {
        return WorldWebStory::query()
            ->visible()
            ->with('world')
            ->orderByDesc('published_at')
            ->orderByDesc('id')
            ->get()
            ->map(fn (WorldWebStory $story) => $this->urls->webStory($story))
            ->filter()
            ->values()
            ->all();
    }

    public function lastModified(): ?DateTimeInterface
    {
        return $this->dateTime(WorldWebStory::query()->visible()->max('updated_at'));
    }
}