<?php

namespace App\Jobs;

use App\Models_v2\Conversation;
use App\Models_v2\FindSession;
use App\Notifications\SendToOwnerWhenOtherInteractRenter;
use App\Notifications\SendToRenterWhenOtherInteract;
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 SendWhenOtherInteract implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

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

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        $conversationId = $this->conversationId;
        $conversation = Conversation::find($conversationId);

        $users = $conversation->users;
        if (!$users) {
            return;
        }

        if (!$conversation) {
            return;
        }

        if (!$conversation->is_find) {
            return;
        }

        $userOwner = $conversation->users()->where('type', User::OWNER)->first();
        $userRenter = $conversation->users()->where('type', User::RENTER)->first();
        $findSessions = null;

        //Bắn cho các chủ trọ mà thằng renter này có tương tác
        if ($userRenter) {
            $findSessions = FindSession::query()
                ->where('user_id', $userRenter->id)
                ->where('status', FindSession::SEARCHING)
                ->get();
        }

        if (!empty($findSessions)) {
            $conversations = Conversation::query()
                ->whereIn('find_session_id', $findSessions->pluck('id')->toArray())
                ->when(!empty($userOwner), function ($q) use ($userOwner) {
                    $q->whereHas('users', function ($q) use ($userOwner) {
                        $q->where('users.id', '<>', $userOwner->id);
                    });
                })
                ->get();
            $userConOwners = [];
            foreach ($conversations as $conversationItem) {
                $userCons = $conversationItem->users;
                foreach ($userCons as $userCon) {

                    if ($userCon->type == User::OWNER) {
                        $userConOwners[] = $userCon->id;
                    }
                }
            }

            $userConOwners = array_unique($userConOwners);

            foreach ($userConOwners as $userConOwner) {
                $userConItem = User::find($userConOwner);
                \Notification::send($userConItem, new
                SendToOwnerWhenOtherInteractRenter($userRenter->id));
            }
        }


        //bắn cho tất cả các khách thuê mà thằng chủ trọ có tương tác

        $conversations = Conversation::query()
            ->whereHas('users', function ($q) use ($userOwner) {
                $q->where('users.id', $userOwner->id);
            })
            ->where('id', '<>', $conversation->id)
            ->get();

        $userConRenters = [];
        foreach ($conversations as $conversationItem) {
            $users = $conversationItem->users;
            foreach ($users as $userItem) {
                if ($userItem->type == User::RENTER) {
                    $userConRenters[] = $userItem->id;
                }
            }
        }

        $userConsRenters = array_unique($userConRenters);
        if (!empty($userConsRenters)) {
            $userConRenterItems = User::query()->whereIn('id', $userConsRenters)->get();
            \Notification::send($userConRenterItems, new SendToRenterWhenOtherInteract());
        }


    }
}
