<?php

namespace App\Listeners;

use App\Components\Functions;
use App\Models\District;
use App\Models\Hostel;
use App\Models\SearchLog;
use App\Models\UserPackageFindHostel;
use App\Models_v2\FindSession;
use App\Notifications\SearchHostel;
use App\Notifications\SearchHostelManageApp;
use App\Notifications\SearchHostelRenter;
use App\User;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class HostelCreated implements ShouldQueue {
	/**
	 * Create the event listener.
	 *
	 * @return void
	 */
	public function __construct() {
		//
	}

	/**
	 * Handle the event.
	 *
	 * @param object $event
	 *
	 * @return void
	 */
	public function handle( $event ) {
		//
		$hostel = $event->hostel;

		$findSessions = FindSession::query()
		                           ->where( 'status', FindSession::SEARCHING )
		                           ->with( 'user' )
		                           ->when( ! empty( $hostel->province_id ), function ( $q ) use ( $hostel ) {
			                           $q->where( 'province_id', $hostel->province_id );
		                           } )->when( ! empty( $hostel->district_id ), function ( $q ) use ( $hostel ) {
				$q->whereHas( 'districts', function ( $q ) use ( $hostel ) {
					$q->where( 'districtid', $hostel->district_id );
				} );
			} )->when( ! empty( $hostel->min_price ), function ( $q ) use ( $hostel ) {
				$q->where( 'min_price', '>=', $hostel->min_price );
			} )->when( ! empty( $hostel->max_price ), function ( $q ) use ( $hostel ) {
				$q->where( 'max_price', '<=', $hostel->max_price );
			} )->when( ! empty( $hostel->type ), function ( $q ) use ( $hostel ) {
				$q->where( 'type', $hostel->type );
			} )->when( ! empty( $hostel->type_rent ), function ( $q ) use ( $hostel ) {
				$q->where( 'type_rent', $hostel->type_rent );
			} )
		                           ->get();

		$owner = $hostel->owner;
		if ( ! $owner ) {
			return;
		}

		$isVip    = false;
		$checkVip = UserPackageFindHostel::query()
		                                 ->where( 'user_id', $owner->id )
		                                 ->where( 'package_id', 3 )
		                                 ->first();

		if ( $checkVip ) {
			$isVip = true;
		}

		foreach ( $findSessions as $findSession ) {
			$user = $findSession->user;
			if ( ! $user ) {
				continue;
			}

			$provinceId    = $findSession->province_id;
			$districtId    = $findSession->districts()->pluck( 'districtid' )->toArray();
			$districtsName = null;
			$addressName   = null;
			if ( is_array( $districtId ) ) {
				$districts     = District::query()
				                         ->whereIn( 'districtid', $districtId )
				                         ->pluck( 'name' )
				                         ->toArray();
				$districtsName = implode( ', ', $districts );
			}
			if ( $isVip ) {
				$itroStaffs = User::query()->where( 'is_agent', true )
				                  ->when( ! empty( $districtId ), function ( $q ) use ( $districtId ) {
					                  $q->whereHas( 'agentDistricts', function ( $q ) use ( $districtId ) {
						                  $q->whereIn( 'districtid', $districtId );
					                  } );
				                  } )
				                  ->get();
				\Notification::send( $itroStaffs, new SearchHostel(
					$user,
					$districtsName,
					$findSession->id,
					$provinceId,
					$districtId
				) );

				\Notification::send( $itroStaffs, new SearchHostelManageApp(
					$user,
					$districtsName,
					$findSession->id,
					$provinceId,
					$districtId
				) );

			} else {
// bắn cho chủ trọ
				\Notification::send( $owner, new SearchHostel(
					$user,
					$districtsName,
					$findSession->id,
					$provinceId,
					$districtId
				) );

				\Notification::send( $owner, new SearchHostelManageApp(
					$user,
					$districtsName,
					$findSession->id,
					$provinceId,
					$districtId
				) );

				//bắn cho nhân viên của từng nhà trọ

				$staffs = $hostel->staffs;
				if ( ! empty( $staffs ) ) {
					\Notification::send( $staffs, new SearchHostel(
						$user,
						$districtsName,
						$findSession->id,
						$provinceId,
						$districtId
					) );

					\Notification::send( $staffs, new SearchHostelManageApp(
						$user,
						$districtsName,
						$findSession->id,
						$provinceId,
						$districtId
					) );
				}
			}
		}

	}
}
