<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Transformers\Users\NotificationTransformer;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Notifications\DatabaseNotification;

class NotificationsController extends Controller {
    public function __construct() {
    }

    public function list(Request $request): JsonResponse {
        $result = fractal($request->user()->notifications, new NotificationTransformer())->respond();
        $request->user()->notifications()->whereNull('read_at')->update(['read_at' => now()]);
        return $result;
    }

    public function count(Request $request): JsonResponse {
        return response()->json(['count' => $request->user()->unreadNotifications()->count()]);
    }

    public function show(Request $request, $uuid): JsonResponse {
        $notification = DatabaseNotification::where('id', $uuid)->first();
        $notification->markAsRead();
        return fractal($notification, new NotificationTransformer())->respond();
    }

}
