<?php

namespace App\Http\Controllers;

use App\Http\Requests\ImprovementTypeRequest;
use App\Http\Resources\ImprovementTypeResource;
use App\Services\ImprovementTypeService;
use Illuminate\Http\JsonResponse;

class ImprovementTypeController extends Controller
{
    public function __construct(
        protected ImprovementTypeService $service,
    ) {}

    public function index(ImprovementTypeRequest $request): JsonResponse
    {
        $items = $this->service->getAll($request->input('origin'));

        return $this->successResponse(payload: ImprovementTypeResource::collection($items));
    }

    public function store(ImprovementTypeRequest $request): JsonResponse
    {
        $item = $this->service->create($request->validated());

        return $this->successResponse(payload: new ImprovementTypeResource($item), message: __('messages.created'), code: 201);
    }

    public function show(int $id): JsonResponse
    {
        $item = $this->service->findById($id);

        return $this->successResponse(payload: new ImprovementTypeResource($item));
    }

    public function update(ImprovementTypeRequest $request, int $id): JsonResponse
    {
        $item = $this->service->update($id, $request->validated());

        return $this->successResponse(payload: new ImprovementTypeResource($item), message: __('messages.updated'));
    }

    public function destroy(int $id): JsonResponse
    {
        $this->service->delete($id);

        return $this->successResponse(message: __('messages.deleted'), code: 204);
    }
}
