<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\SaveNewsRequest;
use App\Models\News;
use App\Services\NewsService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class NewsController extends Controller
{
    public function index(NewsService $news): View
    {
        return view('admin.news.index', [
            'newsItems' => $news->listForAdmin(),
            'newsStats' => $news->stats(),
        ]);
    }

    public function store(SaveNewsRequest $request, NewsService $news): RedirectResponse
    {
        $news->create($request->validated(), $request->user(), $request->file('image'));

        return back()->with('status', 'Новость добавлена.');
    }

    public function update(
        News $newsItem,
        SaveNewsRequest $request,
        NewsService $news,
    ): RedirectResponse {
        $news->update($newsItem, $request->validated(), $request->file('image'));

        return back()->with('status', 'Новость обновлена.');
    }

    public function destroy(News $newsItem, NewsService $news): RedirectResponse
    {
        $news->delete($newsItem);

        return back()->with('status', 'Новость удалена.');
    }
}
