<?php

namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use App\Notification;

class NotificationController extends Controller
{
	
	public function index(Request $request)
	{
		return response()->json($this->_getEmployee()->getNotifications(), 200);
	}

	public function read(Request $request)
    {
		
		$notification = Notification::find($request->notification);

        if (!$notification || $notification->company_id != $this->_getCompany()->id) {
            return response()->json([
				'title' => 'Notification',
                'message' => 'Record not found',
            ], 404);
        }
		
		$read_by = $notification->read_by;
		
		if (!$read_by || !in_array($this->_getEmployee()->id, $read_by)) {
			
			$read_by[] = $this->_getEmployee()->id;
			$notification->read_by = $read_by;
			$notification->updated_at = date('Y-m-d H:i:s');
			$notification->save();
			
		}
		
		return response()->json([
				'title' => 'Notification',
				'message' => 'Notification was read successfully.',
				'object' => $notification
			], 200);
	
    }

}
