<?php

namespace {{ namespace }};

use App\Http\Controllers\Controller;
use {{ requestNamespace }};
use {{ resourceNamespace }};
use {{ serviceNamespace }};
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class {{ class }} extends Controller
{
    /**
     * @var {{ service }}
     */
    protected {{ service }} ${{ serviceVariable }};

    /**
     * DummyModel Constructor
     *
     * @param {{ service }} ${{ serviceVariable }}
     *
     */
    public function __construct({{ service }} ${{ serviceVariable }})
    {
        $this->{{ serviceVariable }} = ${{ serviceVariable }};
    }

    public function index(): \Illuminate\Http\Resources\Json\AnonymousResourceCollection
    {
        return {{ resource }}::collection($this->{{ serviceVariable }}->getAll());
    }

    public function store({{ request }} $request): {{ resource }}|\Illuminate\Http\JsonResponse
    {
        try {
            return new {{ resource }}($this->{{ serviceVariable }}->save($request->validated()));
        } catch (\Exception $exception) {
            report($exception);
            return response()->json(['error' => 'There is an error.'], Response::HTTP_INTERNAL_SERVER_ERROR);
        }
    }

    public function show(int $id): {{ resource }}
    {
        return {{ resource }}::make($this->{{ serviceVariable }}->getById($id));
    }

    public function update({{ request }} $request, int $id): {{ resource }}|\Illuminate\Http\JsonResponse
    {
        try {
            return new {{ resource }}($this->{{ serviceVariable }}->update($request->validated(), $id));
        } catch (\Exception $exception) {
            report($exception);
            return response()->json(['error' => 'There is an error.'], Response::HTTP_INTERNAL_SERVER_ERROR);
        }
    }

    public function destroy(int $id): \Illuminate\Http\JsonResponse
    {
        try {
            $this->{{ serviceVariable }}->deleteById($id);
            return response()->json(['message' => 'Deleted successfully'], Response::HTTP_OK);
        } catch (\Exception $exception) {
            report($exception);
            return response()->json(['error' => 'There is an error.'], Response::HTTP_INTERNAL_SERVER_ERROR);
        }
    }
}
