<?php

namespace Twnt4All\Backend;

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

/**
 * Adds a "TWINT for WooCommerce" submenu under the Pay4All TWINT
 * top-level menu when WooCommerce is active but the paid companion
 * plugin `pay4all-pro` is not installed. Clicking the entry lands on
 * an upsell page explaining that the WooCommerce gateway lives inside
 * Pay4All Pro (distributed outside WordPress.org).
 *
 * The label is rendered in the Pay4All Pro accent colour (#ffba00) so
 * it visually flags itself as an upsell entry - mirrors the pattern
 * already used by pay4all-age's WooTeaser.
 *
 * Registered from Bootstrap::on_plugins_loaded() with the same
 * capability gate as the wizard.
 */
final class WooTeaser
{
    const SLUG = 'pay4all-twint-for-woocommerce';

    public static function register()
    {
        // Priority 100 fires after `Bootstrap::add_admin_menu` (99),
        // which registers the parent "Pay4All TWINT" slug.
        add_action( 'admin_menu', [ self::class, 'add_submenu' ], 100 );
    }

    public static function add_submenu()
    {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }
        if ( ! class_exists( 'WooCommerce' ) ) {
            return;
        }
        if ( class_exists( '\\Pay4All\\Pro\\Plugin' ) ) {
            return;
        }

        $label_plain = esc_html__( 'TWINT for WooCommerce', 'pay4all-for-twint' );
        $label_html  = '<span style="color:#ffba00;">' . $label_plain . '</span>';

        add_submenu_page(
            'pay4all-settings',
            $label_plain,
            $label_html,
            'manage_options',
            self::SLUG,
            [ self::class, 'render' ]
        );
    }

    public static function render()
    {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }
        $lang     = self::current_locale();
        $saas_url = add_query_arg( 'lang', $lang, 'https://pay4all.ch/p/twint-woocommerce' );
        ?>
        <div class="wrap">
            <h1 style="display:flex;align-items:center;gap:12px;">
                <?php esc_html_e( 'TWINT for WooCommerce', 'pay4all-for-twint' ); ?>
                <span style="display:inline-block;background:#ffba00;color:#1d2327;font-weight:700;font-size:12px;padding:2px 10px;border-radius:12px;text-transform:uppercase;letter-spacing:.02em;">PRO</span>
            </h1>

            <div style="max-width:820px;background:#fff8e5;border-left:4px solid #ffba00;padding:16px 20px;margin:16px 0 24px;border-radius:2px;">
                <p style="margin:0 0 6px;font-size:14px;">
                    <strong><?php esc_html_e( 'The TWINT WooCommerce integration requires Pay4All Pro.', 'pay4all-for-twint' ); ?></strong>
                </p>
                <p style="margin:0;color:#3c434a;">
                    <?php esc_html_e( 'The free Pay4All for TWINT plugin handles the TWINT gateway on the Pay4All Shop side. Pay4All Pro adds the WooCommerce surfaces on top: classic checkout + Blocks, HPOS, automatic refunds, background monitoring, multilingual copy, and order management.', 'pay4all-for-twint' ); ?>
                </p>
            </div>

            <h2><?php esc_html_e( 'What Pay4All Pro adds for WooCommerce', 'pay4all-for-twint' ); ?></h2>

            <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(320px,1fr));gap:18px;max-width:1100px;margin-top:16px;">
                <section style="background:#fff;border:1px solid #dcdcde;border-radius:8px;padding:16px 20px;">
                    <h3 style="margin-top:0;"><?php esc_html_e( 'WooCommerce TWINT gateway', 'pay4all-for-twint' ); ?></h3>
                    <p style="color:#555;margin:0;">
                        <?php esc_html_e( 'TWINT shows up as a payment method in the classic checkout and in Checkout Blocks. HPOS-compatible (High-Performance Order Storage).', 'pay4all-for-twint' ); ?>
                    </p>
                </section>

                <section style="background:#fff;border:1px solid #dcdcde;border-radius:8px;padding:16px 20px;">
                    <h3 style="margin-top:0;"><?php esc_html_e( 'Multilingual copy', 'pay4all-for-twint' ); ?></h3>
                    <p style="color:#555;margin:0;">
                        <?php esc_html_e( 'Set the TWINT method title and description separately in FR / DE / IT / EN. Works with WPML and Polylang: the page language switches automatically.', 'pay4all-for-twint' ); ?>
                    </p>
                </section>

                <section style="background:#fff;border:1px solid #dcdcde;border-radius:8px;padding:16px 20px;">
                    <h3 style="margin-top:0;"><?php esc_html_e( 'Order auto-completion', 'pay4all-for-twint' ); ?></h3>
                    <p style="color:#555;margin:0;">
                        <?php esc_html_e( 'Choose how TWINT-paid orders are handled: automatic move to "Completed" for virtual or downloadable products, "Processing" for physical ones.', 'pay4all-for-twint' ); ?>
                    </p>
                </section>

                <section style="background:#fff;border:1px solid #dcdcde;border-radius:8px;padding:16px 20px;">
                    <h3 style="margin-top:0;"><?php esc_html_e( 'TWINT refunds', 'pay4all-for-twint' ); ?></h3>
                    <p style="color:#555;margin:0;">
                        <?php esc_html_e( 'The "Refund" button in WooCommerce automatically triggers the SOAP refund on TWINT. Mirrors the "Refunded" status when the admin flips the status manually.', 'pay4all-for-twint' ); ?>
                    </p>
                </section>

                <section style="background:#fff;border:1px solid #dcdcde;border-radius:8px;padding:16px 20px;">
                    <h3 style="margin-top:0;"><?php esc_html_e( 'Background monitoring', 'pay4all-for-twint' ); ?></h3>
                    <p style="color:#555;margin:0;">
                        <?php esc_html_e( 'Asynchronous tracking of pending TWINT payments. The customer does not wait for the SOAP polling to finish in their browser: the redirect is immediate.', 'pay4all-for-twint' ); ?>
                    </p>
                </section>

                <section style="background:#fff;border:1px solid #dcdcde;border-radius:8px;padding:16px 20px;">
                    <h3 style="margin-top:0;"><?php esc_html_e( 'TWINT webhooks', 'pay4all-for-twint' ); ?></h3>
                    <p style="color:#555;margin:0;">
                        <?php esc_html_e( 'TWINT OUN and redirect callbacks are routed to WooCommerce automatically - no REST configuration required on the merchant portal side.', 'pay4all-for-twint' ); ?>
                    </p>
                </section>
            </div>

            <p style="margin-top:32px;display:flex;flex-wrap:wrap;gap:12px;align-items:center;">
                <a class="button button-primary button-hero" href="https://pay4all.ch/downloads/pay4all-pro" target="_blank" rel="noopener" style="display:inline-flex;align-items:center;gap:8px;">
                    <span class="dashicons dashicons-download" style="font-size:20px;width:20px;height:20px;line-height:1;color:#fff;"></span>
                    <?php esc_html_e( 'Download Pay4all-Pro.zip', 'pay4all-for-twint' ); ?>
                </a>
                <a class="button button-secondary button-hero" href="<?php echo esc_url( $saas_url ); ?>" target="_blank" rel="noopener">
                    <?php esc_html_e( 'Discover Pay4All Pro', 'pay4all-for-twint' ); ?> ↗
                </a>
            </p>

            <p class="description" style="margin-top:16px;color:#555;max-width:820px;">
                <?php esc_html_e( 'Pay4All Pro is distributed from Pay4All.ch with a one-month free trial. No license key entry until the trial ends.', 'pay4all-for-twint' ); ?>
            </p>
        </div>
        <?php
    }

    /**
     * Two-letter locale segment restricted to the Pay4All-supported set,
     * with a French fallback. Matches the pattern used across the other
     * Pay4All teaser pages.
     */
    private static function current_locale()
    {
        $lang = strtolower( substr( (string) determine_locale(), 0, 2 ) );
        return in_array( $lang, [ 'fr', 'de', 'it', 'en' ], true ) ? $lang : 'fr';
    }
}
