<?php
namespace Pay4AllForMypos\Backend;

if ( ! defined( 'ABSPATH' ) ) { exit; }

/**
 * WooCommerce sub-menu shortcut that redirects to the Pay4All myPOS
 * installation wizard.
 *
 * Unlike the Stripe plugin's WcSettings which hosts a full status editor,
 * the myPOS variant is intentionally trivial : the wizard's step 4 already
 * exposes all WC status controls, so this file only registers a friendly
 * entry point under WooCommerce → « Configurer myPOS » that hops the
 * merchant straight to the wizard.
 *
 * Self-guards on WC presence (WP.org §18: no orphan menu items).
 */
final class WcSettings {

    const SLUG = 'pay4all-for-mypos-woo-shortcut';

    public static function register() : void {
        add_action( 'admin_menu', [ __CLASS__, 'add_submenu' ], 12 );
    }

    public static function add_submenu() : void {
        if ( ! current_user_can( 'manage_woocommerce' ) ) { return; }
        if ( ! ConfigPage::has_woocommerce() ) { return; }

        $hook = add_submenu_page(
            'woocommerce',
            __( 'Configure myPOS', 'pay4all-for-mypos' ),
            __( 'Configure myPOS', 'pay4all-for-mypos' ),
            'manage_woocommerce',
            self::SLUG,
            [ __CLASS__, 'render' ]
        );

        // Fires before any HTML output for this submenu page - safe place
        // to send an HTTP redirect via wp_safe_redirect. Keeps the render()
        // fallback for the (rare) case a rogue plugin sends headers early.
        if ( $hook ) {
            add_action( 'load-' . $hook, [ __CLASS__, 'server_side_redirect' ] );
        }
    }

    /**
     * Server-side redirect fired from the load-{hook_suffix} action so we
     * never have to emit an inline <script> for a client-side hop (WP.org
     * §7 forbids inline scripts on admin pages).
     */
    public static function server_side_redirect() : void {
        if ( ! current_user_can( 'manage_woocommerce' ) ) { return; }
        wp_safe_redirect( admin_url( 'admin.php?page=' . WizardPage::SLUG ) );
        exit;
    }

    /**
     * Fallback UI. Only rendered if the server-side redirect above never
     * fires (rogue plugin sent output before load-{hook_suffix}) - shows
     * a plain link the merchant can click.
     */
    public static function render() : void {
        if ( ! current_user_can( 'manage_woocommerce' ) ) { return; }
        $wizard = admin_url( 'admin.php?page=' . WizardPage::SLUG );
        ?>
        <div class="wrap">
            <h1><?php esc_html_e( 'Configure myPOS', 'pay4all-for-mypos' ); ?></h1>
            <p>
                <?php
                printf(
                    wp_kses(
                        /* translators: %s: link to the Pay4All myPOS wizard. */
                        __( 'The myPOS configuration lives in the %s.', 'pay4all-for-mypos' ),
                        [ 'a' => [ 'href' => [] ] ]
                    ),
                    '<a href="' . esc_url( $wizard ) . '"><strong>' . esc_html__( 'Pay4All myPOS wizard', 'pay4all-for-mypos' ) . '</strong></a>'
                );
                ?>
            </p>
            <p>
                <a href="<?php echo esc_url( $wizard ); ?>" class="button button-primary">
                    <?php esc_html_e( 'Open the wizard', 'pay4all-for-mypos' ); ?>
                </a>
            </p>
        </div>
        <?php
    }
}
