<?php

namespace App\Jobs;

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 SendNotificationAdmin implements ShouldQueue {
	use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

	/**
	 * Create a new job instance.
	 *
	 * @return void
	 */
	protected $to;
	protected $title;
	protected $content;

	public function __construct( $to, $title, $content ) {
		//
		$this->to      = $to;
		$this->title   = $title;
		$this->content = $content;
	}

	/**
	 * Execute the job.
	 *
	 * @return void
	 */
	public function handle() {
		//
		$to      = $this->to;
		$title   = $this->title;
		$content = html_entity_decode(strip_tags($this->content));

		$accounts = null;

		if ( $to == 'RENTER' ) {

			$accounts = User::where( 'type', User::RENTER )->get();
		} else if ( $to == 'OWNER' ) {
			$accounts = User::where( 'type', User::OWNER )->get();
		} else {
			$accounts = User::all();
		}

		foreach ( $accounts as $account ) {
			Notification::create( [
				'title'   => $title,
				'content' => $content,
				'to_user' => $account->id,
				'user_id' => $account->id
			] );
		}
	}
}
