<?php

namespace App\Http\Controllers\Site;

use App\Http\Controllers\Controller;
use App\Models\NewsPost;
use App\Models\Page;
use Illuminate\Contracts\View\View;

class NewsController extends Controller
{
    public function index(): View
    {
        $page = Page::query()
            ->published()
            ->where('route_path', '/news')
            ->with('sections')
            ->first();

        $posts = NewsPost::query()
            ->with(['category', 'cover'])
            ->published()
            ->latest('published_at')
            ->paginate(12);

        return view('site.news.index', compact('page', 'posts'));
    }

    public function show(NewsPost $post): View
    {
        abort_unless($post->status === 'published' && $post->published_at && $post->published_at->isPast(), 404);

        return view('site.news.show', [
            'post' => $post,
        ]);
    }
}
