<?php

namespace App\Jobs;

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

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

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

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        $hostelId = $this->hostelId;
        $hostel = Hostel::find($hostelId);
        if (!$hostel) {
            return;
        }

        if ($hostel->source != Hostel::SOURCE_CRAWLER) {
            return;
        }
        HostelPostCrawl::query()
            ->updateOrCreate([
                'post_id' => $hostel->id,
                'hostel_id' => $hostel->id
            ], [
                'user_post' => optional($hostel->owner)->name,
                'user_post_image' => !empty(optional($hostel->owner)->image) ? 'https://itro.vn/' . optional($hostel->owner)->image : null,
                'user_post_phone' => optional($hostel->owner)->phone,
                'content' => $hostel->desc,
                'province_id' => $hostel->province_id,
                'district_id' => $hostel->district_id,
                'ward_id' => $hostel->ward_id,
                'address' => $hostel->address,
                'post_id' => $hostel->id,
                'from' => HostelPostCrawl::FROM_USER_ITRO_POST,
                'type' => $hostel->type,
                'hostel_id' => $hostel->id,
                'lat' => $hostel->lat,
                'lng' => $hostel->lng,
                'title' => $hostel->name,
                'price' => $hostel->min_price,
                'price_min' => $hostel->min_price,
                'price_max' => $hostel->max_price
            ]);

        $item = HostelPostCrawl::query()
            ->where('post_id', $hostel->id)
            ->where('hostel_id', $hostel->id)
            ->first();
        if ($item) {
            $images = HostelImage::query()
                ->where('hostel_id', $hostel->id)
                ->get();
            if (!empty($images->count())) {
                $images = $images
                    ->map(function ($image) {
                        return 'https://itro.vn/files/' . $image->image;
                    })
                    ->toArray();
                HostelPostCrawlMedia::query()
                    ->where('hostel_post_crawl_id', $item->id)
                    ->delete();

                if (!empty($images)) {
                    foreach ($images as $media) {
                        HostelPostCrawlMedia::create([
                            'hostel_post_crawl_id' => $item->id,
                            'media' => $media
                        ]);
                    }
                }
            }
        }
    }
}
