<?php
if (!defined('ABSPATH')) die('Restricted Access');
if (!current_user_can('manage_options')) {
    wp_die(esc_html__('You do not have permission to access this page.', 'ai-flash-tune'));
}

$tracking = class_exists('AIFLT_System_Audit') ? AIFLT_System_Audit::get_tracking_audit() : [];
$conversion = class_exists('AIFLT_System_Audit') ? AIFLT_System_Audit::get_purchase_conversion_audit() : [];
$cart = class_exists('AIFLT_System_Audit') ? AIFLT_System_Audit::get_cart_recovery_audit() : [];
$ai = class_exists('AIFLT_System_Audit') ? AIFLT_System_Audit::get_ai_audit() : [];
$readiness = class_exists('AIFLT_System_Audit') ? AIFLT_System_Audit::readiness_items() : [];
$usage_recent = class_exists('AIFLT_AI_Usage_Log') ? AIFLT_AI_Usage_Log::recent(10) : [];

function aiflt_help_status_badge($ok) {
    $class = $ok ? 'ai-flash-tune-status-good' : 'ai-flash-tune-status-warning';
    $label = $ok ? __('Ready', 'ai-flash-tune') : __('Needs check', 'ai-flash-tune');
    return '<span class="ai-flash-tune-status-badge ' . esc_attr($class) . '">' . esc_html($label) . '</span>';
}
?>
<div class="ai-flash-tune-admin-wrapper">
    <?php include_once AIFLT_PLUGIN_PATH . 'includes/admin/ai-flash-tune-admin-sidebar.php'; ?>
    <div class="ai-flash-tune-main-content">
        <div class="ai-flash-tune-page-header ai-flash-tune-mb-8">
            <div>
                <span class="ai-flash-tune-main-title"><?php echo esc_html__('Help & QA Center', 'ai-flash-tune'); ?></span>
                <p class="aift-page-subtitle"><?php echo esc_html__('Use this page before every serious release to confirm tracking, conversion accuracy, privacy, AI wiring, and addon readiness.', 'ai-flash-tune'); ?></p>
            </div>
        </div>

        <div class="ai-flash-tune-card ai-flash-tune-mb-8">
            <div class="aift-card-heading-row">
                <div>
                    <h3><?php echo esc_html__('Release Readiness Snapshot', 'ai-flash-tune'); ?></h3>
                    <p><?php echo esc_html__('These checks do not replace manual QA, but they quickly show whether the plugin is ready for deeper testing.', 'ai-flash-tune'); ?></p>
                </div>
                <span class="ai-flash-tune-status-badge ai-flash-tune-status-waiting"><?php echo esc_html__('Developer QA', 'ai-flash-tune'); ?></span>
            </div>
            <div class="aift-readiness-grid">
                <?php foreach ($readiness as $item): ?>
                    <div class="aift-readiness-item">
                        <div>
                            <strong><?php echo esc_html($item['label']); ?></strong>
                        </div>
                        <?php echo wp_kses_post(aiflt_help_status_badge(!empty($item['ok']))); ?>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>

        <div class="ai-flash-tune-grid ai-flash-tune-grid-cols-lg-2 ai-flash-tune-gap-6 ai-flash-tune-mb-8">
            <div class="ai-flash-tune-card">
                <h3><?php echo esc_html__('Tracking QA', 'ai-flash-tune'); ?></h3>
                <ul class="aift-check-list">
                    <li><?php echo esc_html__('WooCommerce active:', 'ai-flash-tune'); ?> <strong><?php echo !empty($tracking['woocommerce_active']) ? esc_html__('Yes', 'ai-flash-tune') : esc_html__('No', 'ai-flash-tune'); ?></strong></li>
                    <li><?php echo esc_html__('Tracking enabled:', 'ai-flash-tune'); ?> <strong><?php echo !empty($tracking['tracking_enabled']) ? esc_html__('Yes', 'ai-flash-tune') : esc_html__('No', 'ai-flash-tune'); ?></strong></li>
                    <li><?php echo esc_html__('Funnel rows:', 'ai-flash-tune'); ?> <strong><?php echo esc_html(number_format_i18n($tracking['funnel_rows'] ?? 0)); ?></strong></li>
                    <li><?php echo esc_html__('Behavior rows:', 'ai-flash-tune'); ?> <strong><?php echo esc_html(number_format_i18n($tracking['behavior_rows'] ?? 0)); ?></strong></li>
                    <li><?php echo esc_html__('Recent 7-day events:', 'ai-flash-tune'); ?> <strong><?php echo esc_html(number_format_i18n($tracking['recent_7_day_events'] ?? 0)); ?></strong></li>
                </ul>
                <p class="ai-flash-tune-input-description"><?php echo esc_html__('Manual test: open an incognito window, visit a product page, add to cart, open cart, open checkout, place a test order, then confirm each step appears in Funnel Analysis.', 'ai-flash-tune'); ?></p>
            </div>

            <div class="ai-flash-tune-card">
                <h3><?php echo esc_html__('Purchase / Conversion Audit', 'ai-flash-tune'); ?></h3>
                <ul class="aift-check-list">
                    <li><?php echo esc_html__('Checkout success events:', 'ai-flash-tune'); ?> <strong><?php echo esc_html(number_format_i18n($conversion['sales_events'] ?? 0)); ?></strong></li>
                    <li><?php echo esc_html__('Events with order ID:', 'ai-flash-tune'); ?> <strong><?php echo esc_html(number_format_i18n($conversion['order_id_events'] ?? 0)); ?></strong></li>
                </ul>
                <p class="ai-flash-tune-input-description"><?php echo esc_html($conversion['recommendation'] ?? __('Verify order status handling manually.', 'ai-flash-tune')); ?></p>
            </div>
        </div>

        <div class="ai-flash-tune-grid ai-flash-tune-grid-cols-lg-2 ai-flash-tune-gap-6 ai-flash-tune-mb-8">
            <div class="ai-flash-tune-card">
                <h3><?php echo esc_html__('Cart Recovery Audit', 'ai-flash-tune'); ?></h3>
                <ul class="aift-check-list">
                    <li><?php echo esc_html__('Recovery token table:', 'ai-flash-tune'); ?> <strong><?php echo !empty($cart['token_table']) ? esc_html__('Ready', 'ai-flash-tune') : esc_html__('Not found / addon inactive', 'ai-flash-tune'); ?></strong></li>
                    <li><?php echo esc_html__('Email log table:', 'ai-flash-tune'); ?> <strong><?php echo !empty($cart['email_log_table']) ? esc_html__('Ready', 'ai-flash-tune') : esc_html__('Not found / addon inactive', 'ai-flash-tune'); ?></strong></li>
                    <li><?php echo esc_html__('Recovery tokens:', 'ai-flash-tune'); ?> <strong><?php echo esc_html(number_format_i18n($cart['tokens'] ?? 0)); ?></strong></li>
                    <li><?php echo esc_html__('Recovered orders:', 'ai-flash-tune'); ?> <strong><?php echo esc_html(number_format_i18n($cart['recovered_orders'] ?? 0)); ?></strong></li>
                </ul>
            </div>

            <div class="ai-flash-tune-card">
                <h3><?php echo esc_html__('AI Registry & Provider Audit', 'ai-flash-tune'); ?></h3>
                <ul class="aift-check-list">
                    <li><?php echo esc_html__('Provider:', 'ai-flash-tune'); ?> <strong><?php echo esc_html($ai['provider_status'] ?? __('Unknown', 'ai-flash-tune')); ?></strong></li>
                    <li><?php echo esc_html__('Registered actions:', 'ai-flash-tune'); ?> <strong><?php echo esc_html(number_format_i18n($ai['registered_actions'] ?? 0)); ?></strong></li>
                    <li><?php echo esc_html__('Missing wrapper codes:', 'ai-flash-tune'); ?> <strong><?php echo esc_html(number_format_i18n(count($ai['missing_wrapper_codes'] ?? []))); ?></strong></li>
                </ul>
                <?php if (!empty($ai['missing_wrapper_codes'])): ?>
                    <p class="ai-flash-tune-input-description"><?php echo esc_html(implode(', ', $ai['missing_wrapper_codes'])); ?></p>
                <?php endif; ?>
            </div>
        </div>

        <div class="ai-flash-tune-card ai-flash-tune-mb-8">
            <h3><?php echo esc_html__('Developer Tick List Before Next Phase', 'ai-flash-tune'); ?></h3>
            <div class="aift-qa-columns">
                <ul class="aift-check-list">
                    <li>[ ] <?php echo esc_html__('Fresh install creates required tables.', 'ai-flash-tune'); ?></li>
                    <li>[ ] <?php echo esc_html__('Incognito product view is tracked.', 'ai-flash-tune'); ?></li>
                    <li>[ ] <?php echo esc_html__('Add-to-cart is tracked once, not duplicated.', 'ai-flash-tune'); ?></li>
                    <li>[ ] <?php echo esc_html__('Checkout visit and successful test order are tracked correctly.', 'ai-flash-tune'); ?></li>
                    <li>[ ] <?php echo esc_html__('Failed/cancelled/refunded orders are not counted as successful conversions.', 'ai-flash-tune'); ?></li>
                </ul>
                <ul class="aift-check-list">
                    <li>[ ] <?php echo esc_html__('Local AI mode makes no external request.', 'ai-flash-tune'); ?></li>
                    <li>[ ] <?php echo esc_html__('OpenAI mode works only with key and consent.', 'ai-flash-tune'); ?></li>
                    <li>[ ] <?php echo esc_html__('Zywrap mode sends wrapperCodes from the registry.', 'ai-flash-tune'); ?></li>
                    <li>[ ] <?php echo esc_html__('AI payloads do not contain email, phone, address, IP, token, or raw private notes.', 'ai-flash-tune'); ?></li>
                    <li>[ ] <?php echo esc_html__('AI usage log records success, local fallback, and provider errors.', 'ai-flash-tune'); ?></li>
                </ul>
            </div>
        </div>

        <div class="ai-flash-tune-card">
            <h3><?php echo esc_html__('Recent AI Usage Log', 'ai-flash-tune'); ?></h3>
            <?php if (empty($usage_recent)): ?>
                <p><?php echo esc_html__('No AI usage has been logged yet.', 'ai-flash-tune'); ?></p>
            <?php else: ?>
                <div class="aift-table-wrap">
                    <table class="wp-list-table widefat striped">
                        <thead><tr><th><?php echo esc_html__('Time', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Action', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Provider', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Status', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Wrapper', 'ai-flash-tune'); ?></th></tr></thead>
                        <tbody>
                        <?php foreach ($usage_recent as $row): ?>
                            <tr>
                                <td><?php echo esc_html($row['created_at'] ?? ''); ?></td>
                                <td><?php echo esc_html($row['action_key'] ?? ''); ?></td>
                                <td><?php echo esc_html($row['provider'] ?? ''); ?></td>
                                <td><?php echo esc_html($row['status'] ?? ''); ?></td>
                                <td><?php echo esc_html($row['wrapper_code'] ?? ''); ?></td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>
