<?php

declare(strict_types=1);

namespace App\Services\Sitemaps\Builders;

use App\Models\AcademyPromptTemplate;
use App\Services\Sitemaps\AbstractSitemapBuilder;
use App\Services\Sitemaps\SitemapUrl;
use App\Services\Sitemaps\SitemapUrlBuilder;
use DateTimeInterface;

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

    public function name(): string
    {
        return 'academy-prompts';
    }

    public function items(): array
    {
        if (! (bool) config('academy.enabled', true)) {
            return [];
        }

        $items = [$this->urls->staticRoute('/academy/prompts')];

        $details = AcademyPromptTemplate::query()
            ->active()
            ->published()
            ->orderBy('id')
            ->cursor()
            ->map(fn (AcademyPromptTemplate $prompt): SitemapUrl => $this->urls->staticRoute('/academy/prompts/' . $prompt->slug, $prompt->updated_at ?? $prompt->published_at))
            ->values()
            ->all();

        return array_merge($items, $details);
    }

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