<?php
/**
 * 247ChatMate — Plan Access Trial and Founder Beta Partial
 *
 * Phase 5-A-11
 * Checkpoint: PLANLICENCEUICONSOLIDATION5A11LOCK
 *
 * Purpose:
 * - Renders trial registry and Founder Beta status details as a focused click-through section.
 *
 * Responsibility:
 * - Keep trial/beta display separate from the front Plan and Licence overview.
 * - Preserve existing safe admin-side refresh forms.
 *
 * Does:
 * - Displays prepared registry and countdown values.
 * - Reuses existing forms and nonces where present.
 *
 * Does not:
 * - Start trials directly, validate beta access, call remote registries on render or change plan state.
 * - Change widget, chip, WhatsApp handoff, phone selector or lead routing behaviour.
 */

defined('ABSPATH') || exit;
?>

    <div
        class="matewa-license-check-form"
        style="margin:16px 0; padding:12px; border:1px solid #dcdcde; background:#fff;"
    >
        <h3>Pro Trial Validation</h3>
        <p class="description">
            Start Trial now validates eligibility against the Pro-only registry before the local trial starts.
            The check is admin-side only and uses a signed server-to-server request.
        </p>

        <div class="matewa-license-status-grid">
            <div class="matewa-license-stat">
                <strong>Registry Configured</strong>
                <span><?php echo $trial_registry_configured ? 'Yes' : 'No'; ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Endpoint</strong>
                <span><?php echo esc_html($trial_registry_endpoint ?: 'Not configured'); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Last Registry Code</strong>
                <span class="matewa-license-code"><?php echo esc_html($trial_registry_code); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Registry Status</strong>
                <span><?php echo esc_html($trial_registry_status ?: 'Not available'); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Trial Reference</strong>
                <span><?php echo esc_html($trial_registry_reference ?: 'Not issued'); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Last Checked</strong>
                <span><?php echo esc_html($trial_registry_checked_display); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Registry Trial Start</strong>
                <span><?php echo esc_html($trial_registry_started_display); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Registry Trial Expiry</strong>
                <span><?php echo esc_html($trial_registry_expires_display); ?></span>
            </div>
        </div>

        <?php if ('' !== $trial_registry_message) : ?>
            <p><?php echo esc_html($trial_registry_message); ?></p>
        <?php endif; ?>

        <p class="description">
            Registry matching uses the saved WhatsApp number and site domain. The plugin may send an optional hashed admin email and install UUID as supporting context.
            IP address is not collected, stored, hashed, transmitted, logged, compared, or used by the registry client.
        </p>
    </div>


    <?php if ($founder_beta_should_show_panel) : ?>
    <div
        class="matewa-license-check-form"
        style="margin:16px 0; padding:12px; border:1px solid #dcdcde; background:#fff;"
    >
        <h3>Founder Beta Registry</h3>
        <p class="description">
            Founder Beta registration is admin-side only. It sends the saved site URL, WhatsApp number, install UUID, and optional hashed admin email to the Founder Beta registry.
        </p>

        <div class="matewa-license-status-grid">
            <div class="matewa-license-stat">
                <strong>Founder Registry Configured</strong>
                <span><?php echo $founder_beta_configured ? 'Yes' : 'No'; ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Endpoint</strong>
                <span><?php echo esc_html($founder_beta_endpoint ?: 'Not configured'); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Last Founder Code</strong>
                <span class="matewa-license-code"><?php echo esc_html($founder_beta_code); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Founder Status</strong>
                <span><?php echo esc_html($founder_beta_status ?: ($founder_beta_success ? 'active' : 'Not available')); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Founder Beta Start</strong>
                <span><?php echo esc_html($founder_beta_start_display); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Founder Beta Expiry</strong>
                <span><?php echo esc_html($founder_beta_expiry_display); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Last Checked</strong>
                <span><?php echo esc_html($founder_beta_checked_display); ?></span>
            </div>
        </div>

        <?php if ('' !== $founder_beta_message) : ?>
            <p><?php echo esc_html($founder_beta_message); ?></p>
        <?php endif; ?>

        <div class="matewa-license-status-box <?php echo esc_attr($founder_beta_countdown_class); ?>">
            <h3><?php echo esc_html($founder_beta_countdown_heading); ?></h3>
            <p><?php echo esc_html($founder_beta_countdown_message); ?></p>
            <p><?php echo esc_html($founder_beta_countdown_extra); ?></p>

            <div class="matewa-license-status-grid">
                <div class="matewa-license-stat">
                    <strong>Founder Beta Days Remaining</strong>
                    <span><?php echo $founder_beta_is_registered ? esc_html($founder_beta_days_label) : 'Not available'; ?></span>
                </div>

                <div class="matewa-license-stat">
                    <strong>Founder Beta Expiry Source</strong>
                    <span><?php echo $founder_beta_has_expiry ? 'Local registry response' : 'Not available'; ?></span>
                </div>

                <div class="matewa-license-stat">
                    <strong>Founder Conversion Window</strong>
                    <span>
                        <?php
                        if (!$founder_beta_is_registered) {
                            echo 'Not available';
                        } elseif ($founder_beta_is_expired) {
                            echo 'Expired';
                        } elseif ($founder_beta_days_remaining <= 7) {
                            echo 'Final 7 days';
                        } elseif ($founder_beta_days_remaining <= 14) {
                            echo 'Final 14 days';
                        } elseif ($founder_beta_days_remaining <= 30) {
                            echo 'Final 30 days';
                        } else {
                            echo 'Not yet';
                        }
                        ?>
                    </span>
                </div>
            </div>

            <?php if ($founder_beta_show_founder_annual_cta) : ?>
                <div
                    class="matewa-founder-annual-cta"
                    style="margin-top:12px; padding:12px; border:1px solid #c3c4c7; background:#fff;"
                >
                    <h4>Founder Annual conversion</h4>
                    <p>Move to Founder Annual before Founder Beta expires to keep paid access active.</p>
                    <p>
                        <a
                            class="button button-primary matewa-checkout-nudge-trigger"
                            href="<?php echo esc_url($founder_annual_checkout_url); ?>"
                            target="_blank"
                            rel="noopener noreferrer"
                            data-matewa-checkout-nudge-trigger="1"
                        >
                            <?php echo esc_html($founder_annual_checkout_label); ?>
                        </a>
                    </p>
                    <p class="description">
                        The checkout URL is isolated here so it can be replaced with the LemonSqueezy Founder Annual URL later.
                    </p>
                    <p class="description">
                        Future inactive placeholders are reserved for beta feedback, positive feedback to Google review flow,
                        founder referral invites, and weekly lead summary email value messaging.
                    </p>
                </div>
            <?php endif; ?>
        </div>

        <?php if ($founder_beta_whatsapp_saved && '' !== $founder_beta_saved_access_code) : ?>
            <form method="post" action="<?php echo esc_url(admin_url('admin.php?page=247chatmate')); ?>">
                <?php wp_nonce_field('matewa_founder_beta_nonce', 'matewa_founder_beta_nonce'); ?>
                <input type="hidden" name="matewa_founder_beta_action" value="1" />
                <p>
                    <button type="submit" class="button button-secondary">
                        Refresh Founder Beta Registration
                    </button>
                    <span class="description">This does not call the frontend widget and does not change chip or WhatsApp handoff behaviour.</span>
                </p>
            </form>
        <?php elseif (!$founder_beta_whatsapp_saved) : ?>
            <p><strong>Add and save a WhatsApp number before refreshing Founder Beta.</strong></p>
        <?php else : ?>
            <p class="description">Founder Beta refresh is hidden until a private access key is saved in the licence key field.</p>
        <?php endif; ?>

        <p class="description">
            IP address is not collected, stored, hashed, transmitted, logged, compared, or used by the Founder Beta registry client.
        </p>
    </div>

    <?php endif; ?>
