<?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;

    /**
     * @return array<int, \Filament\Actions\Action>
     */
    protected function getFormActions(): array
    {
        return [
            $this->getCreateFormAction(),
            $this->getCancelFormAction(),
        ];
    }

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

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

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

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