<?php

namespace App\Jobs;

use App\Models\Conversation;
use App\Models\Hostel;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class DeleteHostelConversation
    implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

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

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

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        $hostelId = $this->hostelId;
        $hostel = Hostel::withTrashed()->find($hostelId);
        if (!$hostel) {
            return;
        }
        Conversation::where('hostel_id', $hostel->id)->delete();


        \App\Models_v2\Conversation::query()
            ->where('hostel_id', $hostel->id)
            ->where('is_find', false)
            ->delete();


        $rooms = $hostel->rooms->pluck('id')->toArray();

        \App\Models_v2\Conversation::query()
            ->whereIn('room_id', $rooms)
            ->where('is_find', false)
            ->delete();

    }
}
