<?php

/**
 * BookiFlex Stripe Payment Extension
 *
 * @package    BookiFlex
 * @subpackage StripePayment
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */
declare (strict_types = 1);
namespace BookiFlex\Extensions\StripePayment;

use BookiFlex\Extensions\StripePayment\Action\ConvertPaymentAction;
use BookiFlex\Core\Contract\ExtensionInterface;
use BookiFlex\Core\Contract\Payment\PaymentGatewayManagerInterface;
use BookiFlex\Core\Contract\PluginInterface;
use BookiFlex\Extensions\StripePayment\Action\AuthorizeAction;
use BookiFlex\Extensions\StripePayment\Action\CancelAction;
use BookiFlex\Extensions\StripePayment\Action\CaptureAction;
use BookiFlex\Extensions\StripePayment\Admin\PaymentSettings;
use BookiFlex\Extensions\StripePayment\Service\Configuration;
use FluxSE\PayumStripe\StripeCheckoutSessionGatewayFactory;
use Payum\Core\GatewayFactoryInterface;
defined( 'ABSPATH' ) || exit;
/**
 * Stripe payment gateway.
 */
class StripePayment implements ExtensionInterface {
    protected PluginInterface $plugin;

    public function init( PluginInterface $plugin ) : void {
        $this->plugin = $plugin;
        if ( !self::isPaymentExtensionSupported() ) {
            return;
        }
        // Register ServiceProvider (DI container)
        $plugin->getContainer()->register( new ServiceProvider() );
        $plugin->addConfig( [
            'settings' => [[
                'tab'     => 'payment',
                'factory' => new PaymentSettings($plugin),
                'name'    => 'Payment',
            ]],
        ] );
        /** @var PaymentGatewayManagerInterface $paymentExtension */
        $paymentExtension = $plugin->getContainer()->get( PaymentGatewayManagerInterface::class );
        $paymentExtension->registerPaymentGateway(
            id: 'stripe_checkout_session',
            name: 'Stripe Checkout Session',
            supportAuthorize: true,
            gatewayFactory: fn( array $config, GatewayFactoryInterface $coreGatewayFactory ) => new StripeCheckoutSessionGatewayFactory($config, $coreGatewayFactory),
            gatewaySettingsFactory: function () use($plugin) {
                $gatewaySettings = [
                    'factory'                      => 'stripe_checkout_session',
                    'publishable_key'              => Configuration::publishableKey(),
                    'secret_key'                   => Configuration::secretKey(),
                    'test_mode'                    => Configuration::isTestMode(),
                    'webhook_secret_keys'          => [Configuration::webhookSecretKey()],
                    'payum.action.capture'         => new CaptureAction(),
                    'payum.action.authorize'       => new AuthorizeAction(),
                    'payum.action.cancel.not_open' => new CancelAction(),
                    'payum.action.convert_payment' => $this->plugin->getContainer()->get( ConvertPaymentAction::class ),
                ];
                return $gatewaySettings;
            }
        );
    }

    private static function isPaymentExtensionSupported() : bool {
        return class_exists( 'BookiFlex\\Extensions\\Payment\\Payment' );
    }

    public function getName() : string {
        return 'Stripe Checkout Session';
    }

    public function getVersion() : string {
        return '1.0.0';
    }

    public function isCompatible( string $pluginVersion ) : bool {
        return version_compare( $pluginVersion, '1.0.0', '>=' );
    }

}
