<?php

namespace App\Http\Controllers;

use App\Http\Requests\PermissionRequest;
use App\Http\Resources\PermissionResource;
use App\Services\PermissionService;
use Illuminate\Http\JsonResponse;

class PermissionController extends Controller
{
    public function __construct(protected PermissionService $service) {}

    public function index(): JsonResponse
    {
        $items = $this->service->getAll();

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

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

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

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

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

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

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

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

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