<?php
/**
 * Frontend Native Checkout Shortcode
 *
 * @package Invizo\SubscriptionsLite\Frontend
 */

namespace Invizo\SubscriptionsLite\Frontend;

use Invizo\SubscriptionsLite\Gateway\StripeGateway;

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

class Checkout {

    public static function init() {
        add_shortcode( 'invizo_checkout', array( __CLASS__, 'render_checkout' ) );
        add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ), 100 );
    }

    /**
     * @param int $plan_id Plan post ID.
     * @return string
     */
    protected static function interval_short_label( $plan_id ) {
        $interval = get_post_meta( $plan_id, '_interval', true ) ?: 'month';
        $i        = strtolower( substr( $interval, 0, 2 ) );
        if ( $i === 'mo' ) {
            return 'mo';
        }
        if ( $i === 'ye' ) {
            return 'yr';
        }
        return $interval;
    }

    /**
     * Subtitle fragment after price (editorial style).
     *
     * @param int $plan_id Plan post ID.
     * @return string
     */
    protected static function interval_access_phrase( $plan_id ) {
        $interval = strtolower( (string) get_post_meta( $plan_id, '_interval', true ) );
        if ( strpos( $interval, 'year' ) !== false ) {
            return __( '/ yearly member access', 'invizo-subscriptions-lite' );
        }
        if ( strpos( $interval, 'week' ) !== false ) {
            return __( '/ weekly member access', 'invizo-subscriptions-lite' );
        }
        if ( strpos( $interval, 'day' ) !== false ) {
            return __( '/ daily member access', 'invizo-subscriptions-lite' );
        }
        return __( '/ monthly member access', 'invizo-subscriptions-lite' );
    }

    /**
     * @param int $plan_id Plan post ID.
     * @return string[]
     */
    protected static function plan_summary_features( $plan_id ) {
        $lines = Store::get_plan_feature_lines( $plan_id );
        if ( ! empty( $lines ) ) {
            return $lines;
        }
        return array(
            __( 'Full access to your subscription benefits and updates.', 'invizo-subscriptions-lite' ),
            __( 'Manage billing and renewal from your account anytime.', 'invizo-subscriptions-lite' ),
            __( 'Secure checkout powered by Stripe.', 'invizo-subscriptions-lite' ),
        );
    }

    /**
     * Country list for checkout (WooCommerce if available).
     *
     * @return array<string, string> ISO code => label
     */
    protected static function get_checkout_countries() {
        if ( function_exists( 'WC' ) && WC()->countries ) {
            $list = WC()->countries->get_countries();
            if ( is_array( $list ) && $list !== array() ) {
                return $list;
            }
        }
        return array(
            'US' => __( 'United States', 'invizo-subscriptions-lite' ),
            'GB' => __( 'United Kingdom', 'invizo-subscriptions-lite' ),
            'CA' => __( 'Canada', 'invizo-subscriptions-lite' ),
            'AU' => __( 'Australia', 'invizo-subscriptions-lite' ),
            'DE' => __( 'Germany', 'invizo-subscriptions-lite' ),
            'FR' => __( 'France', 'invizo-subscriptions-lite' ),
            'BD' => __( 'Bangladesh', 'invizo-subscriptions-lite' ),
            'IN' => __( 'India', 'invizo-subscriptions-lite' ),
        );
    }

    /**
     * Privacy policy URL for checkout: Invizo settings page, else WordPress Privacy Policy.
     *
     * @return string Non-empty permalink/URL or ''.
     */
    public static function get_effective_privacy_policy_url() {
        $page_id = (int) get_option( 'invizo_standalone_privacy_policy_page_id', 0 );
        if ( $page_id > 0 ) {
            $u = get_permalink( $page_id );
            if ( is_string( $u ) && $u !== '' ) {
                return $u;
            }
        }
        if ( function_exists( 'get_privacy_policy_url' ) ) {
            $u = get_privacy_policy_url();
            if ( is_string( $u ) && $u !== '' ) {
                return $u;
            }
        }
        return '';
    }

    /**
     * Free trial length from plan meta (days).
     *
     * @param int $plan_id Plan post ID.
     * @return int Days, or 0 if none.
     */
    protected static function plan_trial_days( $plan_id ) {
        $t = get_post_meta( $plan_id, '_trial_period', true );
        return ( is_numeric( $t ) && (int) $t > 0 ) ? (int) $t : 0;
    }

    /**
     * @return array<string, array<string, mixed>>
     */
    protected static function build_plans_for_js() {
        $plans   = get_posts(
            array(
                'post_type'      => 'invizo_plan',
                'posts_per_page' => -1,
                'post_status'    => 'publish',
                'orderby'        => 'title',
                'order'          => 'ASC',
            )
        );
        $out = array();
        foreach ( $plans as $plan ) {
            $pid   = (int) $plan->ID;
            $price = get_post_meta( $pid, '_price', true );
            $trial = self::plan_trial_days( $pid );
            $out[ (string) $pid ] = array(
                'title'            => $plan->post_title,
                'price'            => is_numeric( $price ) ? (float) $price : 0,
                'priceFormatted'   => is_numeric( $price ) ? number_format( (float) $price, 2, '.', '' ) : (string) $price,
                'intervalShort'    => self::interval_short_label( $pid ),
                'intervalPhrase'   => self::interval_access_phrase( $pid ),
                'media'            => (string) get_post_meta( $pid, '_media', true ),
                'features'         => self::plan_summary_features( $pid ),
                'trialDays'        => $trial,
                'trialLine'        => $trial > 0
                    ? sprintf(
                        /* translators: %d: number of days */
                        __( '%d-day free trial', 'invizo-subscriptions-lite' ),
                        $trial
                    )
                    : '',
            );
        }
        return $out;
    }

    public static function enqueue_assets() {
        global $post;
        if ( ! is_a( $post, 'WP_Post' ) || ! has_shortcode( $post->post_content, 'invizo_checkout' ) ) {
            return;
        }

        wp_enqueue_script( 'invizo-stripe-js', 'https://js.stripe.com/v3/', array(), INVIZO_LITE_VERSION, false );
        wp_enqueue_style( 'invizo-checkout-css', INVIZO_LITE_URL . 'assets/css/checkout.css', array(), INVIZO_LITE_VERSION );
        wp_enqueue_script( 'invizo-checkout-js', INVIZO_LITE_URL . 'assets/js/checkout.js', array( 'jquery', 'invizo-stripe-js' ), INVIZO_LITE_VERSION, true );

        $btn_subscribe  = get_option( 'invizo_checkout_btn_subscribe', 'Subscribe Now' );
        $btn_processing = get_option( 'invizo_checkout_btn_processing', 'Processing...' );
        $stripe_keys     = StripeGateway::get_api_keys();
        $privacy_effective = self::get_effective_privacy_policy_url();
        $store_id        = (int) get_option( 'invizo_standalone_store_page_id', 0 );
        $store_url       = $store_id ? get_permalink( $store_id ) : home_url( '/' );
        $success_url     = Auth::get_account_page_url();

        wp_localize_script(
            'invizo-checkout-js',
            'InvizoCheckout',
            array(
                'ajax_url'             => admin_url( 'admin-ajax.php' ),
                'nonce'                => wp_create_nonce( 'invizo_checkout_nonce' ),
                'stripe_pk'            => $stripe_keys['public'],
                'btn_subscribe'        => $btn_subscribe,
                'btn_processing'       => $btn_processing,
                'btn_complete_payment' => __( 'Complete payment', 'invizo-subscriptions-lite' ),
                'privacy_required'     => (bool) $privacy_effective,
                'privacy_policy_url'   => $privacy_effective ? esc_url_raw( $privacy_effective ) : '',
                'success_redirect_url' => $success_url ? esc_url_raw( $success_url ) : '',
                'store_url'            => esc_url_raw( $store_url ),
                'plans'                => self::build_plans_for_js(),
                'i18n_privacy_error'   => __( 'Please accept the Privacy Policy to continue.', 'invizo-subscriptions-lite' ),
                'i18n_fill_details'    => __( 'Please enter your name and email.', 'invizo-subscriptions-lite' ),
                'i18n_stripe_load'     => __( 'Payment form could not load. Refresh and try again.', 'invizo-subscriptions-lite' ),
                'i18n_email_pay'       => __( 'Please enter a valid email address before completing payment.', 'invizo-subscriptions-lite' ),
                'is_logged_in'         => is_user_logged_in(),
            )
        );
    }

    public static function render_checkout( $atts ) {
        $atts = shortcode_atts(
            array(
                'plan_id' => 0,
                'plan'    => 0,
            ),
            $atts,
            'invizo_checkout'
        );

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only plan ID for checkout shortcode.
        $requested_plan_get = isset( $_GET['plan'] ) ? absint( wp_unslash( $_GET['plan'] ) ) : 0;
        $requested_plan_sc  = absint( $atts['plan_id'] ) ? absint( $atts['plan_id'] ) : absint( $atts['plan'] );
        $selected_plan_id   = $requested_plan_get > 0 ? $requested_plan_get : $requested_plan_sc;

        $plans = get_posts(
            array(
                'post_type'      => 'invizo_plan',
                'posts_per_page' => -1,
                'post_status'    => 'publish',
                'orderby'        => 'title',
                'order'          => 'ASC',
            )
        );

        if ( empty( $plans ) ) {
            return '<div class="invizo-checkout-editorial invizo-checkout-editorial--notice"><p>' . esc_html__( 'No subscription plans found. Create a plan in Invizo Subscriptions first.', 'invizo-subscriptions-lite' ) . '</p></div>';
        }

        $valid_selected = false;
        foreach ( $plans as $p ) {
            if ( $p->ID === $selected_plan_id ) {
                $valid_selected = true;
                break;
            }
        }
        if ( ! $valid_selected ) {
            $selected_plan_id = $plans[0]->ID;
        }

        $valid_get_plan = false;
        if ( $requested_plan_get > 0 ) {
            foreach ( $plans as $p ) {
                if ( (int) $p->ID === $requested_plan_get ) {
                    $valid_get_plan = true;
                    break;
                }
            }
        }
        $valid_sc_plan = false;
        if ( $requested_plan_sc > 0 ) {
            foreach ( $plans as $p ) {
                if ( (int) $p->ID === $requested_plan_sc ) {
                    $valid_sc_plan = true;
                    break;
                }
            }
        }
        $checkout_locked_to_plan = $valid_get_plan || $valid_sc_plan;
        $show_plan_picker        = count( $plans ) > 1 && ! $checkout_locked_to_plan;

        $selected_price = get_post_meta( $selected_plan_id, '_price', true );
        $selected_post  = get_post( $selected_plan_id );
        $media          = get_post_meta( $selected_plan_id, '_media', true );
        $feature_lines  = self::plan_summary_features( $selected_plan_id );
        $int_phrase     = self::interval_access_phrase( $selected_plan_id );
        $price_display  = is_numeric( $selected_price ) ? number_format( (float) $selected_price, 2 ) : esc_html( (string) $selected_price );
        $recurring_line = '$' . $price_display . $int_phrase;
        $trial_days     = self::plan_trial_days( $selected_plan_id );
        $trial_line     = $trial_days > 0
            ? sprintf(
                /* translators: %d: number of days */
                __( '%d-day free trial', 'invizo-subscriptions-lite' ),
                $trial_days
            )
            : '';
        $due_today_line = $trial_days > 0 ? '$0.00' : '$' . $price_display;

        $btn_subscribe = get_option( 'invizo_checkout_btn_subscribe', 'Subscribe Now' );

        $privacy_effective = self::get_effective_privacy_policy_url();

        $store_id  = (int) get_option( 'invizo_standalone_store_page_id', 0 );
        $store_url = $store_id ? get_permalink( $store_id ) : home_url( '/' );

        $site_name   = get_bloginfo( 'name' );
        $countries   = self::get_checkout_countries();
        $current_u   = wp_get_current_user();
        $prefill_name = '';
        $prefill_email = '';
        if ( $current_u && $current_u->ID ) {
            $prefill_name  = trim( $current_u->first_name . ' ' . $current_u->last_name );
            $prefill_email = $current_u->user_email;
            if ( $prefill_name === '' ) {
                $prefill_name = $current_u->display_name;
            }
        }

        ob_start();
        ?>
        <div class="invizo-checkout-editorial">
            <div id="invizo-checkout-loader" class="invizo-checkout-editorial__loader invizo-checkout-editorial__loader--hidden" aria-hidden="true">
                <span class="invizo-checkout-editorial__spinner" aria-hidden="true"></span>
            </div>

            <main class="invizo-checkout-editorial__main">
                <div class="invizo-checkout-editorial__container">
                    <a class="invizo-checkout-editorial__back invizo-checkout-editorial__back--above-grid" href="<?php echo esc_url( $store_url ); ?>">
                        <span class="invizo-checkout-editorial__back-icon" aria-hidden="true">←</span>
                        <span class="invizo-checkout-editorial__back-text"><?php esc_html_e( 'Return to plans', 'invizo-subscriptions-lite' ); ?></span>
                    </a>
                    <section class="invizo-checkout-editorial__form-col" aria-label="<?php esc_attr_e( 'Checkout', 'invizo-subscriptions-lite' ); ?>">
                        <div class="invizo-checkout-editorial__form-inner">
                            <header class="invizo-checkout-editorial__form-head">
                                <h2 class="invizo-checkout-editorial__form-title"><?php esc_html_e( 'Complete subscription', 'invizo-subscriptions-lite' ); ?></h2>
                                <p class="invizo-checkout-editorial__form-lead"><?php esc_html_e( 'Secure your access in a few steps.', 'invizo-subscriptions-lite' ); ?></p>
                            </header>

                            <form id="invizo-payment-form" class="invizo-checkout-editorial__form" novalidate>
                                <input type="hidden" id="invizo-plan-id" value="<?php echo esc_attr( (string) $selected_plan_id ); ?>" />

                                <div class="invizo-checkout-editorial__step">
                                    <div class="invizo-checkout-editorial__step-head">
                                        <span class="invizo-checkout-editorial__step-num" aria-hidden="true">1</span>
                                        <h3 class="invizo-checkout-editorial__step-title"><?php esc_html_e( 'Your details', 'invizo-subscriptions-lite' ); ?></h3>
                                    </div>
                                    <div class="invizo-checkout-editorial__fields">
                                        <div class="invizo-checkout-editorial__field">
                                            <label class="invizo-checkout-editorial__label" for="invizo-fname"><?php esc_html_e( 'Full name', 'invizo-subscriptions-lite' ); ?></label>
                                            <input type="text" id="invizo-fname" name="invizo_name" autocomplete="name" class="invizo-checkout-editorial__input" placeholder="<?php esc_attr_e( 'Your name', 'invizo-subscriptions-lite' ); ?>" value="<?php echo esc_attr( $prefill_name ); ?>" />
                                        </div>
                                        <div class="invizo-checkout-editorial__field">
                                            <label class="invizo-checkout-editorial__label" for="invizo-email"><?php esc_html_e( 'Email address', 'invizo-subscriptions-lite' ); ?></label>
                                            <input type="email" id="invizo-email" name="invizo_email" autocomplete="email" class="invizo-checkout-editorial__input" placeholder="<?php esc_attr_e( 'you@example.com', 'invizo-subscriptions-lite' ); ?>" value="<?php echo esc_attr( $prefill_email ); ?>" />
                                        </div>
                                        <div class="invizo-checkout-editorial__field">
                                            <label class="invizo-checkout-editorial__label" for="invizo-billing-country"><?php esc_html_e( 'Country', 'invizo-subscriptions-lite' ); ?></label>
                                            <select id="invizo-billing-country" name="billing_country" class="invizo-checkout-editorial__select" autocomplete="country">
                                                <option value=""><?php esc_html_e( 'Select country', 'invizo-subscriptions-lite' ); ?></option>
                                                <?php foreach ( $countries as $code => $clabel ) : ?>
                                                    <option value="<?php echo esc_attr( $code ); ?>"><?php echo esc_html( $clabel ); ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                        <div class="invizo-checkout-editorial__fields-row">
                                            <div class="invizo-checkout-editorial__field">
                                                <label class="invizo-checkout-editorial__label" for="invizo-billing-city"><?php esc_html_e( 'City', 'invizo-subscriptions-lite' ); ?></label>
                                                <input type="text" id="invizo-billing-city" name="billing_city" autocomplete="address-level2" class="invizo-checkout-editorial__input" placeholder="<?php esc_attr_e( 'City', 'invizo-subscriptions-lite' ); ?>" />
                                            </div>
                                            <div class="invizo-checkout-editorial__field">
                                                <label class="invizo-checkout-editorial__label" for="invizo-billing-postcode"><?php esc_html_e( 'Postal / ZIP code', 'invizo-subscriptions-lite' ); ?></label>
                                                <input type="text" id="invizo-billing-postcode" name="billing_postcode" autocomplete="postal-code" class="invizo-checkout-editorial__input" placeholder="<?php esc_attr_e( 'ZIP', 'invizo-subscriptions-lite' ); ?>" />
                                            </div>
                                        </div>
                                        <?php if ( ! is_user_logged_in() ) : ?>
                                            <p class="invizo-auth__hint"><?php esc_html_e( 'After checkout, sign in or register on this site with the same email address to view this subscription in your account area.', 'invizo-subscriptions-lite' ); ?></p>
                                        <?php endif; ?>
                                    </div>
                                </div>

                                <div class="invizo-checkout-editorial__step">
                                    <div class="invizo-checkout-editorial__step-head">
                                        <span class="invizo-checkout-editorial__step-num" aria-hidden="true">2</span>
                                        <h3 class="invizo-checkout-editorial__step-title"><?php esc_html_e( 'Payment details', 'invizo-subscriptions-lite' ); ?></h3>
                                    </div>
                                    <p id="invizo-payment-placeholder" class="invizo-checkout-editorial__payment-hint" aria-live="polite">
                                        <?php esc_html_e( 'Secure payment with Stripe. The form loads automatically on this page.', 'invizo-subscriptions-lite' ); ?>
                                    </p>
                                    <div id="invizo-payment-element-wrap" class="invizo-checkout-editorial__payment-wrap invizo-checkout-editorial__payment-wrap--loading" aria-busy="true">
                                        <div class="invizo-checkout-editorial__payment-skeleton" aria-hidden="true">
                                            <div class="invizo-checkout-editorial__payment-skeleton-methods">
                                                <span class="invizo-checkout-editorial__payment-skeleton-chip invizo-checkout-editorial__payment-skeleton-chip--active"></span>
                                                <span class="invizo-checkout-editorial__payment-skeleton-chip"></span>
                                                <span class="invizo-checkout-editorial__payment-skeleton-chip"></span>
                                            </div>
                                            <span class="invizo-checkout-editorial__payment-skeleton-line invizo-checkout-editorial__payment-skeleton-line--label invizo-checkout-editorial__payment-skeleton-line--short"></span>
                                            <span class="invizo-checkout-editorial__payment-skeleton-line invizo-checkout-editorial__payment-skeleton-line--field"></span>
                                            <div class="invizo-checkout-editorial__payment-skeleton-grid">
                                                <span class="invizo-checkout-editorial__payment-skeleton-line invizo-checkout-editorial__payment-skeleton-line--field"></span>
                                                <span class="invizo-checkout-editorial__payment-skeleton-line invizo-checkout-editorial__payment-skeleton-line--field"></span>
                                            </div>
                                            <span class="invizo-checkout-editorial__payment-skeleton-line invizo-checkout-editorial__payment-skeleton-line--label invizo-checkout-editorial__payment-skeleton-line--medium"></span>
                                            <span class="invizo-checkout-editorial__payment-skeleton-line invizo-checkout-editorial__payment-skeleton-line--field"></span>
                                            <span class="invizo-checkout-editorial__payment-skeleton-line invizo-checkout-editorial__payment-skeleton-line--copy"></span>
                                        </div>
                                        <div id="payment-element" class="invizo-checkout-editorial__payment-element"></div>
                                    </div>
                                </div>

                                <div class="invizo-checkout-editorial__form-foot">
                                    <div id="payment-message" class="invizo-checkout-editorial__message invizo-checkout-editorial__message--hidden" role="alert"></div>

                                    <?php if ( $privacy_effective ) : ?>
                                        <div class="invizo-checkout-editorial__privacy invizo-checkout-editorial__privacy--before-legal">
                                            <label class="invizo-checkout-editorial__privacy-label">
                                                <input type="checkbox" id="invizo-privacy-accept" name="invizo_privacy_accept" value="1" class="invizo-checkout-editorial__checkbox" />
                                                <span>
                                                    <?php esc_html_e( 'I have read and agree to the ', 'invizo-subscriptions-lite' ); ?>
                                                    <a href="<?php echo esc_url( $privacy_effective ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Privacy Policy', 'invizo-subscriptions-lite' ); ?></a><?php esc_html_e( '.', 'invizo-subscriptions-lite' ); ?>
                                                </span>
                                            </label>
                                        </div>
                                    <?php endif; ?>

                                    <p class="invizo-checkout-editorial__legal<?php echo esc_attr( $privacy_effective ? ' invizo-checkout-editorial__legal--after-privacy' : '' ); ?>">
                                        <?php esc_html_e( 'By subscribing, you authorize us to charge you according to the plan terms until you cancel.', 'invizo-subscriptions-lite' ); ?>
                                    </p>

                                    <div class="invizo-checkout-editorial__actions">
                                        <button type="button" id="submit" class="invizo-checkout-editorial__submit">
                                            <span id="button-text"><?php echo esc_html( $btn_subscribe ); ?></span>
                                            <span class="invizo-checkout-editorial__submit-arrow" aria-hidden="true">→</span>
                                        </button>
                                        <div class="invizo-checkout-editorial__trust" aria-hidden="true">
                                            <span><?php esc_html_e( 'PCI compliant', 'invizo-subscriptions-lite' ); ?></span>
                                            <span class="invizo-checkout-editorial__trust-dot"></span>
                                            <span><?php esc_html_e( 'Secure encrypted', 'invizo-subscriptions-lite' ); ?></span>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </section>

                    <section class="invizo-checkout-editorial__summary" aria-label="<?php esc_attr_e( 'Order summary', 'invizo-subscriptions-lite' ); ?>">
                        <div class="invizo-checkout-editorial__summary-scroll">
                            <?php if ( $show_plan_picker ) : ?>
                                <div class="invizo-checkout-editorial__plan-picker-wrap">
                                    <label class="invizo-checkout-editorial__plan-picker-label" for="invizo-plan-picker"><?php esc_html_e( 'Plan', 'invizo-subscriptions-lite' ); ?></label>
                                    <select id="invizo-plan-picker" class="invizo-checkout-editorial__plan-picker" aria-label="<?php esc_attr_e( 'Select subscription plan', 'invizo-subscriptions-lite' ); ?>">
                                        <?php foreach ( $plans as $plan ) : ?>
                                            <option value="<?php echo esc_attr( (string) $plan->ID ); ?>" <?php selected( $plan->ID, $selected_plan_id ); ?>>
                                                <?php echo esc_html( $plan->post_title ); ?> — $<?php echo esc_html( get_post_meta( $plan->ID, '_price', true ) ); ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            <?php endif; ?>

                            <div class="invizo-checkout-editorial__brand"><?php echo esc_html( strtoupper( $site_name ) ); ?></div>

                            <div class="invizo-checkout-editorial__summary-head">
                                <div id="invizo-summary-figure" class="invizo-checkout-editorial__figure invizo-checkout-editorial__figure--compact">
                                    <?php if ( $media ) : ?>
                                        <img id="invizo-summary-img" src="<?php echo esc_url( $media ); ?>" alt="" loading="lazy" width="120" height="120" />
                                    <?php else : ?>
                                        <div class="invizo-checkout-editorial__figure-placeholder" aria-hidden="true"></div>
                                    <?php endif; ?>
                                </div>
                                <div class="invizo-checkout-editorial__summary-head-main">
                                    <h1 id="invizo-summary-title" class="invizo-checkout-editorial__title"><?php echo esc_html( $selected_post->post_title ); ?></h1>
                                </div>
                            </div>

                            <div class="invizo-checkout-editorial__pricing-block">
                                <div class="invizo-checkout-editorial__pricing-row">
                                    <span class="invizo-checkout-editorial__pricing-label"><?php esc_html_e( 'Subscription', 'invizo-subscriptions-lite' ); ?></span>
                                    <span id="invizo-summary-recurring-line" class="invizo-checkout-editorial__pricing-value"><?php echo esc_html( $recurring_line ); ?></span>
                                </div>
                                <div
                                    id="invizo-summary-trial-row"
                                    class="invizo-checkout-editorial__pricing-row invizo-checkout-editorial__pricing-row--trial<?php echo esc_attr( $trial_days > 0 ? '' : ' invizo-checkout-editorial__pricing-row--hidden' ); ?>"
                                    <?php echo esc_attr( $trial_days > 0 ? '' : ' hidden' ); ?>
                                >
                                    <span class="invizo-checkout-editorial__pricing-label"><?php esc_html_e( 'Free trial', 'invizo-subscriptions-lite' ); ?></span>
                                    <span id="invizo-summary-trial-line" class="invizo-checkout-editorial__pricing-value"><?php echo $trial_days > 0 ? esc_html( $trial_line ) : ''; ?></span>
                                </div>
                                <div class="invizo-checkout-editorial__pricing-row invizo-checkout-editorial__pricing-row--muted">
                                    <span class="invizo-checkout-editorial__pricing-label"><?php esc_html_e( 'Tax', 'invizo-subscriptions-lite' ); ?></span>
                                    <span class="invizo-checkout-editorial__pricing-value"><?php esc_html_e( 'Calculated at payment', 'invizo-subscriptions-lite' ); ?></span>
                                </div>
                                <div class="invizo-checkout-editorial__pricing-row invizo-checkout-editorial__pricing-row--total">
                                    <span class="invizo-checkout-editorial__pricing-label"><?php esc_html_e( 'Due today', 'invizo-subscriptions-lite' ); ?></span>
                                    <span id="invizo-summary-due-today" class="invizo-checkout-editorial__pricing-value"><?php echo esc_html( $due_today_line ); ?></span>
                                </div>
                            </div>

                            <div class="invizo-checkout-editorial__summary-body">
                                <div class="invizo-checkout-editorial__rule" aria-hidden="true"></div>
                                <ul id="invizo-summary-features" class="invizo-checkout-editorial__feature-list">
                                    <?php foreach ( $feature_lines as $i => $line ) : ?>
                                        <li class="invizo-checkout-editorial__feature">
                                            <span class="invizo-checkout-editorial__feature-icon" aria-hidden="true"><?php echo wp_kses( self::checkout_feature_icon_svg( $i % 3 ), self::get_allowed_svg_html() ); ?></span>
                                            <p class="invizo-checkout-editorial__feature-line"><?php echo esc_html( $line ); ?></p>
                                        </li>
                                    <?php endforeach; ?>
                                </ul>
                            </div>
                        </div>
                        <p class="invizo-checkout-editorial__summary-note">
                            <?php esc_html_e( 'Your subscription will renew automatically. Cancel anytime from your account settings. Taxes calculated at checkout based on location.', 'invizo-subscriptions-lite' ); ?>
                        </p>
                    </section>
                </div>
            </main>
        </div>
        <?php
        return ob_get_clean();
    }

    /**
     * Rotating minimal icons for summary list (matches editorial tone).
     *
     * @return array<string, array<string, bool>>
     */
    protected static function get_allowed_svg_html() {
        return array(
            'path' => array(
                'd'               => true,
                'fill'            => true,
                'stroke'          => true,
                'stroke-linecap'  => true,
                'stroke-linejoin' => true,
                'stroke-width'    => true,
            ),
            'svg'  => array(
                'aria-hidden' => true,
                'class'       => true,
                'fill'        => true,
                'height'      => true,
                'viewbox'     => true,
                'width'       => true,
                'xmlns'       => true,
            ),
        );
    }

    /**
     * Rotating minimal icons for summary list (matches editorial tone).
     *
     * @param int $index 0–2.
     * @return string
     */
    protected static function checkout_feature_icon_svg( $index ) {
        $index = (int) $index % 3;
        if ( $index === 1 ) {
            return '<svg class="invizo-checkout-editorial__svg" width="22" height="22" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 6h16v12H4V6zm2 2v8h12V8H6z" stroke="currentColor" stroke-width="1.5"/><path d="M8 12h8M8 15h5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/></svg>';
        }
        if ( $index === 2 ) {
            return '<svg class="invizo-checkout-editorial__svg" width="22" height="22" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 3l8 4v6c0 5-3.5 9-8 10-4.5-1-8-5-8-10V7l8-4z" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round"/></svg>';
        }
        return '<svg class="invizo-checkout-editorial__svg" width="22" height="22" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 5a2 2 0 012-2h12a2 2 0 012 2v14l-8-3-8 3V5z" stroke="currentColor" stroke-width="1.5"/></svg>';
    }
}
