<?php

namespace App\Http\Controllers;

use App\Http\Requests\BankRequest;
use App\Http\Resources\BankResource;
use App\Services\BankService;
use Illuminate\Http\JsonResponse;

class BankController extends Controller
{
    public function __construct(
        protected BankService $service,
    ) {}

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

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

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

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

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

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

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

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

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

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