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

$aiflt_page_title = __('Growth Toolkit', 'ai-flash-tune');
$settings = class_exists('AIFLT_World_Class_Tools') ? AIFLT_World_Class_Tools::get_settings() : [];
$changes = class_exists('AIFLT_World_Class_Tools') ? AIFLT_World_Class_Tools::get_change_log(20) : [];
$revisions = class_exists('AIFLT_World_Class_Tools') ? AIFLT_World_Class_Tools::get_product_copy_revisions(20) : [];
$ideas = class_exists('AIFLT_World_Class_Tools') ? AIFLT_World_Class_Tools::get_ab_test_ideas(10) : [];
$ai_stats = class_exists('AIFLT_AI_Usage_Log') ? AIFLT_AI_Usage_Log::stats(30) : ['total'=>0,'success'=>0,'error'=>0,'local'=>0,'by_provider'=>[]];
$ai_logs = class_exists('AIFLT_AI_Usage_Log') ? AIFLT_AI_Usage_Log::recent(12) : [];

$export_html = wp_nonce_url(add_query_arg(['action' => 'aiflt_export_client_report', 'format' => 'html', 'days' => 7], admin_url('admin-post.php')), 'aiflt_export_client_report');
$export_csv = wp_nonce_url(add_query_arg(['action' => 'aiflt_export_client_report', 'format' => 'csv', 'days' => 7], admin_url('admin-post.php')), 'aiflt_export_client_report');

$rollback_url = function($change_id) {
    return wp_nonce_url(add_query_arg(['action' => 'aiflt_rollback_change'], admin_url('admin-post.php')), 'aiflt_rollback_change');
};
?>
<div class="ai-flash-tune-admin-wrapper">
    <?php include_once('ai-flash-tune-admin-sidebar.php'); ?>
    <div class="ai-flash-tune-main-content aiflt-growth-toolkit-pro">
        <div class="ai-flash-tune-page-header ai-flash-tune-mb-8 aiflt-growth-page-header">
            <div>
                <span class="ai-flash-tune-main-title"><?php echo esc_html($aiflt_page_title); ?></span>
                <p class="ai-flash-tune-page-subtitle"><?php echo esc_html__('World-class controls for client reporting, AI audit logs, safe apply/rollback, and product copy governance.', 'ai-flash-tune'); ?></p>
            </div>
        </div>

        <?php if (isset($_GET['aiflt_saved'])) : ?><div class="notice notice-success"><p><?php echo esc_html__('Growth Toolkit settings saved.', 'ai-flash-tune'); ?></p></div><?php endif; ?>
        <?php if (isset($_GET['aiflt_ab_generated'])) : ?><div class="notice notice-success"><p><?php echo esc_html__('A/B test ideas generated.', 'ai-flash-tune'); ?></p></div><?php endif; ?>
        <?php if (isset($_GET['aiflt_rollback'])) : ?><div class="notice notice-success"><p><?php echo esc_html__('Rollback request completed. Review the safe change log for status.', 'ai-flash-tune'); ?></p></div><?php endif; ?>
        <?php if (isset($_GET['aiflt_revision_restored'])) : ?><div class="notice notice-success"><p><?php echo esc_html__('Product copy revision restored.', 'ai-flash-tune'); ?></p></div><?php endif; ?>
        <?php if (isset($_GET['aiflt_email_test'])) : ?><div class="notice <?php echo $_GET['aiflt_email_test'] === 'sent' ? 'notice-success' : 'notice-error'; ?>"><p><?php echo $_GET['aiflt_email_test'] === 'sent' ? esc_html__('Test weekly summary email sent.', 'ai-flash-tune') : esc_html__('Test email could not be sent. Check WordPress mail/SMTP.', 'ai-flash-tune'); ?></p></div><?php endif; ?>
        <div class="ai-flash-tune-card aiflt-pro-card ai-flash-tune-mb-8">
            <span class="aiflt-kicker"><?php echo esc_html__("Final-stage workflow note", "ai-flash-tune"); ?></span>
            <h3><?php echo esc_html__("Where these governance features appear", "ai-flash-tune"); ?></h3>
            <p><?php echo esc_html__("Generated AI reports are saved in AI Usage & Reports. Safe Change Log records reversible checkout/product changes after they are applied. Product Copy Revision History stores before/after product copy snapshots created by Product Optimizer apply actions.", "ai-flash-tune"); ?></p>
            <div class="aiflt-button-row"><a class="ai-flash-tune-btn ai-flash-tune-btn-blue" href="<?php echo esc_url(admin_url("admin.php?page=aiflt-ai-usage-reports")); ?>"><?php echo esc_html__("Open AI Usage & Reports", "ai-flash-tune"); ?></a></div>
        </div>


        <div class="aiflt-growth-command-center ai-flash-tune-mb-8">
            <div class="aiflt-command-card">
                <span><?php echo esc_html__('AI usage audit', 'ai-flash-tune'); ?></span>
                <strong><?php echo esc_html(number_format_i18n($ai_stats['total'] ?? 0)); ?></strong>
                <p><?php echo esc_html__('Provider actions logged in the last 30 days.', 'ai-flash-tune'); ?></p>
            </div>
            <div class="aiflt-command-card">
                <span><?php echo esc_html__('Safe changes', 'ai-flash-tune'); ?></span>
                <strong><?php echo esc_html(number_format_i18n(count($changes))); ?></strong>
                <p><?php echo esc_html__('Before/after records ready for review or rollback.', 'ai-flash-tune'); ?></p>
            </div>
            <div class="aiflt-command-card">
                <span><?php echo esc_html__('Copy revisions', 'ai-flash-tune'); ?></span>
                <strong><?php echo esc_html(number_format_i18n(count($revisions))); ?></strong>
                <p><?php echo esc_html__('Product-copy drafts and applied revisions.', 'ai-flash-tune'); ?></p>
            </div>
            <div class="aiflt-command-card">
                <span><?php echo esc_html__('Report mode', 'ai-flash-tune'); ?></span>
                <strong><?php echo ($settings['white_label_enabled'] ?? '0') === '1' ? esc_html__('White-label', 'ai-flash-tune') : esc_html__('AI Flash Tune', 'ai-flash-tune'); ?></strong>
                <p><?php echo esc_html__('Client-ready HTML and CSV exports.', 'ai-flash-tune'); ?></p>
            </div>
        </div>

        <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" class="ai-flash-tune-mb-8 aiflt-growth-settings-form" autocomplete="off" data-lpignore="true">
            <?php wp_nonce_field('aiflt_world_class_settings'); ?>
            <input type="hidden" name="action" value="aiflt_save_world_class_settings" />
            <div class="aiflt-world-grid aiflt-world-grid-2">
                <div class="ai-flash-tune-card aiflt-world-card aiflt-pro-card">
                    <span class="aiflt-kicker"><?php echo esc_html__('Client report / white-label output', 'ai-flash-tune'); ?></span>
                    <h3><?php echo esc_html__('Client-ready reporting identity', 'ai-flash-tune'); ?></h3>
                    <p><?php echo esc_html__('Use this when you need a polished report for clients, agencies, internal stakeholders, or store owners.', 'ai-flash-tune'); ?></p>
                    <label class="ai-flash-tune-checkbox-control"><input type="checkbox" name="agency_mode" value="1" <?php checked($settings['agency_mode'] ?? '0', '1'); ?> /> <span><?php echo esc_html__('Enable agency mode', 'ai-flash-tune'); ?></span></label>
                    <label class="ai-flash-tune-checkbox-control"><input type="checkbox" name="white_label_enabled" value="1" <?php checked($settings['white_label_enabled'] ?? '0', '1'); ?> /> <span><?php echo esc_html__('Use white-label report identity', 'ai-flash-tune'); ?></span></label>
                    <div class="aiflt-inline-fields">
                        <div><label class="ai-flash-tune-form-label"><?php echo esc_html__('Agency name', 'ai-flash-tune'); ?></label><input class="ai-flash-tune-input" type="text" autocomplete="off" data-lpignore="true" name="agency_name" value="<?php echo esc_attr($settings['agency_name'] ?? ''); ?>" /></div>
                        <div><label class="ai-flash-tune-form-label"><?php echo esc_html__('Client/store label', 'ai-flash-tune'); ?></label><input class="ai-flash-tune-input" type="text" autocomplete="off" data-lpignore="true" name="client_name" value="<?php echo esc_attr($settings['client_name'] ?? ''); ?>" /></div>
                    </div>
                    <label class="ai-flash-tune-form-label"><?php echo esc_html__('Report logo URL', 'ai-flash-tune'); ?></label>
                    <input class="ai-flash-tune-input" type="url" autocomplete="off" data-lpignore="true" name="report_logo_url" value="<?php echo esc_attr($settings['report_logo_url'] ?? ''); ?>" />
                    <div class="aiflt-chip-list">
                        <?php foreach(['summary'=>'Summary','funnel'=>'Funnel','products'=>'Products','carts'=>'Carts','checkout'=>'Checkout','next_steps'=>'Next steps'] as $key=>$label): ?>
                            <label class="aiflt-chip-check"><input type="checkbox" name="client_report_sections[]" value="<?php echo esc_attr($key); ?>" <?php checked(in_array($key, $settings['client_report_sections'] ?? [], true)); ?> /> <span><?php echo esc_html($label); ?></span></label>
                        <?php endforeach; ?>
                    </div>
                    <div class="aiflt-button-row">
                        <a class="ai-flash-tune-btn ai-flash-tune-btn-blue" href="<?php echo esc_url($export_html); ?>"><?php echo esc_html__('Export client HTML report', 'ai-flash-tune'); ?></a>
                        <a class="ai-flash-tune-btn ai-flash-tune-btn-gray" href="<?php echo esc_url($export_csv); ?>"><?php echo esc_html__('Export evidence CSV', 'ai-flash-tune'); ?></a>
                    </div>
                </div>

                <div class="ai-flash-tune-card aiflt-world-card aiflt-pro-card">
                    <span class="aiflt-kicker"><?php echo esc_html__('Weekly summary + safety defaults', 'ai-flash-tune'); ?></span>
                    <h3><?php echo esc_html__('Automate summaries without losing control', 'ai-flash-tune'); ?></h3>
                    <label class="ai-flash-tune-checkbox-control"><input type="checkbox" name="weekly_email_enabled" value="1" <?php checked($settings['weekly_email_enabled'] ?? '0', '1'); ?> /> <span><?php echo esc_html__('Enable weekly summary email', 'ai-flash-tune'); ?></span></label>
                    <label class="ai-flash-tune-checkbox-control"><input type="checkbox" name="woocommerce_email_style" value="1" <?php checked($settings['woocommerce_email_style'] ?? '1', '1'); ?> /> <span><?php echo esc_html__('Use WooCommerce-style email formatting where available', 'ai-flash-tune'); ?></span></label>
                    <label class="ai-flash-tune-checkbox-control"><input type="checkbox" name="safe_apply_required" value="1" <?php checked($settings['safe_apply_required'] ?? '1', '1'); ?> /> <span><?php echo esc_html__('Require preview/confirmation before AI applies changes', 'ai-flash-tune'); ?></span></label>
                    <label class="ai-flash-tune-checkbox-control"><input type="checkbox" name="auto_capture_before_after" value="1" <?php checked($settings['auto_capture_before_after'] ?? '1', '1'); ?> /> <span><?php echo esc_html__('Capture before/after snapshots for changes', 'ai-flash-tune'); ?></span></label>
                    <label class="ai-flash-tune-form-label"><?php echo esc_html__('Send weekly summary to', 'ai-flash-tune'); ?></label>
                    <input class="ai-flash-tune-input" type="email" autocomplete="email" name="weekly_email_to" value="<?php echo esc_attr($settings['weekly_email_to'] ?? get_option('admin_email')); ?>" />
                    <div class="aiflt-inline-fields">
                        <div><label class="ai-flash-tune-form-label"><?php echo esc_html__('Day', 'ai-flash-tune'); ?></label><select class="ai-flash-tune-input" name="weekly_email_day"><?php foreach(['monday','tuesday','wednesday','thursday','friday','saturday','sunday'] as $d): ?><option value="<?php echo esc_attr($d); ?>" <?php selected($settings['weekly_email_day'] ?? 'monday', $d); ?>><?php echo esc_html(ucfirst($d)); ?></option><?php endforeach; ?></select></div>
                        <div><label class="ai-flash-tune-form-label"><?php echo esc_html__('Hour', 'ai-flash-tune'); ?></label><input class="ai-flash-tune-input" type="number" min="0" max="23" name="weekly_email_hour" value="<?php echo esc_attr($settings['weekly_email_hour'] ?? '9'); ?>" /></div>
                    </div>
                    <div class="aiflt-button-row">
                        <button type="submit" class="ai-flash-tune-btn ai-flash-tune-btn-blue aiflt-growth-save-button"><?php echo esc_html__('Save Growth Toolkit settings', 'ai-flash-tune'); ?></button>
                    </div>
                </div>
            </div>
        </form>

        <div class="aiflt-world-grid aiflt-world-grid-2 ai-flash-tune-mb-8">
            <div class="ai-flash-tune-card aiflt-world-card aiflt-pro-card">
                <span class="aiflt-kicker"><?php echo esc_html__('AI usage logs after real AI actions', 'ai-flash-tune'); ?></span>
                <h3><?php echo esc_html__('AI provider audit log', 'ai-flash-tune'); ?></h3>
                <p><?php echo esc_html__('Track provider, wrapper/action, status, duration, model, and errors after real OpenAI/Zywrap calls.', 'ai-flash-tune'); ?></p>
                <div class="aiflt-mini-stat-row">
                    <span><strong><?php echo esc_html(number_format_i18n($ai_stats['success'] ?? 0)); ?></strong><?php echo esc_html__('Success', 'ai-flash-tune'); ?></span>
                    <span><strong><?php echo esc_html(number_format_i18n($ai_stats['error'] ?? 0)); ?></strong><?php echo esc_html__('Errors', 'ai-flash-tune'); ?></span>
                    <span><strong><?php echo esc_html(number_format_i18n($ai_stats['local'] ?? 0)); ?></strong><?php echo esc_html__('Local/fallback', 'ai-flash-tune'); ?></span>
                </div>
                <div class="aiflt-table-wrap">
                    <table class="widefat striped">
                        <thead><tr><th><?php echo esc_html__('Time', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Provider', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Action', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Status', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Duration', 'ai-flash-tune'); ?></th></tr></thead>
                        <tbody>
                            <?php if(empty($ai_logs)): ?><tr><td colspan="5"><?php echo esc_html__('No AI usage has been logged yet. Run a real OpenAI/Zywrap action to populate this audit trail.', 'ai-flash-tune'); ?></td></tr>
                            <?php else: foreach($ai_logs as $log): ?><tr>
                                <td><?php echo esc_html($log['created_at'] ?? ''); ?></td>
                                <td><span class="aiflt-status-pill"><?php echo esc_html($log['provider'] ?? ''); ?></span></td>
                                <td><strong><?php echo esc_html($log['action_key'] ?? ''); ?></strong><br><span class="description"><?php echo esc_html($log['wrapper_code'] ?? ''); ?></span></td>
                                <td><?php echo esc_html($log['status'] ?? ''); ?></td>
                                <td><?php echo esc_html(number_format_i18n(intval($log['duration_ms'] ?? 0))); ?>ms</td>
                            </tr><?php endforeach; endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="ai-flash-tune-card aiflt-world-card aiflt-pro-card">
                <span class="aiflt-kicker"><?php echo esc_html__('A/B test idea generator', 'ai-flash-tune'); ?></span>
                <h3><?php echo esc_html__('Generate practical experiments from store signals', 'ai-flash-tune'); ?></h3>
                <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" class="aiflt-inline-form">
                    <?php wp_nonce_field('aiflt_generate_ab_test_ideas'); ?>
                    <input type="hidden" name="action" value="aiflt_generate_ab_test_ideas" />
                    <select class="ai-flash-tune-input" name="area"><option value="funnel"><?php echo esc_html__('Funnel', 'ai-flash-tune'); ?></option><option value="checkout"><?php echo esc_html__('Checkout', 'ai-flash-tune'); ?></option><option value="cart"><?php echo esc_html__('Cart recovery', 'ai-flash-tune'); ?></option></select>
                    <button class="ai-flash-tune-btn ai-flash-tune-btn-blue" type="submit"><?php echo esc_html__('Generate ideas', 'ai-flash-tune'); ?></button>
                </form>
                <div class="aiflt-table-wrap"><table class="widefat striped"><thead><tr><th><?php echo esc_html__('Idea', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Impact', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Effort', 'ai-flash-tune'); ?></th></tr></thead><tbody><?php if(empty($ideas)): ?><tr><td colspan="3"><?php echo esc_html__('No experiment ideas generated yet.', 'ai-flash-tune'); ?></td></tr><?php else: foreach($ideas as $idea): ?><tr><td><strong><?php echo esc_html($idea['title']); ?></strong><br><span class="description"><?php echo esc_html($idea['hypothesis']); ?></span></td><td><?php echo esc_html($idea['impact']); ?></td><td><?php echo esc_html($idea['effort']); ?></td></tr><?php endforeach; endif; ?></tbody></table></div>
            </div>
        </div>

        <div class="ai-flash-tune-card aiflt-world-card aiflt-pro-card ai-flash-tune-mb-8">
            <span class="aiflt-kicker"><?php echo esc_html__('Safe apply / rollback for checkout and product-copy changes', 'ai-flash-tune'); ?></span>
            <h3><?php echo esc_html__('Safe change log', 'ai-flash-tune'); ?></h3>
            <p><?php echo esc_html__('Every applied change should include before/after data, source, status, and a rollback path when the object type supports it.', 'ai-flash-tune'); ?></p>
            <div class="aiflt-table-wrap">
                <table class="widefat striped">
                    <thead><tr><th><?php echo esc_html__('Change', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Object', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Before / after', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Status', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Date', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Action', 'ai-flash-tune'); ?></th></tr></thead>
                    <tbody>
                        <?php if(empty($changes)): ?><tr><td colspan="6"><?php echo esc_html__('No tracked changes yet. Future AI apply actions will appear here.', 'ai-flash-tune'); ?></td></tr>
                        <?php else: foreach($changes as $change): 
                            $can_rollback = ($change['status'] ?? '') !== 'reverted' && in_array(($change['object_type'] ?? ''), ['product','option'], true);
                        ?>
                        <tr>
                            <td><strong><?php echo esc_html($change['label'] ?: $change['change_type']); ?></strong><br><span class="description"><?php echo esc_html($change['source'] ?? ''); ?></span></td>
                            <td><?php echo esc_html(trim(($change['object_type'] ?? '') . ' #' . ($change['object_id'] ?? ''))); ?></td>
                            <td><details><summary><?php echo esc_html__('View snapshot', 'ai-flash-tune'); ?></summary><pre><?php echo esc_html(wp_json_encode(['before'=>json_decode($change['before_data'] ?: '{}', true), 'after'=>json_decode($change['after_data'] ?: '{}', true)], JSON_PRETTY_PRINT)); ?></pre></details></td>
                            <td><span class="aiflt-status-pill"><?php echo esc_html($change['status']); ?></span></td>
                            <td><?php echo esc_html($change['created_at']); ?></td>
                            <td>
                                <?php if($can_rollback): ?>
                                    <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" onsubmit="return confirm('<?php echo esc_js(__('Rollback this change? Review the snapshot first.', 'ai-flash-tune')); ?>');">
                                        <?php wp_nonce_field('aiflt_rollback_change'); ?>
                                        <input type="hidden" name="action" value="aiflt_rollback_change" />
                                        <input type="hidden" name="change_id" value="<?php echo esc_attr($change['id']); ?>" />
                                        <button class="ai-flash-tune-btn ai-flash-tune-btn-gray" type="submit"><?php echo esc_html__('Rollback', 'ai-flash-tune'); ?></button>
                                    </form>
                                <?php else: ?>
                                    <span class="description"><?php echo esc_html__('No action', 'ai-flash-tune'); ?></span>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; endif; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="ai-flash-tune-card aiflt-world-card aiflt-pro-card">
            <span class="aiflt-kicker"><?php echo esc_html__('Product copy revision history', 'ai-flash-tune'); ?></span>
            <h3><?php echo esc_html__('Drafts, applied copy changes, compare, and restore', 'ai-flash-tune'); ?></h3>
            <p><?php echo esc_html__('Review product-copy changes before applying them. Restore the previous copy when a revision underperforms or was applied by mistake.', 'ai-flash-tune'); ?></p>
            <div class="aiflt-table-wrap">
                <table class="widefat striped">
                    <thead><tr><th><?php echo esc_html__('Product', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Revision', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Before / after', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Status', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Date', 'ai-flash-tune'); ?></th><th><?php echo esc_html__('Action', 'ai-flash-tune'); ?></th></tr></thead>
                    <tbody>
                        <?php if(empty($revisions)): ?><tr><td colspan="6"><?php echo esc_html__('No product copy revisions yet.', 'ai-flash-tune'); ?></td></tr>
                        <?php else: foreach($revisions as $rev): ?>
                        <tr>
                            <td><strong><?php echo esc_html($rev['current_title'] ?: ('#'.$rev['product_id'])); ?></strong><br><span class="description">#<?php echo esc_html($rev['product_id']); ?></span></td>
                            <td><?php echo esc_html($rev['revision_label']); ?><br><span class="description"><?php echo esc_html($rev['source']); ?></span></td>
                            <td><details><summary><?php echo esc_html__('Compare copy', 'ai-flash-tune'); ?></summary><div class="aiflt-copy-compare"><div><strong><?php echo esc_html__('Before', 'ai-flash-tune'); ?></strong><p><?php echo esc_html(wp_trim_words(($rev['before_title'] ?? '') . ' ' . wp_strip_all_tags($rev['before_short_description'] ?? ''), 42)); ?></p></div><div><strong><?php echo esc_html__('After', 'ai-flash-tune'); ?></strong><p><?php echo esc_html(wp_trim_words(($rev['after_title'] ?? '') . ' ' . wp_strip_all_tags($rev['after_short_description'] ?? ''), 42)); ?></p></div></div></details></td>
                            <td><span class="aiflt-status-pill"><?php echo esc_html($rev['status']); ?></span></td>
                            <td><?php echo esc_html($rev['created_at']); ?></td>
                            <td>
                                <?php if(($rev['status'] ?? '') !== 'restored'): ?>
                                    <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" onsubmit="return confirm('<?php echo esc_js(__('Restore the previous product copy from this revision?', 'ai-flash-tune')); ?>');">
                                        <?php wp_nonce_field('aiflt_restore_product_revision'); ?>
                                        <input type="hidden" name="action" value="aiflt_restore_product_revision" />
                                        <input type="hidden" name="revision_id" value="<?php echo esc_attr($rev['id']); ?>" />
                                        <button class="ai-flash-tune-btn ai-flash-tune-btn-gray" type="submit"><?php echo esc_html__('Restore previous copy', 'ai-flash-tune'); ?></button>
                                    </form>
                                <?php else: ?>
                                    <span class="description"><?php echo esc_html__('Already restored', 'ai-flash-tune'); ?></span>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; endif; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" class="ai-flash-tune-mt-8">
            <?php wp_nonce_field('aiflt_send_test_weekly_summary'); ?>
            <input type="hidden" name="action" value="aiflt_send_test_weekly_summary" />
            <button type="submit" class="ai-flash-tune-btn ai-flash-tune-btn-gray"><?php echo esc_html__('Send test weekly summary email', 'ai-flash-tune'); ?></button>
        </form>
    </div>
</div>
<style>
.aiflt-growth-toolkit-pro .aiflt-growth-command-center{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:16px}.aiflt-command-card{background:#fff;border:1px solid #e2e8f0;border-radius:22px;padding:20px;box-shadow:0 14px 34px rgba(15,23,42,.07)}.aiflt-command-card span{display:block;font-size:11px;text-transform:uppercase;letter-spacing:.1em;color:#64748b;font-weight:900}.aiflt-command-card strong{display:block;font-size:26px;color:#0f172a;margin:8px 0}.aiflt-command-card p{margin:0;color:#64748b}.aiflt-pro-card{border-radius:24px!important;border:1px solid #e2e8f0!important;box-shadow:0 18px 44px rgba(15,23,42,.08)!important}.aiflt-mini-stat-row{display:grid;grid-template-columns:repeat(3,1fr);gap:10px;margin:14px 0}.aiflt-mini-stat-row span{padding:13px;border-radius:16px;background:#f8fafc;border:1px solid #e2e8f0;color:#64748b;font-size:12px}.aiflt-mini-stat-row strong{display:block;color:#0f172a;font-size:22px}.aiflt-status-pill{display:inline-flex;padding:5px 10px;border-radius:999px;background:#eef2ff;color:#3730a3;font-size:12px;font-weight:800}details summary{cursor:pointer;color:#2563eb;font-weight:800}details pre{max-height:260px;overflow:auto;background:#0f172a;color:#e2e8f0;padding:14px;border-radius:12px;white-space:pre-wrap}.aiflt-copy-compare{display:grid;grid-template-columns:1fr 1fr;gap:12px;margin-top:10px}.aiflt-copy-compare>div{background:#f8fafc;border:1px solid #e2e8f0;border-radius:14px;padding:12px}.aiflt-table-wrap table td{vertical-align:top}.aiflt-button-row{display:flex;flex-wrap:wrap;gap:10px;margin-top:14px}@media(max-width:1100px){.aiflt-growth-toolkit-pro .aiflt-growth-command-center{grid-template-columns:1fr 1fr}.aiflt-copy-compare{grid-template-columns:1fr}}@media(max-width:760px){.aiflt-growth-toolkit-pro .aiflt-growth-command-center,.aiflt-mini-stat-row{grid-template-columns:1fr}}
</style>
