<?php

namespace App\Http\Controllers;

use App\Http\Requests\ClientPaymentMethodRequest;
use App\Http\Resources\CardsListResource;
use App\Http\Resources\ClientPaymentMethodResource;
use App\Services\ClientPaymentMethodService;
use Illuminate\Http\JsonResponse;

class ClientPaymentMethodController extends Controller
{
    public function __construct(
        private readonly ClientPaymentMethodService $service
    ) {}

    public function index(int $clientId): JsonResponse
    {
        $paymentMethods = $this->service->getByClientId($clientId);

        return $this->successResponse(
            payload: CardsListResource::collection($paymentMethods)
        );
    }

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

        return $this->successResponse(
            payload: new ClientPaymentMethodResource($paymentMethod)
        );
    }

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

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

    public function update(ClientPaymentMethodRequest $request, int $id): JsonResponse
    {
        $paymentMethod = $this->service->findById($id);

        $paymentMethod = $this->service->update($paymentMethod, $request->validated());

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

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

        $this->service->delete($paymentMethod);

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