<?php

namespace App\Http\Controllers;

use App\Models\HCountry;
use Illuminate\Http\Request;

class HelperController extends Controller
{
    public function country(Request $request)
    {
        if ($request->input('country')) {
            $country = HCountry::where(function ($q) use ($request) {
                $q->where('code', $request->input('country'))
                    ->orWhere([
                        'num_code' => $request->input('country'),
                        'iso1_code' => $request->input('country'),
                        'iso3_code' => $request->input('country'),
                        'name' => $request->input('country'),
                        'name_caps' => $request->input('country'),
                    ]);
            })->first();
            return response()->json([
                'status' => true,
                'data' => $country,
            ]);
        } else {
            $countries = HCountry::all();
            return response()->json([
                'status' => true,
                'data' => $countries,
            ]);
        }
    }
}
