<?php

namespace App\Filament\Clusters\Cards\CardOrders\Pages;

use App\Filament\Clusters\Cards\CardOrders\CardOrderResource;
use App\Modules\CardOrder\Repositories\CardOrderRepository;
use Filament\Resources\Pages\CreateRecord;

class CreateCardOrder extends CreateRecord
{
    protected static string $resource = CardOrderResource::class;

    protected function getRedirectUrl(): string
    {
        $defaultUrl = $this->getResource()::getUrl('index');

        /** @var \App\Modules\CardOrder\Models\CardOrder */
        $record = $this->record;

        $OnlinePayment = CardOrderRepository::make()
            ->createOnlinePaymentOrder($record);

        return $OnlinePayment->successful()
            ? $OnlinePayment->paymentLink()
            : $defaultUrl;
    }
}
