<?php

namespace App\Console\Commands\PropertyWebMenuMapping;

use App\Core\Service\PropertyWebService;
use App\Exceptions\ApiErrorException;
use App\Models\PropertyWeb;
use App\Models\PropertyWebMenu;
use App\Models\PropertyWebMenuMapping;
use Illuminate\Console\Command;
use Exception;
use Illuminate\Support\Facades\Log;


class PropertyWebMenuMappingConsole extends Command
{

    protected $signature = 'cron:property-web-menu-mapping';

    protected $description = '';


    public function __construct(){
        parent::__construct();
    }

    public function handle()
    {

        try {

            $this->info(date('Y-m-d H:i:s') . ' : ' . date('Y-m-d') . ' Start');

            $webMenus = PropertyWebMenu::all();
            $webs = PropertyWeb::all();

            $datas = [];
            foreach ($webs as $key => $web){
                foreach ($webMenus as $key1 => $menu){
                    $datas[$key][$key1]['property_id'] = $web->property_id;
                    $datas[$key][$key1]['property_web_id'] = $web->id;
                    $datas[$key][$key1]['property_web_menu_id'] = $menu->id;
                    $datas[$key][$key1]['order_number'] = $menu->order_number;
                    $datas[$key][$key1]['status'] = 1;
                    $datas[$key][$key1]['created_by'] = 1;
                    $datas[$key][$key1]['updated_by'] = 1;
                    $datas[$key][$key1]['created_at'] = 1;
                    $datas[$key][$key1]['updated_at'] = 1;
                }
            }

            PropertyWebMenuMapping::insert(collect($datas)->flatten(1)->toArray());

            $this->info(date('Y-m-d H:i:s') . ' : ' . date('Y-m-d') . ' Finished');

        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $this->error(date('Y-m-d H:i:s') . ' : ' . date('Y-m-d') . ' ERROR: ' . $message);
        }

    }

}
