<?php

namespace App\Http\Controllers;

use App\Models\Article;

class ArticleController extends Controller
{
    public function index()
    {
        $articles = Article::published()
            ->latest('published_at')
            ->paginate(7);

        $featured = $articles->getCollection()->firstWhere('featured', true)
            ?? $articles->getCollection()->first();

        return view('pages.news.index', [
            'articles' => $articles,
            'featured' => $featured,
        ]);
    }

    public function show(Article $article)
    {
        abort_unless($article->published, 404);

        $more = Article::published()
            ->where('id', '!=', $article->id)
            ->latest('published_at')
            ->take(3)
            ->get();

        return view('pages.news.show', [
            'article' => $article,
            'more'    => $more,
        ]);
    }
}
