<?php

namespace App\Jobs;

use App\Components\Functions;
use App\Models\Hostel;
use App\Models\Notification;
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 RemindCreateRoom 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;
		$user   = User::find( $userId );
		if ( $user ) {
			if($user->type != User::OWNER)
			{
				return;
			}
			$cntHostel = Hostel::query()->where( 'owner_id', $userId )->count();
			if ( $cntHostel == 0 ) {
				Notification::create( [
					'to_user' => $userId,
					'title'   => 'Thông báo từ itro.vn',
					'user_id' => $userId,
					'content' => 'Dường như bạn chưa tạo nhà trọ dành cho bạn. Hãy tạo ngay để bắt đầu quản lý nhà trọ một cách hiệu quả. Nếu có thắc mắc khi tạo nhà trọ, hãy gọi 0868987355 để đươc trợ giúp'
				] );
			}

			Functions::remindCreateRoomJob($userId);

		}
	}
}
