<?php

namespace App\Console\Commands;

use App\Components\Functions;
use Illuminate\Console\Command;

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

    /**
     * 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 mixed
     */
    public function handle()
    {
        //
        $districts = \DB::table('district')->get();

        foreach ($districts as $district)
        {
            $name = $district->type.' '.$district->name;
            $coor = Functions::geocode($name);
            \DB::table('district')->where('districtid', $district->districtid)->update([
                'lat' => $coor['lat'],
                'lng' => $coor['lng'],
            ]);
            $this->line('Done with '.$district->name);
        }

        $this->line('-------------------------------');

        $wards = \DB::table('ward')->get();

        foreach ($wards as $ward)
        {

            $name = $ward->type.' '.$ward->name;
            $coor = Functions::geocode($name);
            \DB::table('ward')->where('wardid', $ward->wardid)->update([
                'lat' => $coor['lat'],
                'lng' => $coor['lng'],
            ]);
            $this->line('Done with '.$ward->name);
        }
    }
}
