<?php

namespace App\Modules\CardOrder\Filament\Actions;

use App\Modules\CardOrder\Models\CardOrder;
use App\Modules\CardOrder\Repositories\CardOrderRepository;
use Filament\Actions\Action;
use Filament\Notifications\Notification;

class PayCardOrderAction
{
    public static function make(): Action
    {
        return Action::make('card_order_pay')
            ->label(__('Pay'))
            ->icon('heroicon-o-credit-card')
            ->requiresConfirmation()
            ->action(function (CardOrder $record) {
                $onlinePayment = CardOrderRepository::make()
                    ->createOnlinePaymentOrder($record);

                if ($onlinePayment->successful()) {
                    Notification::make()
                        ->success()
                        ->title('Sending')
                        ->send();

                    return redirect()->away($onlinePayment->paymentLink());
                }

                Notification::make()
                    ->danger()
                    ->title('Could not send')
                    ->send();
            });
    }
}
