<?php

declare(strict_types=1);

namespace App\Services\Sitemaps;

use Illuminate\Http\Response;

final class SitemapXmlRenderer
{
    /**
     * @param  list<SitemapIndexItem>  $items
     */
    public function renderIndex(array $items): string
    {
        return \view('sitemaps.index', [
            'items' => $items,
        ])->render();
    }

    /**
     * @param  list<SitemapUrl>  $items
     */
    public function renderUrlset(array $items): string
    {
        return \view('sitemaps.urlset', [
            'items' => $items,
            'hasImages' => \collect($items)->contains(fn (SitemapUrl $item): bool => $item->images !== []),
        ])->render();
    }

    /**
     * @param  list<GoogleNewsSitemapUrl>  $items
     */
    public function renderGoogleNewsUrlset(array $items): string
    {
        return \view('sitemaps.news-urlset', [
            'items' => $items,
        ])->render();
    }

    public function xmlResponse(string $content): Response
    {
        return \response($content, 200, [
            'Content-Type' => 'application/xml; charset=UTF-8',
            'Cache-Control' => 'public, max-age=' . max(60, (int) \config('sitemaps.cache_ttl_seconds', 900)),
        ]);
    }
}