<?php

declare(strict_types=1);

namespace App\Http\Controllers\RSS;

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

/**
 * BlogFeedController
 *
 * GET /rss/blog → latest blog posts feed (spec §3.6)
 */
final class BlogFeedController extends Controller
{
    public function __construct(private readonly RSSFeedBuilder $builder) {}

    public function __invoke(): Response
    {
        $feedUrl = url('/rss/blog');
        $posts   = Cache::remember('rss:blog', 600, fn () =>
            BlogPost::published()
                ->with('author:id,username')
                ->latest('published_at')
                ->limit(RSSFeedBuilder::FEED_LIMIT)
                ->get()
        );

        return $this->builder->buildFromBlogPosts(
            'Blog',
            'Latest posts from the Skinbase blog.',
            $feedUrl,
            $posts,
        );
    }
}
