<?php

namespace App\Domains\Admin\Controllers;

use App\Domains\Admin\Actions\UpdateAvailablePaymentMethod\UpdateAvailablePaymentMethodAction;
use App\Domains\Admin\Actions\UpdateAvailablePaymentMethod\UpdateAvailablePaymentMethodRequest;
use App\Models\AvailablePaymentMethod;
use App\Scopes\CommonController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class TenantPaymentMethodsUpdateController extends CommonController
{
    public function __invoke(int $id, Request $request): JsonResponse
    {
        $availablePaymentMethod = AvailablePaymentMethod::findOrFail($id);

        $action = new UpdateAvailablePaymentMethodAction(new UpdateAvailablePaymentMethodRequest(
            availablePaymentMethod: $availablePaymentMethod,
            name: $request->input('name'),
            description: $request->input('description'),
            active: (bool) $request->input('active'),
            configuration: (array) $request->input('configuration', []),
        ));

        $response = $action->execute();

        return response()->json([
            'status' => $response->success ? 'success' : 'error',
            'message' => $response->message,
        ]);
    }
}
