<?php

namespace App\Domains\Integration\Controllers;

use App\Domains\Client\Models\Client;
use App\Domains\Integration\Requests\TelepagosWebhookRequest;
use App\Domains\Integration\Services\TelepagosWebhookService;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;

class TelepagosWebhookController extends Controller
{
    /**
     * Handle the incoming Telepagos webhook.
     */
    public function handle(TelepagosWebhookRequest $request, Client $client, TelepagosWebhookService $service): JsonResponse
    {
        try {
            $cashinId = $request->validated('id');

            $service->handleWebhook($client, $cashinId);

            return response()->json(['status' => 'success']);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'code' => 'integration.webhook_failed',
                'message' => __('api.integration.webhook_failed'),
            ], 500);
        }
    }
}
