<?php

namespace App\Modules\HR\EmployeeSelfService\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Dashboard\Services\NotificationCenterService;
use App\Modules\HR\EmployeeSelfService\Requests\MeNotificationIndexRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\View\View;

class MeNotificationController extends Controller
{
    public function __construct(
        private readonly NotificationCenterService $notifications,
    ) {}

    public function index(MeNotificationIndexRequest $request): View
    {
        $user = $request->user();
        $categoryFilter = $request->filterCategory();
        $paginator = $this->notifications->paginate($user, $categoryFilter);
        $activeType = $request->input('type', 'all');

        return view('pages.me.notifications.index', compact('paginator', 'activeType'));
    }

    public function markRead(Request $request, string $id): RedirectResponse
    {
        if (! Str::isUuid($id)) {
            abort(404);
        }
        $ok = $this->notifications->markRead($request->user(), $id);
        if (! $ok) {
            abort(404);
        }

        return back()->with('status', __('notifications.marked_read'));
    }

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

        return back()->with('status', __('notifications.marked_all_read'));
    }
}
