<?php

namespace App\Http\Controllers;

use App\Services\{{modelName}}Service;
use App\Http\Requests\{{modelName}}Request;
use App\Http\Resources\{{modelName}}Resource;
use Illuminate\Http\JsonResponse;

class {{modelName}}Controller extends Controller
{
    public function __construct(
        protected {{modelName}}Service $service,
    ) {}

    public function index(): JsonResponse
    {
        $items = $this->service->getAll();
        return $this->successResponse(payload: {{modelName}}Resource::collection($items));
    }

    public function store({{modelName}}Request $request): JsonResponse
    {
        $item = $this->service->create($request->validated());
        return $this->successResponse(payload: new {{modelName}}Resource($item), message: __('messages.created'), code: 201);
    }

    public function show(int $id): JsonResponse
    {
        $item = $this->service->findById($id);
        return $this->successResponse(payload: new {{modelName}}Resource($item));
    }

    public function update({{modelName}}Request $request, int $id): JsonResponse
    {
        $item = $this->service->update($id, $request->validated());
        return $this->successResponse(payload: new {{modelName}}Resource($item), message: __('messages.updated'));
    }

    public function destroy(int $id): JsonResponse
    {
        $this->service->delete($id);
        return $this->successResponse(message: __('messages.deleted'), code: 204);
    }
}
