<?php

namespace App\Jobs;

use App\Models_v2\FindSession;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class RemindRenterSearchHostel implements ShouldQueue {
	use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

	/**
	 * Create a new job instance.
	 *
	 * @return void
	 */
	protected $userId;

	public function __construct( $userId ) {
		//
		$this->userId = $userId;
	}

	/**
	 * Execute the job.
	 *
	 * @return void
	 */
	public function handle() {
		//
		$userId           = $this->userId;
		$findSessionCount = FindSession::query()
		                               ->where( 'user_id', $userId )
		                               ->count();

		if ( $findSessionCount > 0 ) {
			return;
		}

		$user = User::find( $userId );
		if ( ! $user ) {
			return;
		}

		\Notification::send( $user, new \App\Notifications\RemindRenterSearchHostel() );
	}
}
