<?php

namespace Modules\City\Controllers\Api;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\City\Models\Resources\CityResource;
use Modules\City\Services\CityService;

class CityController extends Controller
{

    public function __construct(
        private CityService $cityService,
    ){}

    /**
     * Display a listing of the countries.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request):JsonResponse
    {
        $cities = $this->cityService->get();
        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>CityResource::collection($cities)
        ]);
    }

    /**
     * Show the form for editing the specified Country.
     *
     * @param  int $id
     * @return JsonResponse
     */
    public function show(int $id) : JsonResponse
    {
        $city = $this->cityService->checkItem($id);
        if (!$city)
        {
            abort(404);
        }
        $city = $this->cityService->getItem($id);
        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>new CityResource($city)
        ]);
    }


}
