<?php

namespace App\Console\Commands;

use App\Models\Listing;
use App\Models\ZListing;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class MigrateListing extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migrate:listing';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // $olisting = ZListing::where(['post_type' => 'property', 'post_status' => 'publish'])->get();
        // foreach ($olisting as $ol) {
        //     $listing = Listing::where('ref_id', $ol->ID)->first();
        //     if ($ol->thumbnail) {
        //         $thid = $ol->thumbnail->meta_value;
        //         $thLis = ZListing::where('ID', $thid)->first();
        //         if ($thLis) {
        //             if (str_contains($thLis->guid, 'luxrealty.co.uk')) {
        //                 $timageUrl = str_replace("luxrealty.co.uk", "luxrealty.ae", $thLis->guid);
        //             } else {
        //                 $timageUrl = $thLis->guid;
        //             }
        //             $tcontents = @file_get_contents($timageUrl);
        //             if ($tcontents === false) {
        //             } else {
        //                 $tinfo = pathinfo($timageUrl);
        //                 $tname = 'listing_' . rand(10000, 99999) . rand(1000, 9999);
        //                 $tname = $tname . '.' . $tinfo['extension'];
        //                 $tpath = 'storage/listings/' . $tname;
        //                 Storage::disk('local')->put($tpath, $tcontents);
        //                 $listing->update(['thumb_image' => $tpath]);
        //             }
        //         }
        //     }
        //     $items = [];
        //     foreach ($ol->images as $image) {
        //         $child = ZListing::where('ID', $image->meta_value)->first();
        //         if ($child && $child->guid) {
        //             $imageUrl = '';
        //             if (str_contains($child->guid, 'luxrealty.co.uk')) {
        //                 $imageUrl = str_replace("luxrealty.co.uk", "luxrealty.ae", $child->guid);
        //             } else {
        //                 $imageUrl = $child->guid;
        //             }
        //             $contents = @file_get_contents($imageUrl);
        //             if ($contents === false) {
        //             } else {
        //                 $info = pathinfo($imageUrl);
        //                 $name = 'listing_' . rand(10000, 99999) . rand(1000, 9999);
        //                 $name = $name . '.' . $info['extension'];
        //                 $path = 'storage/listings/' . $name;
        //                 Storage::disk('local')->put($path, $contents);
        //                 $object = (object) ['sort_order' => 0, 'path' => $path];
        //                 $items[] = $object;
        //             }
        //         }
        //     }
        //     $listing->update(['gallery' => json_encode($items)]);
        // }
    }
}
