<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\RoadObserved;

class UpdateRoadObserved extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'webservice:update-roadobserved';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update road observed table (province_fa, city_fa)';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $ids = RoadObserved::whereNotNull('province_id')
                           ->whereNotNull('city_id')
                           ->whereNull('province_fa')
                           ->whereNull('city_fa')
                           ->select('id')
                           ->get()
                           ->pluck('id');


        foreach ($ids as $id) {

            \DB::update("UPDATE road_observeds set province_fa = (select name_fa from provinces where id = (select province_id from road_observeds where id = ?)),
            city_fa = (select name_fa from cities where id = (select city_id from road_observeds where id = ?)) where id = ?", [$id, $id, $id]);
        }

        echo 'done at: '.date("Y-m-d H:i:s")."\n";

    }
}
