<?php

namespace App\Http\Controllers;

use App\Http\Requests\PaymentSplitRequest;
use App\Http\Resources\PaymentSplitResource;
use App\Services\PaymentSplitService;
use Illuminate\Http\JsonResponse;

class PaymentSplitController extends Controller
{
    public function __construct(
        protected PaymentSplitService $service,
    ) {}

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

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

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

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

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

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

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

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

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

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