<?php

namespace Modules\Country\Controllers\Api;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\Country\Models\Resources\CountryResource;
use Modules\Country\Requests\Admin\CountryRequest;
use Modules\Country\Services\CountryService;
use Modules\Currency\Services\CurrencyService;

class CountryController extends Controller
{

    public function __construct(
        private CountryService $countryService,
    ){}

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

    /**
     * Show the form for editing the specified Country.
     *
     * @param  int $id
     * @return JsonResponse
     */
    public function show(int $id) : JsonResponse
    {
        $country = $this->countryService->checkItem($id);
        if (!$country)
        {
            abort(404);
        }
        $country = $this->countryService->getItem($id);

        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>new CountryResource($country)
        ]);
    }


}
