<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\UserNotification;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
    public function index(): View
    {
        $notifications = UserNotification::query()
            ->where('user_id', auth()->id())
            ->latest('id')
            ->paginate(20);

        return view('user.notifications', compact('notifications'));
    }

    public function markRead(Request $request): JsonResponse
    {
        $ids = $request->input('ids', []);

        $query = UserNotification::query()->where('user_id', auth()->id());
        if (! empty($ids)) {
            $query->whereIn('id', (array) $ids);
        }

        $query->update(['read_at' => now()]);

        return response()->json(['status' => 'ok']);
    }
}
