<?php

namespace App\Http\Controllers\CRM;

use App\Models\CRM\City;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PassportCityController extends Controller
{
    public function show(City $city)
    {
        return [
            'value' => $city->id,
            'label' => $city->name,
            'dropdown_label' => "({$city->zip}) {$city->name}, {$city->country->name}",
        ];
    }

    public function search(Request $request)
    {
        $term = $request->query('search', '');

        $cities = City::query()
            ->where('name', 'like', "%{$term}%")
            ->orWhere('zip', 'like', "%{$term}%")
            ->limit(20)
            ->get();

        // Return short label + long dropdown_label
        return $cities->map(function ($city) {
            return [
                'value'             => $city->id,
                'label'             => "{$city->name} ({$city->country->name})",
                'dropdown_label'    => "({$city->zip}) {$city->name}, {$city->country->name}",
            ];
        });
    }
    
}
