<?php

namespace App\Http\Controllers;

use App\Models\News;
use Illuminate\Http\Request;

class NewsPageController extends Controller
{
    public function index()
    {
        $allNews = News::query()->with('author')->latest()->get();

        return view('web.pages.news.index', compact('allNews'));
    }

    public function show($news)
    {
        $news = News::where('slug', $news)->with('author', 'comments')->first();
        $recentNews = News::query()->with('author')->latest()->limit(3)->get();

        return view('web.pages.news.show', compact('news', 'recentNews'));
    }

    public function storeComment(Request $request, News $news)
    {
        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'message' => 'required|string',
            'author_name' => 'nullable|string|max:255',
        ]);

        $news->comments()->create($validated);

        return back()->with('success', 'Comment added successfully!');
    }
}
