<?php

namespace App\Http\Controllers;

use App\Http\Requests\ProviderPaymentMethodRequest;
use App\Http\Resources\ProviderPaymentMethodResource;
use App\Services\ProviderPaymentMethodService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

class ProviderPaymentMethodController extends Controller
{
    public function __construct(
        private readonly ProviderPaymentMethodService $service
    ) {}

    public function index($id): JsonResponse
    {
        $paymentMethods = $this->service->getByProvider($id);
        return $this->successResponse(
            payload: ProviderPaymentMethodResource::collection($paymentMethods)
        );
    }

    public function show(int $id): ProviderPaymentMethodResource
    {
        $paymentMethod = $this->service->findById($id);
        return new ProviderPaymentMethodResource($paymentMethod);
    }

    public function store(ProviderPaymentMethodRequest $request): JsonResponse
    {
        $paymentMethod = $this->service->create($request->validated());
        return $this->successResponse(
            payload: new ProviderPaymentMethodResource($paymentMethod)
        );
    }

    public function update(ProviderPaymentMethodRequest $request, int $id): JsonResponse
    {
        $paymentMethod = $this->service->findById($id);
        
        $paymentMethod = $this->service->update($paymentMethod, $request->validated());
        return $this->successResponse(
            payload: new ProviderPaymentMethodResource($paymentMethod)
        );
    }

    public function destroy(int $id): JsonResponse
    {
        $paymentMethod = $this->service->findById($id);
        
        $this->service->delete($paymentMethod);
        return $this->successResponse(
            message: __("messages.deleted"),
            code: 204,
        );
    }
}
