<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use Exception;
use App\Models\DeliveryArea;
use App\Services\DeliveryAreaService;
use App\Http\Requests\PaginateRequest;
use App\Http\Resources\DeliveryAreaResource;

class DeliveryAreaController extends Controller
{

    public DeliveryAreaService $deliveryAreaService;

    public function __construct(DeliveryAreaService $deliveryAreaService)
    {
        $this->deliveryAreaService = $deliveryAreaService;
    }

    public function index(PaginateRequest $request): \Illuminate\Http\Response|\Illuminate\Http\Resources\Json\AnonymousResourceCollection|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory
    {
        try {
            return DeliveryAreaResource::collection($this->deliveryAreaService->list($request));
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }

    public function show(DeliveryArea $deliveryArea): \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Contracts\Foundation\Application|DeliveryAreaResource
    {
        try {
            return new DeliveryAreaResource($this->deliveryAreaService->show($deliveryArea));
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }
}