<?php

declare(strict_types=1);

namespace App\Services\Sitemaps\Builders;

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

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

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

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

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

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

        return array_merge($items, $details);
    }

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