<?php

namespace Modules\Area\Controllers\Api;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\Area\Enums\AreaStatusEnum;
use Modules\Area\Models\Area;
use Modules\Area\Models\Resources\AreaResource;
use Modules\Area\Services\AreaService;

class AreaController extends Controller
{
    
    private AreaService $areaService;

    public function __construct(AreaService $areaService)
    {
        $this->areaService = $areaService;
    }

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

    public function show(Request $request,$id) :JsonResponse
    {
        $area = Area::findOrFail($id);
        $area = $this->areaService->getItem($area);
        $area = new AreaResource($area);
        $response = [
            'status'=>200,
            'message'=>'',
            'data'=> $area
        ];
        return ResponseService::response($response);
    }
}
