<?php

declare(strict_types=1);

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Story;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\View\View;

/**
 * Stories listing page — /stories
 */
final class StoriesController extends Controller
{
    public function index(Request $request): View
    {
        $featured = Cache::remember('stories:featured', 300, fn () =>
            Story::published()->featured()
                ->with('author', 'tags')
                ->orderByDesc('published_at')
                ->first()
        );

        $stories = Cache::remember('stories:list:page:' . ($request->get('page', 1)), 300, fn () =>
            Story::published()
                ->with('author', 'tags')
                ->orderByDesc('published_at')
                ->paginate(12)
                ->withQueryString()
        );

        return view('web.stories.index', [
            'featured'              => $featured,
            'stories'               => $stories,
            'page_title'            => 'Stories — Skinbase',
            'page_meta_description' => 'Artist interviews, community spotlights, tutorials and announcements from Skinbase.',
            'page_canonical'        => url('/stories'),
            'page_robots'           => 'index,follow',
            'breadcrumbs'           => collect([
                (object) ['name' => 'Stories', 'url' => '/stories'],
            ]),
        ]);
    }
}
