<?php
/**
 * 247ChatMate — Plan Access Advanced Diagnostics Partial
 *
 * Phase 5-A-11
 * Checkpoint: PLANLICENCEUICONSOLIDATION5A11LOCK
 *
 * Purpose:
 * - Renders secondary technical status details for support and testing.
 *
 * Responsibility:
 * - Keep registry mirror, lifecycle and reconciliation diagnostics away from the light front section.
 * - Preserve existing manual admin-only refresh/sync forms.
 *
 * Does:
 * - Displays existing prepared diagnostic values.
 * - Reuses existing handlers and nonces.
 *
 * Does not:
 * - Activate, restore, remove, downgrade or extend Pro access.
 * - Change LemonSqueezy, Founder Beta, trial registry, widget, chip, phone selector, lead routing or WhatsApp handoff logic.
 */

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

    <div
        class="matewa-license-check-form"
        style="margin:16px 0; padding:12px; border:1px solid #dcdcde; background:#fff;"
    >
        <h3>Paid Licence Registry Mirror</h3>
        <p class="description">
            Manual admin-only advisory sync for the future paid licence registry. LemonSqueezy remains the paid licence source of truth.
        </p>

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

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

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

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

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

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

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

        <form method="post" action="<?php echo esc_url(admin_url('admin.php?page=247chatmate')); ?>">
            <?php wp_nonce_field('matewa_paid_licence_registry_manual_sync_nonce', 'matewa_paid_licence_registry_manual_sync_nonce'); ?>
            <input type="hidden" name="matewa_paid_licence_registry_manual_sync_action" value="1" />
            <p>
                <button type="submit" class="button button-secondary">
                    Manual paid registry sync
                </button>
                <span class="description">Advisory mirror only. This does not activate, block, downgrade, or extend Pro access.</span>
            </p>
        </form>

        <p class="description">
            This manual sync sends plugin/version, site URL/domain, local paid validation status, local plan status, safe Founder Beta/trial advisory status, a hashed paid licence reference, and a manual sync marker only. It does not send IP addresses, visitor identifiers, chat content, lead content, raw WhatsApp numbers, raw admin email, the full raw licence key, or raw Founder Beta invite codes.
        </p>
    </div>


    <div
        class="matewa-license-check-form"
        style="margin:16px 0; padding:12px; border:1px solid #dcdcde; background:#fff;"
    >
        <h3>Paid lifecycle status</h3>
        <p class="description">
            Lifecycle events are advisory only. Paid access is still controlled by LemonSqueezy licence validation.
        </p>

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

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

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

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

            <div class="matewa-license-stat">
                <strong>Last Lifecycle Event</strong>
                <span><?php echo esc_html($paid_lifecycle_event ?: 'Not available'); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Current State Updated</strong>
                <span><?php echo esc_html($paid_lifecycle_current_state_updated); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Fresh Validation Required</strong>
                <span><?php echo esc_html($paid_lifecycle_requires_fresh_validation); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Entitlement Decision</strong>
                <span class="matewa-license-code"><?php echo esc_html($paid_lifecycle_entitlement_decision); ?></span>
            </div>

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

            <div class="matewa-license-stat">
                <strong>Terminal Event</strong>
                <span><?php echo esc_html($paid_lifecycle_terminal); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Grace Warning</strong>
                <span><?php echo esc_html($paid_lifecycle_grace_warning); ?></span>
            </div>

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

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

            <div class="matewa-license-stat">
                <strong>Last Received UTC</strong>
                <span><?php echo esc_html($paid_lifecycle_last_received_utc ?: 'Not available'); ?></span>
            </div>
        </div>

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

        <?php if ($paid_lifecycle_configured) : ?>
            <form method="post" action="<?php echo esc_url(admin_url('admin.php?page=247chatmate')); ?>">
                <?php wp_nonce_field('matewa_paid_lifecycle_status_refresh_nonce', 'matewa_paid_lifecycle_status_refresh_nonce'); ?>
                <input type="hidden" name="matewa_paid_lifecycle_status_refresh_action" value="1" />
                <p>
                    <button type="submit" class="button button-secondary">
                        Refresh paid lifecycle status
                    </button>
                    <span class="description">Manual read-only refresh only. This does not activate, restore, remove, downgrade, or extend Pro access.</span>
                </p>
            </form>
        <?php else : ?>
            <p>
                <button type="button" class="button button-secondary" disabled>Refresh paid lifecycle status</button>
                <span class="description">Add the private lifecycle status secret in config before refreshing.</span>
            </p>
        <?php endif; ?>

        <p class="description">
            This display uses hashed lookup identifiers only and stores the safe status response locally for admin visibility. It does not show secrets, raw payloads, raw email, raw WhatsApp numbers, raw licence keys, Founder Beta invite codes, visitor identifiers, chat content, lead content, or IP addresses.
        </p>



    <div
        class="matewa-license-check-form"
        style="margin:16px 0; padding:12px; border:1px solid #dcdcde; background:#fff;"
    >
        <h3>Lifecycle entitlement warning bridge</h3>
        <p class="description">
            Advisory warning only. This bridge compares safe local/admin status values against the stored lifecycle advisory result. It does not activate, restore, remove, downgrade, or extend Pro access.
        </p>

        <div class="matewa-license-status-grid">
            <div class="matewa-license-stat">
                <strong>Warning Category</strong>
                <span class="matewa-license-code"><?php echo esc_html($paid_lifecycle_warning_code); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Warning Level</strong>
                <span><?php echo esc_html($paid_lifecycle_warning_severity); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Source of Truth</strong>
                <span><?php echo esc_html($paid_lifecycle_warning_source_of_truth); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Entitlement Decision</strong>
                <span class="matewa-license-code">unchanged</span>
            </div>

            <div class="matewa-license-stat">
                <strong>Terminal Guard Active</strong>
                <span><?php echo esc_html($paid_lifecycle_warning_terminal_guard_active); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Fresh Validation Required</strong>
                <span><?php echo esc_html($paid_lifecycle_warning_fresh_validation_required); ?></span>
            </div>

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

            <div class="matewa-license-stat">
                <strong>Local Paid Valid</strong>
                <span><?php echo esc_html($paid_lifecycle_warning_local_paid_valid); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Local Paid Code</strong>
                <span><?php echo esc_html($paid_lifecycle_warning_local_paid_code ?: 'Not available'); ?></span>
            </div>

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

            <div class="matewa-license-stat">
                <strong>Lifecycle Event Category</strong>
                <span><?php echo esc_html($paid_lifecycle_warning_lifecycle_event_category ?: 'Not available'); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Lifecycle Terminal Event</strong>
                <span><?php echo esc_html($paid_lifecycle_warning_lifecycle_terminal_event ?: 'Not available'); ?></span>
            </div>

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

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

            <div class="matewa-license-stat">
                <strong>Lifecycle Data Stale</strong>
                <span><?php echo esc_html($paid_lifecycle_warning_lifecycle_stale); ?></span>
            </div>
        </div>

        <p><?php echo esc_html($paid_lifecycle_warning_message); ?></p>

        <?php if ('' !== $paid_lifecycle_warning_terminal_guard_reason) : ?>
            <p><strong>Guard Reason:</strong> <?php echo esc_html($paid_lifecycle_warning_terminal_guard_reason); ?></p>
        <?php endif; ?>

        <p><strong>Recommended Action:</strong> <?php echo esc_html($paid_lifecycle_warning_recommended_action); ?></p>

        <p class="description">
            This warning bridge reads existing admin-safe status only. It writes no options, creates no secrets, performs no frontend/widget calls, and adds no cron or background enforcement.
        </p>
    </div>


    <div
        class="matewa-license-check-form"
        style="margin:16px 0; padding:12px; border:1px solid #dcdcde; background:#fff;"
    >
        <h3>Paid lifecycle reconciliation / manual recheck</h3>
        <p class="description">
            Lifecycle data is advisory only. Paid Pro access is controlled by LemonSqueezy/local validation. Lifecycle status cannot activate, restore, remove, downgrade, or extend Pro.
        </p>

        <div class="matewa-license-status-grid">
            <div class="matewa-license-stat">
                <strong>Lifecycle Advisory Status</strong>
                <span><?php echo esc_html($paid_lifecycle_stored_status ?: ($paid_lifecycle_status ?: 'Not available')); ?></span>
            </div>

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

            <div class="matewa-license-stat">
                <strong>Lifecycle Event Category</strong>
                <span><?php echo esc_html($paid_lifecycle_warning_lifecycle_event_category ?: 'Not available'); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Lifecycle Terminal Candidate</strong>
                <span><?php echo esc_html($paid_lifecycle_terminal); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Lifecycle Grace Warning</strong>
                <span><?php echo esc_html($paid_lifecycle_grace_warning); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Entitlement Decision</strong>
                <span class="matewa-license-code">unchanged</span>
            </div>

            <div class="matewa-license-stat">
                <strong>Fresh Validation Required</strong>
                <span><?php echo esc_html($paid_lifecycle_warning_fresh_validation_required); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>LemonSqueezy/local Validation</strong>
                <span><?php echo esc_html($paid_lifecycle_warning_local_paid_valid); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Local Pro Entitlement</strong>
                <span><?php echo esc_html($paid_lifecycle_reconciliation_mode); ?></span>
            </div>

            <div class="matewa-license-stat">
                <strong>Validation Source</strong>
                <span><?php echo esc_html($paid_lifecycle_reconciliation_license_source); ?></span>
            </div>

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

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

        <p>
            <strong>Authority:</strong> LemonSqueezy/local validation remains the paid Pro source of truth. Lifecycle and paid registry mirror data are admin-only advisory signals.
        </p>

        <p class="description">
            Use the buttons below only when you want to refresh existing admin-safe snapshots. They reuse existing handlers and do not create new entitlement decisions.
        </p>

        <div style="display:flex; flex-wrap:wrap; gap:8px; align-items:center; margin-top:10px;">
            <?php if ($paid_lifecycle_can_manual_lifecycle_refresh) : ?>
                <form method="post" action="<?php echo esc_url(admin_url('admin.php?page=247chatmate')); ?>" style="margin:0;">
                    <?php wp_nonce_field('matewa_paid_lifecycle_status_refresh_nonce', 'matewa_paid_lifecycle_status_refresh_nonce'); ?>
                    <input type="hidden" name="matewa_paid_lifecycle_status_refresh_action" value="1" />
                    <button type="submit" class="button button-secondary">Manual lifecycle refresh</button>
                </form>
            <?php else : ?>
                <button type="button" class="button button-secondary" disabled>Manual lifecycle refresh</button>
            <?php endif; ?>

            <?php if ($paid_lifecycle_can_manual_paid_recheck) : ?>
                <form method="post" action="<?php echo esc_url(admin_url('admin.php?page=247chatmate')); ?>" style="margin:0;" data-matewa-license-check-form="1">
                    <?php wp_nonce_field('matewa_check_license_nonce', 'matewa_check_license_nonce'); ?>
                    <input type="hidden" name="matewa_check_license_action" value="1" />
                    <button type="submit" class="button button-secondary">Manual paid licence recheck</button>
                </form>
            <?php else : ?>
                <button type="button" class="button button-secondary" disabled>Manual paid licence recheck</button>
            <?php endif; ?>

            <?php if ($paid_lifecycle_can_manual_registry_sync) : ?>
                <form method="post" action="<?php echo esc_url(admin_url('admin.php?page=247chatmate')); ?>" style="margin:0;">
                    <?php wp_nonce_field('matewa_paid_licence_registry_manual_sync_nonce', 'matewa_paid_licence_registry_manual_sync_nonce'); ?>
                    <input type="hidden" name="matewa_paid_licence_registry_manual_sync_action" value="1" />
                    <button type="submit" class="button button-secondary">Manual paid registry sync</button>
                </form>
            <?php else : ?>
                <button type="button" class="button button-secondary" disabled>Manual paid registry sync</button>
            <?php endif; ?>
        </div>

        <p class="description">
            This reconciliation block reads existing safe admin values only. It writes no options itself, creates no secrets, performs no frontend/widget calls, and adds no cron or background lifecycle enforcement.
        </p>
    </div>
