<?php
/**
 * GT BOGO Engine — Settings (Enterprise)
 * @package GT_BOGO_Engine
 * @since   3.11.0
 */
if (!defined('ABSPATH')) exit;
if (!current_user_can('manage_options')) return;

$is_pro  = function_exists('gt_bogo_has_pro') ? gt_bogo_has_pro() : false;
$message = ''; $message_type = '';
$version = defined('GT_BOGO_VERSION') ? GT_BOGO_VERSION : '3.11.0';

// ── Save Settings ─────────────────────────────────────────────────────────
if (isset($_POST['gtbogo_save_settings']) && check_admin_referer('gtbogo_save_settings')) {

    // Cart & Checkout
    update_option('gtbogo_auto_add',          isset($_POST['auto_add']));
    update_option('gtbogo_auto_remove',        isset($_POST['auto_remove']));
    update_option('gtbogo_show_discount_label',isset($_POST['show_discount_label']));
    update_option('gtbogo_discount_label_text',sanitize_text_field($_POST['discount_label_text'] ?? 'Complimentary'));
    update_option('gtbogo_cart_notice',        isset($_POST['cart_notice']));
    update_option('gtbogo_cart_notice_pos',    sanitize_text_field($_POST['cart_notice_pos'] ?? 'before_cart'));
    update_option('gtbogo_conflict_strategy',  sanitize_text_field($_POST['conflict_strategy'] ?? 'highest_priority'));
    update_option('gtbogo_max_discount_pct',   intval($_POST['max_discount_pct'] ?? 0));
    update_option('gtbogo_exclude_roles',      array_map('sanitize_text_field', (array)($_POST['exclude_roles'] ?? [])));

    // Display
    update_option('gtbogo_badge_position',     sanitize_text_field($_POST['badge_position']  ?? 'top_left'));
    update_option('gtbogo_badge_inherit_theme',isset($_POST['badge_inherit_theme']));
    update_option('gtbogo_progress_bar',       isset($_POST['progress_bar']));
    update_option('gtbogo_progress_bar_pos',   sanitize_text_field($_POST['progress_bar_pos'] ?? 'before_cart'));

    // Performance
    update_option('gtbogo_cache_rules',        isset($_POST['cache_rules']));
    update_option('gtbogo_cache_ttl',          intval($_POST['cache_ttl'] ?? 300));
    update_option('gtbogo_ajax_cart',          isset($_POST['ajax_cart']));

    // Analytics
    update_option('gtbogo_analytics_enabled',  isset($_POST['analytics_enabled']));
    update_option('gtbogo_analytics_retention_days', intval($_POST['analytics_retention'] ?? 90));
    update_option('gtbogo_analytics_exclude_roles', array_map('sanitize_text_field', (array)($_POST['analytics_exclude_roles'] ?? [])));

    // Audit Log
    update_option('gtbogo_log_enabled',        isset($_POST['log_enabled']));
    update_option('gtbogo_log_max_entries',    intval($_POST['log_max_entries'] ?? 1000));
    update_option('gtbogo_log_min_level',      sanitize_text_field($_POST['log_min_level'] ?? 'info'));
    update_option('gtbogo_log_alert_email',    sanitize_email($_POST['log_alert_email'] ?? ''));
    update_option('gtbogo_log_alert_on_error', isset($_POST['log_alert_on_error']));

    // Customer Emails
    update_option('gtbogo_customer_emails_enabled', isset($_POST['customer_emails_enabled']));
    update_option('gtbogo_email_accent_color',      sanitize_hex_color($_POST['email_accent_color'] ?? '#E10000'));

    // Advanced
    update_option('gtbogo_uninstall_data',     isset($_POST['uninstall_data']));
    update_option('gtbogo_debug_mode',         isset($_POST['debug_mode']));

    if (function_exists('gt_bogo_log')) gt_bogo_log('Settings updated', 'Admin settings saved', 'success');
    delete_transient('gtbogo_active_rules');
    $message = 'Settings saved.'; $message_type = 'success';
}

// Fetch current
$s = [
    'auto_add'            => get_option('gtbogo_auto_add', true),
    'auto_remove'         => get_option('gtbogo_auto_remove', true),
    'show_discount_label' => get_option('gtbogo_show_discount_label', true),
    'discount_label_text' => get_option('gtbogo_discount_label_text', 'Complimentary'),
    'cart_notice'         => get_option('gtbogo_cart_notice', true),
    'cart_notice_pos'     => get_option('gtbogo_cart_notice_pos', 'before_cart'),
    'conflict_strategy'   => get_option('gtbogo_conflict_strategy', 'highest_priority'),
    'max_discount_pct'    => get_option('gtbogo_max_discount_pct', 0),
    'exclude_roles'       => (array)get_option('gtbogo_exclude_roles', []),
    'badge_position'      => get_option('gtbogo_badge_position', 'top_left'),
    'badge_inherit_theme' => get_option('gtbogo_badge_inherit_theme', true),
    'progress_bar'        => get_option('gtbogo_progress_bar', true),
    'progress_bar_pos'    => get_option('gtbogo_progress_bar_pos', 'before_cart'),
    'cache_rules'         => get_option('gtbogo_cache_rules', true),
    'cache_ttl'           => get_option('gtbogo_cache_ttl', 300),
    'ajax_cart'           => get_option('gtbogo_ajax_cart', true),
    'analytics_enabled'   => get_option('gtbogo_analytics_enabled', true),
    'analytics_retention' => get_option('gtbogo_analytics_retention_days', 90),
    'analytics_excl'      => (array)get_option('gtbogo_analytics_exclude_roles', []),
    'log_enabled'         => get_option('gtbogo_log_enabled', true),
    'log_max_entries'     => get_option('gtbogo_log_max_entries', 1000),
    'log_min_level'       => get_option('gtbogo_log_min_level', 'info'),
    'log_alert_email'     => get_option('gtbogo_log_alert_email', get_option('admin_email')),
    'log_alert_on_error'  => get_option('gtbogo_log_alert_on_error', false),
    'customer_emails_enabled' => get_option('gtbogo_customer_emails_enabled', false),
    'email_accent_color'      => get_option('gtbogo_email_accent_color', '#E10000'),
    'uninstall_data'      => get_option('gtbogo_uninstall_data', false),
    'debug_mode'          => get_option('gtbogo_debug_mode', false),
];

$wp_roles = wp_roles()->get_names();

require_once __DIR__ . '/../ui-system/wrapper-start.php';
?>
<div class="gt-page-header gt-animate-in">
    <div>
        <h1 class="gt-page-title">⚙️ Settings</h1>
        <p class="gt-page-subtitle">Global plugin configuration — cart behaviour, display, performance, analytics, and advanced options</p>
    </div>
    <span class="gt-badge gt-badge-free">v<?php echo esc_html($version); ?></span>
</div>
<?php if ($message): ?><div class="gt-notice gt-notice-<?php echo esc_attr($message_type); ?> gt-animate-in">✓ <?php echo esc_html($message); ?></div><?php endif; ?>

<form method="post" action="">
<?php wp_nonce_field('gtbogo_save_settings'); ?>

<!-- CART & CHECKOUT -->
<div class="gt-card gt-animate-in" style="margin-bottom:20px">
    <div class="gt-card-header"><h2 class="gt-card-title">🛒 Cart &amp; Checkout Behaviour</h2></div>

    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Auto-Add Free Item</div><div class="gt-setting-desc">When a customer qualifies for a BOGO deal, automatically add the complimentary item to their cart. If disabled, customers must add the item manually.</div></div>
        <label class="gt-toggle"><input type="checkbox" name="auto_add" value="1" <?php checked($s['auto_add']); ?>><span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span></label>
    </div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Auto-Remove When Unqualified</div><div class="gt-setting-desc">If the customer removes items and no longer qualifies, automatically remove the auto-added complimentary item from the cart.</div></div>
        <label class="gt-toggle"><input type="checkbox" name="auto_remove" value="1" <?php checked($s['auto_remove']); ?>><span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span></label>
    </div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Show Discount Line Label</div><div class="gt-setting-desc">Display a label next to discounted items in the cart (e.g. "Complimentary"). Inherits your theme's cart line styling.</div></div>
        <label class="gt-toggle"><input type="checkbox" name="show_discount_label" value="1" <?php checked($s['show_discount_label']); ?>><span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span></label>
    </div>
    <div class="gt-form-group" style="padding:0 20px 16px;margin:0">
        <label class="gt-form-label">Discount Label Text</label>
        <input type="text" name="discount_label_text" class="gt-input" style="max-width:260px" value="<?php echo esc_attr($s['discount_label_text']); ?>" placeholder="Complimentary" maxlength="32">
        <div class="gt-form-hint">Short label shown next to the discounted cart line. No exclamation marks.</div>
    </div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Show Cart Progress Notice</div><div class="gt-setting-desc">Display a message in the cart when the customer is close to qualifying for a deal. Encourages them to add another item.</div></div>
        <label class="gt-toggle"><input type="checkbox" name="cart_notice" value="1" <?php checked($s['cart_notice']); ?>><span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span></label>
    </div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Cart Notice Position</div></div>
        <select name="cart_notice_pos" class="gt-select" style="width:220px">
            <option value="before_cart"    <?php selected($s['cart_notice_pos'],'before_cart'); ?>>Before cart table</option>
            <option value="after_cart"     <?php selected($s['cart_notice_pos'],'after_cart'); ?>>After cart table</option>
            <option value="cart_totals"    <?php selected($s['cart_notice_pos'],'cart_totals'); ?>>Above cart totals</option>
        </select>
    </div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Rule Conflict Strategy</div><div class="gt-setting-desc">When multiple PRO rules are eligible for the same cart, this determines which rule fires.</div></div>
        <select name="conflict_strategy" class="gt-select" style="width:220px">
            <option value="highest_priority" <?php selected($s['conflict_strategy'],'highest_priority'); ?>>Highest priority rule wins</option>
            <option value="best_for_customer"<?php selected($s['conflict_strategy'],'best_for_customer'); ?>>Best deal for customer</option>
            <option value="all_eligible"     <?php selected($s['conflict_strategy'],'all_eligible'); ?>>All eligible rules stack</option>
            <option value="first_match"      <?php selected($s['conflict_strategy'],'first_match'); ?>>First match only</option>
        </select>
    </div>
    <div class="gt-setting-row" style="border:none">
        <div><div class="gt-setting-label">Maximum Discount Per Order (%)</div><div class="gt-setting-desc">Cap the total BOGO discount as a percentage of the cart subtotal. 0 = no cap. Use to protect against stacking that pushes orders into negative margin.</div></div>
        <input type="number" name="max_discount_pct" class="gt-input" style="width:100px" value="<?php echo esc_attr($s['max_discount_pct']); ?>" min="0" max="100">
    </div>

    <div style="padding:16px 20px 0;border-top:1px solid var(--gt-border)">
        <div class="gt-form-label" style="margin-bottom:8px">Exclude User Roles from All BOGO Rules</div>
        <div style="display:flex;flex-wrap:wrap;gap:8px">
            <?php foreach ($wp_roles as $slug => $name):
                $sel = in_array($slug, $s['exclude_roles']);
            ?>
            <label style="display:flex;align-items:center;gap:6px;cursor:pointer;padding:6px 12px;border-radius:8px;border:1px solid <?php echo $sel?'var(--gt-red)':'var(--gt-border)'; ?>;background:<?php echo $sel?'rgba(225,0,0,0.08)':'rgba(255,255,255,0.02)'; ?>;font-size:12px;color:var(--gt-soft)">
                <input type="checkbox" name="exclude_roles[]" value="<?php echo esc_attr($slug); ?>" <?php checked($sel); ?> style="accent-color:var(--gt-red)">
                <?php echo esc_html($name); ?>
            </label>
            <?php endforeach; ?>
        </div>
        <div class="gt-form-hint" style="margin-top:8px">Checked roles are never shown BOGO deals. Use for wholesale, admin, or staff accounts.</div>
    </div>
</div>

<!-- DISPLAY -->
<div class="gt-card gt-animate-in" style="margin-bottom:20px">
    <div class="gt-card-header"><h2 class="gt-card-title">🎨 Display Settings</h2></div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Show Product Badge</div><div class="gt-setting-desc">Display a badge on eligible product images. The badge inherits your active theme's styling — no hardcoded colours or fonts.</div></div>
        <label class="gt-toggle"><input type="checkbox" name="show_badge" value="1" <?php checked(!empty(get_option('gtbogo_show_badge', false))); ?>><span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span></label>
    </div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Badge Position</div></div>
        <select name="badge_position" class="gt-select" style="width:220px">
            <option value="top_left"     <?php selected($s['badge_position'],'top_left'); ?>>Top left</option>
            <option value="top_right"    <?php selected($s['badge_position'],'top_right'); ?>>Top right</option>
            <option value="bottom_left"  <?php selected($s['badge_position'],'bottom_left'); ?>>Bottom left</option>
            <option value="bottom_right" <?php selected($s['badge_position'],'bottom_right'); ?>>Bottom right</option>
        </select>
    </div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Badge Inherits Theme Styling</div><div class="gt-setting-desc">When enabled, the badge uses your theme's colours and font. When disabled, it uses the plugin default (navy + white). Luxury brand safe — recommended on.</div></div>
        <label class="gt-toggle"><input type="checkbox" name="badge_inherit_theme" value="1" <?php checked($s['badge_inherit_theme']); ?>><span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span></label>
    </div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Show Cart Progress Bar</div><div class="gt-setting-desc">Display a progress bar in the cart showing how close the customer is to qualifying. Uses the Cart Progress Bar superpower when available.</div></div>
        <label class="gt-toggle"><input type="checkbox" name="progress_bar" value="1" <?php checked($s['progress_bar']); ?>><span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span></label>
    </div>
    <div class="gt-setting-row" style="border:none">
        <div><div class="gt-setting-label">Progress Bar Position</div></div>
        <select name="progress_bar_pos" class="gt-select" style="width:220px">
            <option value="before_cart"  <?php selected($s['progress_bar_pos'],'before_cart'); ?>>Before cart table</option>
            <option value="after_cart"   <?php selected($s['progress_bar_pos'],'after_cart'); ?>>After cart table</option>
            <option value="cart_totals"  <?php selected($s['progress_bar_pos'],'cart_totals'); ?>>Above cart totals</option>
        </select>
    </div>
</div>

<!-- PERFORMANCE -->
<div class="gt-card gt-animate-in" style="margin-bottom:20px">
    <div class="gt-card-header"><h2 class="gt-card-title">⚡ Performance</h2></div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Cache Active Rules</div><div class="gt-setting-desc">Cache the active rule set using WordPress transients. Dramatically reduces database queries on high-traffic stores. The cache is automatically cleared when rules are saved.</div></div>
        <label class="gt-toggle"><input type="checkbox" name="cache_rules" value="1" <?php checked($s['cache_rules']); ?>><span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span></label>
    </div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Cache TTL (seconds)</div><div class="gt-setting-desc">How long to cache the active rule set before refreshing from the database. 300 seconds (5 minutes) is recommended.</div></div>
        <input type="number" name="cache_ttl" class="gt-input" style="width:120px" value="<?php echo esc_attr($s['cache_ttl']); ?>" min="30" max="3600">
    </div>
    <div class="gt-setting-row" style="border:none">
        <div><div class="gt-setting-label">AJAX Cart Compatibility</div><div class="gt-setting-desc">Enable compatibility with AJAX-based cart plugins (e.g. CartFlows, SureCart). Triggers rule evaluation on AJAX cart update events. Disable if you experience double-discount issues with custom cart plugins.</div></div>
        <label class="gt-toggle"><input type="checkbox" name="ajax_cart" value="1" <?php checked($s['ajax_cart']); ?>><span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span></label>
    </div>
</div>

<!-- ANALYTICS -->
<div class="gt-card gt-animate-in" style="margin-bottom:20px">
    <div class="gt-card-header"><h2 class="gt-card-title">📊 Analytics</h2></div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Enable Analytics Tracking</div><div class="gt-setting-desc">Record order-level BOGO data (rule triggered, order total, discount amount) for the Analytics dashboard. Data is stored locally — no external services.</div></div>
        <label class="gt-toggle"><input type="checkbox" name="analytics_enabled" value="1" <?php checked($s['analytics_enabled']); ?>><span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span></label>
    </div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Data Retention (days)</div><div class="gt-setting-desc">Analytics records older than this are automatically purged. 90 days is recommended. Set 0 to retain indefinitely (not recommended on high-volume stores).</div></div>
        <input type="number" name="analytics_retention" class="gt-input" style="width:120px" value="<?php echo esc_attr($s['analytics_retention']); ?>" min="0" max="730">
    </div>
    <div style="padding:16px 20px 0;border-top:1px solid var(--gt-border)">
        <div class="gt-form-label" style="margin-bottom:8px">Exclude Roles from Analytics</div>
        <div style="display:flex;flex-wrap:wrap;gap:8px">
            <?php foreach ($wp_roles as $slug => $name):
                $sel = in_array($slug, $s['analytics_excl']);
            ?>
            <label style="display:flex;align-items:center;gap:6px;cursor:pointer;padding:6px 12px;border-radius:8px;border:1px solid <?php echo $sel?'var(--gt-red)':'var(--gt-border)'; ?>;background:<?php echo $sel?'rgba(225,0,0,0.08)':'rgba(255,255,255,0.02)'; ?>;font-size:12px;color:var(--gt-soft)">
                <input type="checkbox" name="analytics_exclude_roles[]" value="<?php echo esc_attr($slug); ?>" <?php checked($sel); ?> style="accent-color:var(--gt-red)">
                <?php echo esc_html($name); ?>
            </label>
            <?php endforeach; ?>
        </div>
        <div class="gt-form-hint" style="margin-top:8px;margin-bottom:0">Orders from excluded roles are not recorded in analytics. Exclude administrator to keep internal test orders out of your data.</div>
    </div>
</div>

<!-- AUDIT LOG -->
<div class="gt-card gt-animate-in" style="margin-bottom:20px">
    <div class="gt-card-header"><h2 class="gt-card-title">📋 Audit Log</h2></div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Enable Audit Logging</div><div class="gt-setting-desc">Log all plugin events — rule saves, activations, deactivations, imports, and errors. Essential for compliance and troubleshooting.</div></div>
        <label class="gt-toggle"><input type="checkbox" name="log_enabled" value="1" <?php checked($s['log_enabled']); ?>><span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span></label>
    </div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Maximum Log Entries</div><div class="gt-setting-desc">Auto-purge oldest entries when this limit is reached. Recommended: 1000 for most stores.</div></div>
        <input type="number" name="log_max_entries" class="gt-input" style="width:120px" value="<?php echo esc_attr($s['log_max_entries']); ?>" min="100" max="10000">
    </div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Minimum Log Level</div><div class="gt-setting-desc">Only events at or above this level are recorded. "Info" captures everything; "Error" captures only failures.</div></div>
        <select name="log_min_level" class="gt-select" style="width:160px">
            <option value="info"  <?php selected($s['log_min_level'],'info'); ?>>Info (all events)</option>
            <option value="warn"  <?php selected($s['log_min_level'],'warn'); ?>>Warn + Error only</option>
            <option value="error" <?php selected($s['log_min_level'],'error'); ?>>Error only</option>
        </select>
    </div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Email Alert on Error</div><div class="gt-setting-desc">Send an email when an error-level event is logged. Useful for catching license failures, import errors, or rule execution exceptions immediately.</div></div>
        <label class="gt-toggle"><input type="checkbox" name="log_alert_on_error" value="1" <?php checked($s['log_alert_on_error']); ?>><span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span></label>
    </div>
    <div class="gt-setting-row" style="border:none">
        <div><div class="gt-setting-label">Alert Email Address</div></div>
        <input type="email" name="log_alert_email" class="gt-input" style="width:280px" value="<?php echo esc_attr($s['log_alert_email']); ?>">
    </div>
</div>

<!-- ADVANCED -->
<div class="gt-card gt-animate-in" style="margin-bottom:20px;border-color:rgba(245,158,11,0.2)">
    <div class="gt-card-header"><h2 class="gt-card-title">🔧 Advanced</h2></div>
    <div class="gt-setting-row">
        <div><div class="gt-setting-label">Debug Mode</div><div class="gt-setting-desc">Write verbose debug output to the WordPress debug log (wp-content/debug.log). Only enable temporarily when diagnosing an issue. Never leave enabled in production.</div></div>
        <label class="gt-toggle"><input type="checkbox" name="debug_mode" value="1" <?php checked($s['debug_mode']); ?>><span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span></label>
    </div>
    <div class="gt-setting-row" style="border:none">
        <div>
            <div class="gt-setting-label" style="color:#fca5a5">Delete All Data on Uninstall</div>
            <div class="gt-setting-desc">When enabled, uninstalling the plugin will permanently delete all rules, analytics data, audit logs, and plugin settings from the database. This cannot be undone. Leave disabled unless you intend a complete clean removal.</div>
        </div>
        <label class="gt-toggle"><input type="checkbox" name="uninstall_data" value="1" <?php checked($s['uninstall_data']); ?>><span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span></label>
    </div>
</div>

<div style="text-align:right;margin-bottom:40px">
    <!-- Customer Emails -->
    <div class="gt-card gt-animate-in" style="margin-bottom:20px">
        <div class="gt-card-header"><h2 class="gt-card-title">✉️ Customer Emails</h2><span class="gt-text-soft gt-text-sm">White-labelled transactional emails sent to your customers — uses your store name and logo</span></div>
        <div style="display:flex;flex-direction:column">
            <div class="gt-setting-row">
                <div>
                    <div class="gt-setting-label">Enable Customer Emails</div>
                    <div class="gt-setting-desc">Sends branded transactional emails to customers for BOGO confirmations, tier upgrades, streak rewards, referrals, free gifts, deal expiry, cart nudges and winback campaigns. Uses your store name and logo — GT BOGO Engine is never visible to your customers.</div>
                </div>
                <label class="gt-toggle">
                    <input type="checkbox" name="customer_emails_enabled" value="1" <?php checked(!empty($s['customer_emails_enabled'])); ?>>
                    <span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span>
                </label>
            </div>
            <div class="gt-setting-row" style="border:none">
                <div>
                    <div class="gt-setting-label">Email Accent Color</div>
                    <div class="gt-setting-desc">Used for buttons, borders and highlights in customer emails. Defaults to your brand color. Leave as #E10000 if unsure.</div>
                </div>
                <div style="display:flex;align-items:center;gap:10px">
                    <input type="color" name="email_accent_color" value="<?php echo esc_attr($s['email_accent_color']); ?>" style="width:48px;height:36px;border:1px solid var(--gt-border);border-radius:6px;background:transparent;cursor:pointer;padding:2px">
                    <input type="text" name="email_accent_color" value="<?php echo esc_attr($s['email_accent_color']); ?>" class="gt-input" style="max-width:110px;font-family:monospace" maxlength="7" oninput="this.previousElementSibling.value=this.value">
                </div>
            </div>
        </div>
        <div style="margin:0 0 0;padding:16px 20px;background:rgba(34,197,94,0.05);border-top:1px solid var(--gt-border);border-radius:0 0 12px 12px">
            <p style="margin:0;font-size:12px;color:var(--gt-subtle)">📬 Emails are sent via your WordPress mail configuration (wp_mail). For reliable delivery configure SMTP in your WordPress settings or use a mail plugin like WP Mail SMTP. Extended delivery via Mailchimp or Klaviyo is available in the <a href="<?php echo esc_url(admin_url('admin.php?page=gt-bogo-engine&tab=integrations')); ?>" style="color:var(--gt-red)">Integrations page</a>.</p>
        </div>
    </div>

    <button type="submit" name="gtbogo_save_settings" class="gt-btn gt-btn-primary" style="padding:14px 40px">💾 Save Settings</button>
</div>
</form>

<!-- GUIDANCE -->
<div class="gt-card gt-animate-in" style="background:linear-gradient(135deg,rgba(59,130,246,0.06),rgba(7,12,28,0));border-color:rgba(59,130,246,0.15)">
    <div class="gt-card-header"><h2 class="gt-card-title">🧠 Settings Recommendations</h2></div>
    <div class="gt-feature-grid gt-feature-grid--2col">
        <div><div style="font-size:13px;font-weight:700;color:var(--gt-white);margin-bottom:6px">Recommended Production Setup</div><div style="font-size:13px;color:var(--gt-soft);line-height:1.7">Auto-add ON · Auto-remove ON · Cache rules ON (300s TTL) · Analytics ON (90-day retention) · Audit log ON (Info level, 1000 entries) · Debug mode OFF · Delete on uninstall OFF. This is the safest and most performant configuration for a live store.</div></div>
        <div><div style="font-size:13px;font-weight:700;color:var(--gt-white);margin-bottom:6px">Conflict Strategy Selection</div><div style="font-size:13px;color:var(--gt-soft);line-height:1.7">"Highest priority" is the safest default — you control outcomes via priority numbers. "Best for customer" maximises order conversion but reduces margin control. "All eligible stack" is powerful for promotions but requires a maximum discount cap to prevent runaway discounting.</div></div>
        <div><div style="font-size:13px;font-weight:700;color:var(--gt-white);margin-bottom:6px">Always Exclude Admin from Analytics</div><div style="font-size:13px;color:var(--gt-soft);line-height:1.7">Test orders placed by administrators contaminate your analytics data. Add the administrator role to the analytics exclusion list from day one. This keeps your AOV, order count, and revenue figures clean and representative of actual customer behaviour.</div></div>
        <div><div style="font-size:13px;font-weight:700;color:var(--gt-white);margin-bottom:6px">Maximum Discount Cap for Stacking</div><div style="font-size:13px;color:var(--gt-soft);line-height:1.7">If using "All eligible stack" conflict strategy, set a maximum discount cap of 50–60% to protect margin. Without this cap, a customer with multiple qualifying rules could receive a discount that exceeds your actual margin on the order, resulting in a net loss.</div></div>
    </div>
</div>

<?php require_once __DIR__ . '/../ui-system/wrapper-end.php'; ?>
