<?php

namespace App\Http\Controllers\Player;

use App\Http\Controllers\Controller;
use App\Models\News;
use Inertia\Inertia;
use Inertia\Response;

/**
 * Public-facing news reading — successor to the legacy `view/main/news.php`
 * / `view/main/index.php` teaser list. Output goes through React/JSX, which
 * escapes text content by default, closing the stored-XSS hole the legacy
 * app had (raw `echo $news['n_title']` with no escaping at all).
 */
class NewsController extends Controller
{
    public function index(): Response
    {
        return Inertia::render('Public/News', [
            'news' => News::query()
                ->published()
                ->with('author:id,name')
                ->latest('published_at')
                ->paginate(15)
                ->withQueryString(),
        ]);
    }

    public function show(News $news): Response
    {
        abort_unless($news->published_at && $news->published_at->isPast(), 404);

        return Inertia::render('Public/NewsShow', [
            'news' => $news->load('author:id,name'),
        ]);
    }
}
