<?php

namespace App\Repositories;

use App\Models\City;
use Illuminate\Support\Facades\DB;

class Cities
{
    public static function getCitiesByName($query)
    {
        return City::select('id', DB::raw("concat(nom,' (',code_postal,')') as text"))->where('nom', 'LIKE', "$query%")->orderBy('nom', 'ASC')->take(30)->get();
    }

    public static function getCitiesByCP($query)
    {
        return City::select('id', DB::raw("concat(nom,' (',code_postal,')') as text"))->where('code_postal', 'LIKE', "%q$guery%")->orderBy('nom', 'ASC')->take(30)->get();
    }

    public static function getCPByCity($id)
    {
        $ville = self::get($id);
        $codes = explode('-', $ville->code_postal);

        return $codes;
    }

    public static function getNomByCity($id)
    {
        $ville = self::get($id);

        return $ville->nom;
    }

    public static function get($id)
    {
        return City::find($id);
    }

    public static function getCoords($adresse)
    {
        // dd(app('geocoder')->geocode('Los Angeles, CA')->get());
        // dd(app('geocoder')->geocode('5 boulevard du Port, Amiens, France')->get());
        // dump($adresse);
        $geocode = app('geocoder')->geocode($adresse)->get();
        // dump($geocode);
        if (! count($geocode)) {
            return false;
        }
        // dump($geocode);
        $res = $geocode[0]->getCoordinates()->toArray();
        // dump($res);
        $latitude = $res[0];
        $longitude = $res[1];

        // dump($latitude);
        // dump($longitude);
        return ['latitude' => $latitude, 'longitude' => $longitude];

    }

    public static function getCoordsByCity($id)
    {
        $ville = City::find($id);

        return ['latitude' => $ville->latitude, 'longitude' => $ville->longitude];
    }
}
