<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Notification;
use Illuminate\Support\Facades\DB;
use Auth;

class NotificationController extends Controller
{
    public function index()
    {
        return Notification::all()->orderby('updated_at','desc');
    }

    public function show(Notification $notification)
    {
     	return $notification;
    }

    public function store(Request $request)
    {
        $notification = Notification::create($request->all());

        return response()->json($notification, 201);
    }

    public function update(Request $request, Notification $notification)
    {
        $notification->update($request->all());

        return response()->json($notification, 200);
    }

    public function delete(Notification $notification)
    {
        $notification->delete();

        return response()->json(null, 204);
    }

    public function getUserMessage()
    {
        $user_all_notification = Notification::Where("user_id",Auth::user()->id)
            ->orderby('updated_at','desc')
            ->paginate(5);
        $user_unread_notification = Notification::Where([["user_id",Auth::user()->id],["is_read",0]])
            ->orderby('updated_at','desc')
            ->get();

    	return response()->json([
    		"message"			=>$user_all_notification,
			"unread_message"	=>$user_unread_notification
		],200);
    }

    public function readNotification($notification_id){
    	$affected = DB::table('notifications')
            ->Where('id', $notification_id)
            ->update(array('is_read' => 1));
    	return response()->json("Read message", 200);
    }


}