<?php

namespace App\Jobs;

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

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

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        $item = HostelPostCrawl::find($this->id);

        Hostel::query()
            ->updateOrCreate([
                'hostel_post_crawl_id' => $this->id,
            ], [
                'name' => $item->title,
                'phone' => $item->user_post_phone,
                'address' => $item->address,
                'lat' => $item->lat,
                'lng' => $item->lng,
                'province_id' => $item->province_id,
                'district_id' => $item->district_id,
                'ward_id' => $item->ward_id,
                'type' => $item->type,
                'desc' => $item->content,
                'images',
                'image',
                'owner_name' => $item->user_post,
                'owner_phone' => $item->user_post_phone,
                'source' => Hostel::SOURCE_CRAWLER,
                'hotline' => $item->user_post_phone
            ]);

    }
}
