<?php

namespace App\Jobs;

use App\Models\Hostel;
use App\Models\HostelRating;
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 RemindRating implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected  $userId;
    protected $hostelId;
    public function __construct($userId, $hostelId)
    {
        //
	    $this->userId = $userId;
	    $this->hostelId = $hostelId;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
	    $userId = $this->userId;
	    $hostel = Hostel::find($this->hostelId);
	    if($hostel) {
		    $user = User::find( $userId );
		    if ( $user ) {
			    $cntHostel = HostelRating::where( 'user_id', $userId )->where( 'hostel_id', $this->hostelId )->count();
			    if ( $cntHostel == 0 ) {
				    Notification::create( [
					    'to_user' => $userId,
					    'title'   => 'Thông báo từ itro.vn',
					    'user_id' => $userId,
					    'content' => 'Đã được 1 tháng bạn ở nhà trọ '.$hostel->name.'. Bạn có đánh giá như thế nào về nhà trọ này ? Hãy đánh giá nhà trọ để giúp mọi người biết thêm thông tin về nhà trọ nhé'
				    ] );
			    }

		    }
	    }
    }
}
