<?php

namespace App\Http\Controllers;
use App\SmNotification;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class SmNotificationController extends Controller{
	public function __construct(){
        $this->middleware('PM');
        // User::checkAuth();
	}

	public function viewSingleNotification($id){
		try {

			$notification = SmNotification::find($id);
			$notification->is_read = 1;
			$notification->save();
			$this->markDatabaseNotificationsAsRead($notification->user_id, $notification->message);

			return redirect()->back();
		}catch (\Exception $e) {
		   Toastr::error('Operation Failed', 'Failed');
		   return redirect()->back();
		}
    }


	public function viewAllNotification($id){
		try{
			$user = Auth()->user();
			if(Auth()->user()->role_id != 1){

				if($user->role_id == 2){
					SmNotification::where('user_id', Auth::user()->id)->where('role_id', 2)->update(['is_read' => 1]);
					Toastr::success('Operation successful', 'Success');
				}elseif($user->role_id == 3){
					SmNotification::where('user_id', Auth::user()->id)->where('role_id', '!=', 2)->update(['is_read' => 1]);
					Toastr::success('Operation successful', 'Success');
				}else{
					SmNotification::where('user_id', $user->id)->where('role_id', '!=', 2)->where('role_id', '!=', 3)->update(['is_read' => 1]);
					Toastr::success('Operation successful', 'Success');
				}
			}else{
				SmNotification::where('user_id', $user->id)->where('role_id', 1)->update(['is_read' => 1]);
					Toastr::success('Operation successful', 'Success');
			}
			$this->markDatabaseNotificationsAsRead($user->id);
			return redirect()->back();
		}catch (\Exception $e) {
		   Toastr::error('Operation Failed', 'Failed');
		   return redirect()->back();
		}
    }

    public function udpateNotification($id){
        try {
            $notification = SmNotification::find($id);
            $notification->is_read = 1;
            $notification->save();
            $this->markDatabaseNotificationsAsRead($notification->user_id, $notification->message);
            $url = @$notification->url ? url($notification->url) : url('view/single/notification/'.$notification->id);
            return redirect(url($url));
        } catch (\Throwable $th) {
            Toastr::error('Operation Failed', 'Failed');
            return redirect()->back();
        }
         return SmNotification::findOrFail($id);
    }
	
    public function viewNotice($id){
		Toastr::error('Operation Failed', 'Failed');
		return redirect()->back();
    }
	public function allNotification(){
		try {
			$notifications = SmNotification::where('role_id', auth()->user()->role_id)
				->where('user_id', auth()->user()->id)
				->where('is_read', 0)
				->where('active_status', 1)
				->paginate(10);
			return view('backEnd.notification_list.notification_list', compact('notifications'));
		} catch (\Throwable $th) {
			Toastr::error('Operation Failed', 'Failed');
			return redirect()->back();
		}
	}

	private function markDatabaseNotificationsAsRead($userId, $message = null)
	{
		if (!Schema::hasTable('notifications')) {
			return;
		}

		$query = DB::table('notifications')
			->where('notifiable_id', $userId)
			->whereNull('read_at');

		if ($message) {
			$query->where('data', 'like', '%' . addcslashes($message, '%_\\') . '%');
		}

		$query->update([
			'read_at' => now(),
			'updated_at' => now(),
		]);
	}
}
