<?php

namespace App\Http\Controllers;

use App\Enums\Rubrique;
use App\Models\Article;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
    public function index(Request $request)
    {
        $query = Article::query()
            ->where('publie', true)
            ->latest('publie_le');

        $rubrique = $request->query('rubrique');
        if ($rubrique && $r = Rubrique::tryFrom($rubrique)) {
            $query->where('rubrique', $r);
        }

        $articles = $query->paginate(9)->withQueryString();

        return view('public.articles', compact('articles', 'rubrique'));
    }

    public function show(Article $article)
    {
        // Seuls les articles publiés sont visibles publiquement
        abort_unless($article->publie, 404);

        $article->increment('vues');

        return view('public.article', compact('article'));
    }
}