<?php

declare(strict_types=1);

namespace App\Http\Controllers\RSS;

use App\Http\Controllers\Controller;
use App\Models\Artwork;
use App\Services\RSS\RSSFeedBuilder;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;

/**
 * GlobalFeedController
 *
 * GET /rss → global latest-artworks feed (spec §3.1)
 */
final class GlobalFeedController extends Controller
{
    public function __construct(private readonly RSSFeedBuilder $builder) {}

    public function __invoke(): Response
    {
        $feedUrl  = url('/rss');
        $artworks = Cache::remember('rss:global', 300, fn () =>
            Artwork::public()->published()
                ->with(['user:id,username', 'categories:id,name,slug,content_type_id'])
                ->latest('published_at')
                ->limit(RSSFeedBuilder::FEED_LIMIT)
                ->get()
        );

        return $this->builder->buildFromArtworks(
            'Latest Artworks',
            'The newest artworks published on Skinbase.',
            $feedUrl,
            $artworks,
        );
    }
}
