<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AppNotification;
use Illuminate\Http\JsonResponse;

class NotificationFeedController extends Controller
{
    public function unread(): JsonResponse
    {
        $items = AppNotification::where('user_id', auth()->id())
            ->whereNull('read_at')
            ->latest()
            ->take(10)
            ->get(['id', 'title', 'message', 'channel', 'created_at']);

        return response()->json([
            'count' => $items->count(),
            'items' => $items,
        ]);
    }

    public function markRead(AppNotification $notification): JsonResponse
    {
        abort_unless($notification->user_id === auth()->id(), 403);
        $notification->update(['read_at' => now()]);

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