<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\UserNotify;

class NotifyController extends Controller
{
    const ITEM_PER_PAGE = 30;
    private $_user = null;

    public function __construct()
    {
        $this->_user = auth('api')->user();
    }

    public function all()
    {
        $notifyCount = UserNotify::where('user_id', $this->_user->id)->where('is_check', false)->count();
        $notifyTop = UserNotify::select('*')->where('user_id', $this->_user->id)->orderBy('created_at', 'DESC')->paginate(static::ITEM_PER_PAGE);

        return response()->json(['data' => [
            'all'     => $notifyCount,
            'list'    => $notifyTop
        ]], 200);
    }

    public function update()
    {
        UserNotify::where('user_id', $this->_user->id)->where('is_check', false)->update(['is_check' => 1]);
        return response()->json(null, 204);
    }

    public function mark($id = 0)
    {
        UserNotify::where('user_id', $this->_user->id)->where('id', $id)->update(['is_check' => 1, 'is_read' => 1]);
        $notifyTop = UserNotify::select('*')->where('user_id', $this->_user->id)->orderBy('created_at', 'DESC')->paginate(static::ITEM_PER_PAGE);

        return response()->json(['data' => ['list' => $notifyTop]], 200);
    }

    public function destroy()
    {
        UserNotify::where('user_id', $this->_user->id)->delete();
        $notifyTop = UserNotify::select('*')->where('user_id', $this->_user->id)->orderBy('created_at', 'DESC')->paginate(static::ITEM_PER_PAGE);

        return response()->json(['data' => [
            'all'     => 0,
            'contact' => 0,
            'list'    => $notifyTop
        ]], 200);
    }
}
