<?php

namespace App\Http\Controllers;

use App\Models\AdminNotification;
use App\Services\NotificationManagementService;
use Illuminate\Http\Request;

class NotificationManagementController extends Controller
{
    public function __construct(private NotificationManagementService $service) {}

    public function index(Request $request)
    {
        return view('admin.notifications', $this->service->page($request->user()->id));
    }

    public function read(Request $request, AdminNotification $notification)
    {
        $this->service->markRead($notification, $request->user()->id);
        if (($notification->data['type'] ?? null) === 'professional_application_submitted') {
            return redirect()->route('applications.show', $notification->data['professional_id']);
        }

        return back();
    }

    public function readAll(Request $request)
    {
        $this->service->markAllRead($request->user()->id);

        return back()->with('success', 'All notifications marked as read.');
    }

    public function destroy(Request $request, AdminNotification $notification)
    {
        $this->service->delete($notification, $request->user()->id);

        return back()->with('success', 'Notification deleted.');
    }

    public function clear(Request $request)
    {
        $this->service->clear($request->user()->id);

        return back()->with('success', 'Notifications cleared.');
    }
}
