<?php
/**
 * GT BOGO Engine — Deal Lockout
 * @package GT_BOGO_Engine
 * @since   3.10.1
 */
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 = '';

if ($is_pro && isset($_POST['gtbogo_save_lockout']) && check_admin_referer('gtbogo_lockout', 'gtbogo_lo_nonce')) {
    $lo = [
        'prevent_stack_on_sale'    => isset($_POST['lo_prevent_sale']),
        'max_discount_pct'         => intval($_POST['lo_max_discount']    ?? 0),
        'min_margin_pct'           => intval($_POST['lo_min_margin']      ?? 0),
        'prevent_coupon_stack'     => isset($_POST['lo_prevent_coupon']),
        'single_rule_per_product'  => isset($_POST['lo_single_per_prod']),
        'max_rules_per_cart'       => intval($_POST['lo_max_cart_rules']  ?? 0),
        'exclude_categories'       => array_map('intval', (array)($_POST['lo_exclude_cats'] ?? [])),
    ];
    update_option('gtbogo_deal_lockout', $lo);
    $message = 'Lockout settings saved.';
}

$lo = get_option('gtbogo_deal_lockout', [
    'prevent_stack_on_sale'  => false, 'max_discount_pct'      => 0,
    'min_margin_pct'         => 0,     'prevent_coupon_stack'  => false,
    'single_rule_per_product'=> false, 'max_rules_per_cart'    => 0,
    'exclude_categories'     => [],
]);

$all_cats = get_terms(['taxonomy' => 'product_cat', 'hide_empty' => false]);

require_once __DIR__ . '/../ui-system/wrapper-start.php';
?>
<div class="gt-page-header gt-animate-in">
    <div>
        <h1 class="gt-page-title">🔒 Deal Lockout</h1>
        <p class="gt-page-subtitle">Protect your margins — prevent simultaneous promotions and runaway discounts</p>
    </div>
</div>
<?php if ($message): ?><div class="gt-notice gt-notice-success gt-animate-in">✓ <?php echo esc_html($message); ?></div><?php endif; ?>

<?php if (!$is_pro): ?>
<div class="gt-pro-gate gt-animate-in">
    <span class="gt-pro-gate-icon">🔒</span>
    <h2>PRO License Required</h2>
    <p>Deal Lockout prevents two promotions running simultaneously on the same product — critical for luxury and fashion brands protecting margins.</p>
    <div class="gt-flex" style="justify-content:center;gap:12px">
        <a href="https://graphictshirts.shop/bogo/" target="_blank" rel="noopener" class="gt-btn gt-btn-primary">Purchase PRO</a>
    </div>
</div>
<?php else: ?>
<form method="post" action="">
<?php wp_nonce_field('gtbogo_lockout', 'gtbogo_lo_nonce'); ?>

<div class="gt-card gt-animate-in" style="margin-bottom:20px">
    <div class="gt-card-header"><h2 class="gt-card-title">🛡️ Stacking Protection</h2></div>
    <?php foreach([
        ['lo_prevent_sale',  'prevent_stack_on_sale',  'Block BOGO on Sale Items',         'BOGO rules will not apply to products already marked as on-sale in WooCommerce'],
        ['lo_prevent_coupon','prevent_coupon_stack',   'Block BOGO + Coupon Stacking',     'Prevent customers from combining BOGO rules with WooCommerce coupon codes'],
        ['lo_single_per_prod','single_rule_per_product','One Rule Per Product Maximum',     'Only the highest-priority matching rule fires per product — no stacking on the same item'],
    ] as $row): ?>
    <div class="gt-setting-row">
        <div>
            <div class="gt-setting-label"><?php echo esc_html($row[2]); ?></div>
            <div class="gt-setting-desc"><?php echo esc_html($row[3]); ?></div>
        </div>
        <label class="gt-toggle">
            <input type="checkbox" name="<?php echo $row[0]; ?>" value="1" <?php checked(!empty($lo[$row[1]])); ?>>
            <span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span>
        </label>
    </div>
    <?php endforeach; ?>
</div>

<div class="gt-card gt-animate-in" style="margin-bottom:20px">
    <div class="gt-card-header"><h2 class="gt-card-title">📊 Discount Caps</h2><span class="gt-text-soft gt-text-sm">Set to 0 to disable</span></div>
    <div class="gt-feature-grid gt-feature-grid--2col">
        <div class="gt-form-group" style="margin:0">
            <label class="gt-form-label">Maximum Total Discount (%)</label>
            <input type="number" name="lo_max_discount" class="gt-input" min="0" max="100" value="<?php echo esc_attr($lo['max_discount_pct']??0); ?>">
            <div class="gt-form-hint">Cart discount will not exceed this percentage of cart total (0 = no cap)</div>
        </div>
        <div class="gt-form-group" style="margin:0">
            <label class="gt-form-label">Minimum Margin Protection (%)</label>
            <input type="number" name="lo_min_margin" class="gt-input" min="0" max="100" value="<?php echo esc_attr($lo['min_margin_pct']??0); ?>">
            <div class="gt-form-hint">Rule will not fire if resulting margin drops below this % (requires cost price set on product)</div>
        </div>
        <div class="gt-form-group" style="margin:0">
            <label class="gt-form-label">Maximum Rules Per Cart</label>
            <input type="number" name="lo_max_cart_rules" class="gt-input" min="0" max="15" value="<?php echo esc_attr($lo['max_rules_per_cart']??0); ?>">
            <div class="gt-form-hint">Maximum number of BOGO rules that can fire on a single cart (0 = no limit)</div>
        </div>
    </div>
</div>

<div class="gt-card gt-animate-in" style="margin-bottom:20px">
    <div class="gt-card-header"><h2 class="gt-card-title">🚫 Excluded Categories</h2><span class="gt-text-soft gt-text-sm">BOGO rules never apply to these</span></div>
    <div style="max-height:200px;overflow-y:auto;border:1px solid var(--gt-border);border-radius:8px;padding:10px;display:flex;flex-direction:column;gap:6px">
        <?php foreach ((array)$all_cats as $cat): ?>
        <label style="display:flex;align-items:center;gap:8px;cursor:pointer">
            <input type="checkbox" name="lo_exclude_cats[]" value="<?php echo intval($cat->term_id); ?>"
                   <?php checked(in_array($cat->term_id,(array)($lo['exclude_categories']??[]))); ?> style="accent-color:var(--gt-red)">
            <span style="font-size:13px;color:var(--gt-white)"><?php echo esc_html($cat->name); ?></span>
        </label>
        <?php endforeach; ?>
    </div>
</div>

<div class="gt-card gt-animate-in">
    <div style="display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:16px">
        <div class="gt-text-soft gt-text-sm">Lockout rules take priority over all PRO rules and packs</div>
        <button type="submit" name="gtbogo_save_lockout" class="gt-btn gt-btn-primary" style="padding:12px 32px">💾 Save Lockout Settings</button>
    </div>
</div>
</form>
<?php endif; ?>
<?php require_once __DIR__ . '/../ui-system/wrapper-end.php'; ?>
