<?php

namespace Pay4AllForMypos\Blocks;

use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
use Pay4AllForMypos\Checkout\MyposGateway;

defined( 'ABSPATH' ) || exit;

/**
 * Pay4All myPOS payment method for the WooCommerce Cart/Checkout Blocks.
 *
 * The real payment UI is hosted by myPOS (auto-submitting form redirects
 * to mypos.com), so the block-side integration is minimal - we just need
 * to surface the gateway in the block's payment-method picker. The
 * redirect to myPOS happens after `process_payment()` returns
 * `result=success` + `redirect=URL`, which WC Blocks honours natively via
 * the Store API.
 */
final class MyposBlocksMethod extends AbstractPaymentMethodType
{
    /** @var string Registry key - must match the WC gateway id. */
    protected $name = MyposGateway::SLUG;

    public function initialize()
    {
        $this->settings = get_option( 'pay4all_for_mypos_settings', [] );
    }

    public function is_active()
    {
        if ( ! function_exists( 'WC' ) || ! WC()->payment_gateways ) {
            return false;
        }
        $gateways = WC()->payment_gateways->payment_gateways();
        if ( ! isset( $gateways[ MyposGateway::SLUG ] ) ) {
            return false;
        }
        $gateway = $gateways[ MyposGateway::SLUG ];
        // Reuse the WC Payment_Gateway logic - same conditions as the
        // classic checkout (enabled + Pack present + supported currency).
        return $gateway->is_available();
    }

    /**
     * Register the JS bundle that calls
     * `wc.wcBlocksRegistry.registerPaymentMethod()` and return its handle
     * so WC Blocks enqueues it on the checkout block.
     *
     * The JS file itself is shipped by Batch 4 - referencing the path
     * here is enough for `wp_register_script` to succeed; if the file is
     * missing at request time WordPress will emit a 404 for the asset
     * but the block registry entry still shows up.
     *
     * @return string[]
     */
    public function get_payment_method_script_handles()
    {
        $handle = 'pay4all-for-mypos-blocks';
        $src    = plugins_url( 'assets/js/blocks/checkout.js', PAY4ALL_FOR_MYPOS_PLUGIN_FILE );
        wp_register_script(
            $handle,
            $src,
            [ 'wp-element', 'wc-blocks-registry', 'wc-settings' ],
            PAY4ALL_FOR_MYPOS_PLUGIN_VERSION,
            true
        );
        if ( function_exists( 'wp_set_script_translations' ) ) {
            wp_set_script_translations( $handle, 'pay4all-for-mypos', PAY4ALL_FOR_MYPOS_PLUGIN_PATH . '/localization' );
        }
        return [ $handle ];
    }

    /**
     * Data exposed to the JS bundle via
     * `wc.wcSettings.getSetting('pay4all_for_mypos_data', {})`.
     *
     * @return array
     */
    public function get_payment_method_data()
    {
        $title       = (string) $this->get_setting( 'title', __( 'Card / TWINT (myPOS)', 'pay4all-for-mypos' ) );
        $description = (string) $this->get_setting( 'description', __( 'Secure payment via myPOS. You will be redirected to complete your order.', 'pay4all-for-mypos' ) );

        // If the classic gateway is instantiated, prefer its live title +
        // description so any per-request overrides (e.g. from an admin
        // filter) still surface in the block picker.
        if ( function_exists( 'WC' ) && WC()->payment_gateways ) {
            $gateways = WC()->payment_gateways->payment_gateways();
            if ( isset( $gateways[ MyposGateway::SLUG ] ) ) {
                $gw          = $gateways[ MyposGateway::SLUG ];
                $title       = (string) $gw->get_title();
                $description = (string) $gw->get_description();
            }
        }

        return [
            'title'       => $title,
            'description' => $description,
            'supports'    => [ 'products' ],
        ];
    }
}
