<?php

namespace App\Http\Controllers\CRM;

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

class CountryCodeController extends Controller
{
    public function show(Country $country)
    {
        return [
            'value' => $country->id,
            'label' => $country->long_iso,
            'dropdown_label' => "({$country->long_iso}) {$country->name}",
        ];
    }

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

        $countries = Country::query()
            ->where('name', 'like', "%{$term}%")
            ->orWhere('short_iso', 'like', "%{$term}%")
            ->orWhere('long_iso', 'like', "%{$term}%")
            ->limit(20)
            ->get();

        return $countries->map(function ($country) {
            return [
                'value'             => $country->id,
                'label'             => $country->long_iso,
                'dropdown_label'    => "{$country->long_iso} ({$country->name})",
            ];
        });
    }
}
