<?php

namespace App\Http\Controllers;

use App\Models\Notification;
use App\Services\NotificationService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
    public function __construct(
        private readonly NotificationService $service,
    ) {}

    public function markAllRead(Request $request): RedirectResponse
    {
        $this->service->markAllAsRead($request->user());

        return back();
    }

    public function destroyAll(Request $request): RedirectResponse
    {
        $this->service->deleteAll($request->user());

        return back();
    }

    public function markRead(Request $request, Notification $notification): RedirectResponse
    {
        abort_unless($notification->user_id === $request->user()->id, 403);

        $this->service->markAsRead($notification);

        return back();
    }

    public function destroy(Request $request, Notification $notification): RedirectResponse
    {
        abort_unless($notification->user_id === $request->user()->id, 403);

        $this->service->delete($notification);

        return back();
    }
}
