<?php
/**
 * Returns Page — [wpnest_rfw_returns_page] shortcode.
 *
 * Branches:
 *  1. ?order=<id>         — logged-in customer wizard (3 steps)
 *  2. ?token=<guest_token> — guest wizard after successful verify
 *  3. logged-in, no params — list of eligible orders
 *  4. logged-out, no params — guest verify form (email + order_key)
 *
 * @package WPNest_Returns_For_WooCommerce
 */

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

class WPNest_RFW_Returns_Page {

    private static $instance = null;

    public static function get_instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        add_shortcode( 'wpnest_rfw_returns_page', array( $this, 'render_shortcode' ) );
        add_action( 'template_redirect', array( $this, 'maybe_force_404' ), 1 );
    }

    /**
     * Forces a real 404 on the /returns/ page when the admin disabled the
     * "Returns page" toggle. Wizard URLs (?order= or ?token=) still work so
     * the My Account "Return this order" button never lands on a dead page.
     */
    public function maybe_force_404() {
        $page_id = (int) get_option( 'wpnest_rfw_page_id', 0 );
        if ( $page_id <= 0 || ! is_page( $page_id ) ) {
            return;
        }
        if ( get_option( 'wpnest_rfw_show_on_returns_page', 1 ) ) {
            return;
        }

        // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Public read-only routing on the shortcode page.
        $has_order = ! empty( $_GET['order'] );
        $has_token = ! empty( $_GET['token'] );
        // phpcs:enable WordPress.Security.NonceVerification.Recommended

        if ( $has_order || $has_token ) {
            return;
        }

        global $wp_query;
        $wp_query->set_404();
        status_header( 404 );
        nocache_headers();

        $template = get_404_template();
        if ( $template ) {
            include $template;
            exit;
        }
    }

    public function render_shortcode( $atts = array() ) {
        if ( ! get_option( 'wpnest_rfw_enabled', 1 ) ) {
            return '';
        }

        $atts = shortcode_atts(
            array(
                // Admin-editable knobs: pass attributes in WP page editor to suppress sections.
                // Defaults preserve full UX; `[wpnest_rfw_returns_page show_resources="0"]` hides PDFs.
                'show_resources'  => '1',
                'show_login_hint' => '1',
                'show_intro'      => '1',
            ),
            $atts,
            'wpnest_rfw_returns_page'
        );
        $atts = apply_filters( 'wpnest_rfw_returns_page_atts', $atts );

        // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Public read-only routing on the shortcode page.
        $order_id_param  = isset( $_GET['order'] ) ? (int) $_GET['order'] : 0;
        $token_param     = isset( $_GET['token'] ) ? sanitize_text_field( wp_unslash( $_GET['token'] ) ) : '';
        $email_token     = isset( $_GET['t'] ) ? sanitize_text_field( wp_unslash( $_GET['t'] ) ) : '';
        // phpcs:enable WordPress.Security.NonceVerification.Recommended

        // Email-link path: `?order=ID&t=TOKEN` z maila — token waliduje order_id
        // przez HMAC, plugin wystawia jednorazowy guest_token i redirectuje
        // klienta wprost do wizardu (bez wpisywania email + numer).
        if ( $order_id_param > 0 && '' !== $email_token && ! headers_sent() ) {
            if ( WPNest_RFW_Email_Token::validate( $order_id_param, $email_token ) ) {
                $order = wc_get_order( $order_id_param );
                if ( $order instanceof WC_Order ) {
                    $eligibility = WPNest_RFW_Eligibility::evaluate( $order_id_param );
                    if ( ! empty( $eligibility['eligible'] ) ) {
                        $guest_token = WPNest_RFW_Guest_Handler::generate_guest_token( $order_id_param );
                        $target      = add_query_arg(
                            array(
                                'token' => $guest_token,
                                't'     => false,
                                'order' => false,
                            ),
                            get_permalink()
                        );
                        wp_safe_redirect( $target );
                        exit;
                    }
                }
            }
            // Token invalid lub order nieeligible — fallback do guest form
            // z pre-fillem danych (zobaczy istniejące zamówienie, jasny komunikat).
        }

        ob_start();
        ?>
        <div class="wpnest-rfw-app wpnest-rfw-returns-page">
            <?php
            if ( $order_id_param > 0 && is_user_logged_in() ) {
                $this->render_wizard_for_owned_order( $order_id_param );
            } elseif ( '' !== $token_param ) {
                $this->render_wizard_for_guest_token( $token_param );
            } elseif ( is_user_logged_in() ) {
                $this->render_logged_in_section( $atts );
            } else {
                $this->render_guest_section( $atts, $order_id_param, $email_token );
            }
            if ( '1' === (string) $atts['show_resources'] ) {
                $this->render_resources_section();
            }
            ?>
        </div>
        <?php
        return (string) ob_get_clean();
    }

    private function render_wizard_for_owned_order( $order_id ) {
        $order = wc_get_order( (int) $order_id );
        if ( ! $order instanceof WC_Order || (int) $order->get_customer_id() !== get_current_user_id() ) {
            $this->render_notice( __( 'This order is not available for return.', 'wpnest-returns-for-woocommerce' ) );
            return;
        }
        $eligibility = WPNest_RFW_Eligibility::evaluate( (int) $order_id );
        if ( empty( $eligibility['eligible'] ) ) {
            $this->render_notice( $eligibility['reason'] ? $eligibility['reason'] : __( 'This order is not eligible for return.', 'wpnest-returns-for-woocommerce' ) );
            return;
        }
        $this->render_wizard( $order, '' );
    }

    private function render_wizard_for_guest_token( $token ) {
        $order_id = WPNest_RFW_Guest_Handler::validate_guest_token( $token );
        if ( ! $order_id ) {
            $this->render_notice( __( 'Your session has expired. Please verify again.', 'wpnest-returns-for-woocommerce' ) );
            $this->render_guest_section();
            return;
        }
        $order = wc_get_order( (int) $order_id );
        if ( ! $order instanceof WC_Order ) {
            $this->render_notice( __( 'Order not found.', 'wpnest-returns-for-woocommerce' ) );
            return;
        }
        $eligibility = WPNest_RFW_Eligibility::evaluate( (int) $order_id );
        if ( empty( $eligibility['eligible'] ) ) {
            $this->render_notice( $eligibility['reason'] ? $eligibility['reason'] : __( 'This order is not eligible for return.', 'wpnest-returns-for-woocommerce' ) );
            return;
        }
        $this->render_wizard( $order, $token );
    }

    private function render_notice( $message ) {
        ?>
        <div class="wpnest-rfw-empty"><?php echo esc_html( $message ); ?></div>
        <?php
    }

    /**
     * Renders the 3-step wizard (Items → Reasons → Summary) entirely server-side.
     * Step navigation is client-side (CSS toggle); only final submit is AJAX.
     *
     * @param WC_Order $order
     * @param string   $guest_token  Empty for logged-in flow.
     */
    private function render_wizard( WC_Order $order, $guest_token = '' ) {
        $items   = array();
        foreach ( $order->get_items() as $oi_id => $oi ) {
            if ( $oi instanceof WC_Order_Item_Product ) {
                $items[ (int) $oi_id ] = $oi;
            }
        }
        if ( empty( $items ) ) {
            $this->render_notice( __( 'This order has no returnable items.', 'wpnest-returns-for-woocommerce' ) );
            return;
        }
        $reasons = WPNest_RFW_Reason_Codes::get_all();
        ?>
        <section class="wpnest-rfw-section wpnest-rfw-wizard"
                 data-order-id="<?php echo esc_attr( (int) $order->get_id() ); ?>"
                 data-order-number="<?php echo esc_attr( $order->get_order_number() ); ?>"
                 data-guest-token="<?php echo esc_attr( $guest_token ); ?>"
                 data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_rest' ) ); ?>">

            <header class="wpnest-rfw-wizard__head">
                <h2><?php
                /* translators: %s: order number */
                printf( esc_html__( 'Return order #%s', 'wpnest-returns-for-woocommerce' ), esc_html( $order->get_order_number() ) ); ?></h2>
                <ol class="wpnest-rfw-stepper">
                    <li class="is-active" data-rfw-step-tab="1"><span>1</span> <?php esc_html_e( 'Items', 'wpnest-returns-for-woocommerce' ); ?></li>
                    <li data-rfw-step-tab="2"><span>2</span> <?php esc_html_e( 'Reasons', 'wpnest-returns-for-woocommerce' ); ?></li>
                    <li data-rfw-step-tab="3"><span>3</span> <?php esc_html_e( 'Summary', 'wpnest-returns-for-woocommerce' ); ?></li>
                </ol>
            </header>

            <form class="wpnest-rfw-wizard__form" novalidate>

                <!-- Step 1: items -->
                <div class="wpnest-rfw-step is-active" data-rfw-step="1">
                    <p class="wpnest-rfw-help"><?php esc_html_e( 'Select the items you want to return and adjust the quantity if you bought more than one.', 'wpnest-returns-for-woocommerce' ); ?></p>

                    <div class="wpnest-rfw-items">
                        <?php foreach ( $items as $oi_id => $oi ) :
                            $product   = $oi->get_product();
                            $img       = $product ? $product->get_image( array( 64, 64 ) ) : '';
                            $name      = $oi->get_name();
                            $qty_avail = max( 1, (int) $oi->get_quantity() );
                            $line_meta = wc_display_item_meta( $oi, array( 'echo' => false ) );
                            $price_html = $order->get_formatted_line_subtotal( $oi );
                        ?>
                            <label class="wpnest-rfw-item" data-rfw-item="<?php echo esc_attr( $oi_id ); ?>">
                                <input type="checkbox" name="select" value="1" class="wpnest-rfw-item__check">
                                <?php if ( $img ) : ?>
                                    <span class="wpnest-rfw-item__thumb"><?php echo wp_kses_post( $img ); ?></span>
                                <?php endif; ?>
                                <span class="wpnest-rfw-item__body">
                                    <span class="wpnest-rfw-item__name"><?php echo esc_html( $name ); ?></span>
                                    <?php if ( $line_meta ) : ?>
                                        <span class="wpnest-rfw-item__meta"><?php echo wp_kses_post( $line_meta ); ?></span>
                                    <?php endif; ?>
                                    <span class="wpnest-rfw-item__price"><?php echo wp_kses_post( $price_html ); ?></span>
                                </span>
                                <span class="wpnest-rfw-item__qty">
                                    <span class="wpnest-rfw-item__qty-label"><?php esc_html_e( 'Qty', 'wpnest-returns-for-woocommerce' ); ?></span>
                                    <input type="number" name="qty" min="1" max="<?php echo esc_attr( $qty_avail ); ?>" value="<?php echo esc_attr( $qty_avail ); ?>" class="wpnest-rfw-item__qty-input">
                                    <span class="wpnest-rfw-item__qty-max"><?php
                                        /* translators: %d: max returnable quantity */
                                        printf( esc_html__( 'of %d', 'wpnest-returns-for-woocommerce' ), (int) $qty_avail ); ?></span>
                                </span>
                            </label>
                        <?php endforeach; ?>
                    </div>

                    <div class="wpnest-rfw-step__actions">
                        <button type="button" class="button alt btn btn-primary" data-rfw-next="2"><?php esc_html_e( 'Next', 'wpnest-returns-for-woocommerce' ); ?></button>
                    </div>
                    <div class="wpnest-rfw-feedback" role="status" aria-live="polite"></div>
                </div>

                <!-- Step 2: reasons -->
                <div class="wpnest-rfw-step" data-rfw-step="2">
                    <p class="wpnest-rfw-help"><?php esc_html_e( 'Choose a reason for each item you are returning.', 'wpnest-returns-for-woocommerce' ); ?></p>

                    <div class="wpnest-rfw-reasons" data-rfw-reasons>
                        <?php /* JS clones a template per selected item */ ?>
                    </div>

                    <template data-rfw-reason-tpl>
                        <div class="wpnest-rfw-reason" data-rfw-reason-row>
                            <div class="wpnest-rfw-reason__head">
                                <strong data-rfw-reason-name></strong>
                                <span class="wpnest-rfw-reason__qty" data-rfw-reason-qty></span>
                            </div>
                            <label class="wpnest-rfw-form__row">
                                <span class="wpnest-rfw-form__label"><?php esc_html_e( 'Reason', 'wpnest-returns-for-woocommerce' ); ?></span>
                                <select name="reason_code" required>
                                    <option value=""><?php esc_html_e( 'Choose a reason…', 'wpnest-returns-for-woocommerce' ); ?></option>
                                    <?php foreach ( $reasons as $code => $label ) : ?>
                                        <option value="<?php echo esc_attr( $code ); ?>"><?php echo esc_html( $label ); ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </label>
                            <label class="wpnest-rfw-form__row wpnest-rfw-reason__text" hidden>
                                <span class="wpnest-rfw-form__label"><?php esc_html_e( 'Add details', 'wpnest-returns-for-woocommerce' ); ?></span>
                                <textarea name="reason_text" rows="2" maxlength="1000"></textarea>
                            </label>
                        </div>
                    </template>

                    <div class="wpnest-rfw-step__actions">
                        <button type="button" class="button btn btn-ghost" data-rfw-prev="1"><?php esc_html_e( 'Back', 'wpnest-returns-for-woocommerce' ); ?></button>
                        <button type="button" class="button alt btn btn-primary" data-rfw-next="3"><?php esc_html_e( 'Next', 'wpnest-returns-for-woocommerce' ); ?></button>
                    </div>
                    <div class="wpnest-rfw-feedback" role="status" aria-live="polite"></div>
                </div>

                <!-- Step 3: summary -->
                <div class="wpnest-rfw-step" data-rfw-step="3">
                    <p class="wpnest-rfw-help"><?php esc_html_e( 'Please review your return request before submitting.', 'wpnest-returns-for-woocommerce' ); ?></p>

                    <div class="wpnest-rfw-summary" data-rfw-summary></div>

                    <?php
                    // Extra fields contract for Pro / 3rd-party. Each field is rendered with
                    // basic markup; Pro can also use the `wpnest_rfw_wizard_extra_fields`
                    // action below for fully custom UI.
                    $extra_fields = apply_filters( 'wpnest_rfw_form_fields', array(), $order, $guest_token );
                    if ( is_array( $extra_fields ) && ! empty( $extra_fields ) ) :
                        ?>
                        <div class="wpnest-rfw-form__extras">
                            <?php foreach ( $extra_fields as $field ) :
                                if ( empty( $field['name'] ) || empty( $field['type'] ) ) { continue; }
                                $name     = sanitize_key( (string) $field['name'] );
                                $type     = sanitize_key( (string) $field['type'] );
                                $label    = isset( $field['label'] ) ? (string) $field['label'] : $name;
                                $required = ! empty( $field['required'] );
                                $value    = isset( $field['value'] ) ? (string) $field['value'] : '';
                                $id       = 'wpnest-rfw-extra-' . $name;
                            ?>
                                <p class="wpnest-rfw-form__row">
                                    <?php if ( 'checkbox' === $type ) : ?>
                                        <label class="wpnest-rfw-check">
                                            <input type="checkbox" id="<?php echo esc_attr( $id ); ?>" name="extra[<?php echo esc_attr( $name ); ?>]" value="1" <?php checked( '1', $value ); ?> <?php echo $required ? 'required' : ''; ?>>
                                            <span><?php echo esc_html( $label ); ?></span>
                                        </label>
                                    <?php elseif ( 'textarea' === $type ) : ?>
                                        <label for="<?php echo esc_attr( $id ); ?>"><?php echo esc_html( $label ); ?></label>
                                        <textarea id="<?php echo esc_attr( $id ); ?>" name="extra[<?php echo esc_attr( $name ); ?>]" rows="2" <?php echo $required ? 'required' : ''; ?>><?php echo esc_textarea( $value ); ?></textarea>
                                    <?php else : ?>
                                        <label for="<?php echo esc_attr( $id ); ?>"><?php echo esc_html( $label ); ?></label>
                                        <input type="<?php echo esc_attr( in_array( $type, array( 'text', 'email', 'tel', 'number' ), true ) ? $type : 'text' ); ?>" id="<?php echo esc_attr( $id ); ?>" name="extra[<?php echo esc_attr( $name ); ?>]" value="<?php echo esc_attr( $value ); ?>" <?php echo $required ? 'required' : ''; ?>>
                                    <?php endif; ?>
                                    <?php if ( ! empty( $field['hint'] ) ) : ?>
                                        <small class="wpnest-rfw-form__hint"><?php echo esc_html( $field['hint'] ); ?></small>
                                    <?php endif; ?>
                                </p>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                    <?php
                    // Hook for Pro to render fully custom field UI (file uploads, conditional rows, etc.).
                    do_action( 'wpnest_rfw_wizard_extra_fields', $order, $guest_token );
                    ?>

                    <p class="wpnest-rfw-form__row">
                        <label class="wpnest-rfw-check">
                            <input type="checkbox" name="confirm" value="1" required>
                            <span><?php esc_html_e( 'I understand this initiates a return request and that I will receive a confirmation by email.', 'wpnest-returns-for-woocommerce' ); ?></span>
                        </label>
                    </p>

                    <div class="wpnest-rfw-step__actions">
                        <button type="button" class="button btn btn-ghost" data-rfw-prev="2"><?php esc_html_e( 'Back', 'wpnest-returns-for-woocommerce' ); ?></button>
                        <button type="submit" class="button alt btn btn-primary wpnest-rfw-submit"><?php esc_html_e( 'Submit return request', 'wpnest-returns-for-woocommerce' ); ?></button>
                    </div>
                    <div class="wpnest-rfw-feedback" role="status" aria-live="polite"></div>
                </div>

            </form>
        </section>
        <?php
    }

    private function render_logged_in_section( $atts = array() ) {
        $user_id = get_current_user_id();
        $orders  = wc_get_orders(
            array(
                'customer_id' => $user_id,
                'limit'       => 50,
                'orderby'     => 'date',
                'order'       => 'DESC',
            )
        );

        $eligible = array();
        foreach ( $orders as $order ) {
            if ( $order instanceof WC_Order && WPNest_RFW_Eligibility::is_eligible( $order->get_id() ) ) {
                $eligible[] = $order;
            }
        }
        $page_id = (int) get_option( 'wpnest_rfw_page_id', 0 );
        $base    = $page_id ? get_permalink( $page_id ) : home_url( '/' );

        $current_user = wp_get_current_user();
        $first_name   = trim( (string) $current_user->first_name );
        $show_intro   = '1' === (string) ( $atts['show_intro'] ?? '1' );
        ?>
        <section class="wpnest-rfw-section">
            <?php if ( $show_intro ) : ?>
                <h2 class="wpnest-rfw-section__title">
                    <?php
                    if ( $first_name ) {
                        /* translators: %s: customer first name */
                        printf( esc_html__( 'Hi %s — choose an order to return', 'wpnest-returns-for-woocommerce' ), esc_html( $first_name ) );
                    } else {
                        esc_html_e( 'Choose an order to return', 'wpnest-returns-for-woocommerce' );
                    }
                    ?>
                </h2>
                <p class="wpnest-rfw-help">
                    <?php esc_html_e( 'Pick the order you want to return below. The whole process takes about 60 seconds — you can pick individual items and quantities in the next step.', 'wpnest-returns-for-woocommerce' ); ?>
                </p>
            <?php endif; ?>

            <?php if ( empty( $eligible ) ) : ?>
                <div class="wpnest-rfw-empty wpnest-rfw-empty--friendly">
                    <span class="wpnest-rfw-empty__icon" aria-hidden="true">📦</span>
                    <h3><?php esc_html_e( 'Nothing to return right now', 'wpnest-returns-for-woocommerce' ); ?></h3>
                    <p><?php esc_html_e( 'You have no recent orders eligible for return. The window typically lasts 14 days from the order date.', 'wpnest-returns-for-woocommerce' ); ?></p>
                    <p>
                        <a class="button btn btn-ghost" href="<?php echo esc_url( wc_get_page_permalink( 'shop' ) ); ?>">
                            <?php esc_html_e( 'Continue shopping', 'wpnest-returns-for-woocommerce' ); ?>
                        </a>
                    </p>
                </div>
            <?php else : ?>
                <ul class="wpnest-rfw-orders" role="list">
                    <?php foreach ( $eligible as $order ) :
                        $order_id    = (int) $order->get_id();
                        $items_count = (int) $order->get_item_count();
                        $first_item  = '';
                        foreach ( $order->get_items() as $line_item ) {
                            if ( $line_item instanceof WC_Order_Item_Product ) {
                                $first_item = $line_item->get_name();
                                break;
                            }
                        }
                    ?>
                        <li class="wpnest-rfw-order">
                            <div class="wpnest-rfw-order__head">
                                <span class="wpnest-rfw-order__num">#<?php echo esc_html( $order->get_order_number() ); ?></span>
                                <span class="wpnest-rfw-order__date"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></span>
                            </div>
                            <div class="wpnest-rfw-order__body">
                                <p class="wpnest-rfw-order__items">
                                    <?php
                                    if ( $first_item ) {
                                        if ( $items_count > 1 ) {
                                            printf(
                                                /* translators: 1: first item name, 2: number of additional items */
                                                esc_html( _n( '%1$s and %2$d more item', '%1$s and %2$d more items', $items_count - 1, 'wpnest-returns-for-woocommerce' ) ),
                                                esc_html( $first_item ),
                                                (int) ( $items_count - 1 )
                                            );
                                        } else {
                                            echo esc_html( $first_item );
                                        }
                                    }
                                    ?>
                                </p>
                                <p class="wpnest-rfw-order__total"><?php echo wp_kses_post( $order->get_formatted_order_total() ); ?></p>
                            </div>
                            <div class="wpnest-rfw-order__foot">
                                <a href="<?php echo esc_url( add_query_arg( 'order', $order_id, $base ) ); ?>" class="button alt btn btn-primary wpnest-rfw-btn">
                                    <?php esc_html_e( 'Return this order →', 'wpnest-returns-for-woocommerce' ); ?>
                                </a>
                            </div>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </section>
        <?php
    }

    private function render_guest_section( $atts = array(), $prefill_order_id = 0, $email_token = '' ) {
        $show_intro      = '1' === (string) ( $atts['show_intro'] ?? '1' );
        $show_login_hint = '1' === (string) ( $atts['show_login_hint'] ?? '1' );
        $login_url       = wc_get_page_permalink( 'myaccount' );

        // Pre-fill z linku w mailu — gdy redirect (auto-issue guest_token)
        // nie zadziałał (np. zamówienie nieeligible), wciąż uzupełniamy pola
        // żeby klient nie musiał wpisywać ręcznie.
        $prefill_email        = '';
        $prefill_order_number = '';
        $prefill_warning      = '';
        if ( $prefill_order_id > 0 && '' !== $email_token
            && WPNest_RFW_Email_Token::validate( $prefill_order_id, $email_token ) ) {
            $prefill_order = wc_get_order( $prefill_order_id );
            if ( $prefill_order instanceof WC_Order ) {
                $prefill_email        = $prefill_order->get_billing_email();
                $prefill_order_number = (string) $prefill_order->get_order_number();
                $eligibility          = WPNest_RFW_Eligibility::evaluate( $prefill_order_id );
                if ( empty( $eligibility['eligible'] ) ) {
                    $prefill_warning = ! empty( $eligibility['reason'] )
                        ? $eligibility['reason']
                        : __( 'This order is not eligible for return.', 'wpnest-returns-for-woocommerce' );
                }
            }
        }
        ?>
        <section class="wpnest-rfw-section">
            <?php if ( $show_intro ) : ?>
                <h2 class="wpnest-rfw-section__title"><?php esc_html_e( 'Start your return', 'wpnest-returns-for-woocommerce' ); ?></h2>
                <p class="wpnest-rfw-help">
                    <?php esc_html_e( 'Enter the email and the order number from your confirmation email — takes about 30 seconds.', 'wpnest-returns-for-woocommerce' ); ?>
                </p>
            <?php endif; ?>

            <?php if ( $prefill_warning ) : ?>
                <div class="wpnest-rfw-feedback wpnest-rfw-feedback--error" role="alert">
                    <?php echo esc_html( $prefill_warning ); ?>
                </div>
            <?php endif; ?>

            <form class="wpnest-rfw-guest-form" id="wpnest-rfw-guest-form" novalidate>
                <p class="wpnest-rfw-form__row">
                    <label for="wpnest-rfw-guest-email"><?php esc_html_e( 'Email address', 'wpnest-returns-for-woocommerce' ); ?></label>
                    <input type="email" id="wpnest-rfw-guest-email" name="email" autocomplete="email" required placeholder="<?php esc_attr_e( 'name@example.com', 'wpnest-returns-for-woocommerce' ); ?>" value="<?php echo esc_attr( $prefill_email ); ?>">
                </p>
                <p class="wpnest-rfw-form__row">
                    <label for="wpnest-rfw-guest-order"><?php esc_html_e( 'Order number', 'wpnest-returns-for-woocommerce' ); ?></label>
                    <input type="text" id="wpnest-rfw-guest-order" name="order_number" autocomplete="off" inputmode="numeric" required placeholder="<?php esc_attr_e( '1234', 'wpnest-returns-for-woocommerce' ); ?>" value="<?php echo esc_attr( $prefill_order_number ); ?>">
                    <small class="wpnest-rfw-form__hint">
                        <?php esc_html_e( 'Order number from your confirmation email (e.g. 1234 or #1234) — with or without the # sign.', 'wpnest-returns-for-woocommerce' ); ?>
                    </small>
                </p>
                <p class="wpnest-rfw-form__row wpnest-rfw-form__row--actions">
                    <button type="submit" class="button alt btn btn-primary wpnest-rfw-btn">
                        <?php esc_html_e( 'Continue →', 'wpnest-returns-for-woocommerce' ); ?>
                    </button>
                </p>
                <div class="wpnest-rfw-feedback" role="status" aria-live="polite"></div>
            </form>

            <?php if ( $show_login_hint && $login_url ) : ?>
                <p class="wpnest-rfw-login-hint">
                    <?php
                    printf(
                        /* translators: %s: link to my account login */
                        esc_html__( 'Have an account? %s to skip this step.', 'wpnest-returns-for-woocommerce' ),
                        '<a href="' . esc_url( $login_url ) . '">' . esc_html__( 'Log in', 'wpnest-returns-for-woocommerce' ) . '</a>'
                    );
                    ?>
                </p>
            <?php endif; ?>
        </section>
        <?php
    }

    private function render_resources_section() {
        $en_url = WPNest_RFW_PDF_Generator::get_model_form_url( 'en' );
        $pl_url = WPNest_RFW_PDF_Generator::get_model_form_url( 'pl' );

        if ( ! $en_url && ! $pl_url ) {
            return;
        }
        ?>
        <section class="wpnest-rfw-section">
            <aside class="wpnest-rfw-resources-card">
                <div class="wpnest-rfw-resources-card__icon" aria-hidden="true">📄</div>
                <div class="wpnest-rfw-resources-card__body">
                    <h3><?php esc_html_e( 'Prefer to send a paper form?', 'wpnest-returns-for-woocommerce' ); ?></h3>
                    <p><?php esc_html_e( 'Download the EU model withdrawal form and post it to us — the online flow above does the same thing faster, but the paper option is always available.', 'wpnest-returns-for-woocommerce' ); ?></p>
                    <div class="wpnest-rfw-resources-card__actions">
                        <?php if ( $en_url ) : ?>
                            <a class="button btn btn-ghost" href="<?php echo esc_url( $en_url ); ?>" target="_blank" rel="noopener">
                                <?php esc_html_e( 'Download (EN)', 'wpnest-returns-for-woocommerce' ); ?>
                            </a>
                        <?php endif; ?>
                        <?php if ( $pl_url ) : ?>
                            <a class="button btn btn-ghost" href="<?php echo esc_url( $pl_url ); ?>" target="_blank" rel="noopener">
                                <?php esc_html_e( 'Pobierz (PL)', 'wpnest-returns-for-woocommerce' ); ?>
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            </aside>
        </section>
        <?php
    }
}
