<?php
/**
 * Pro Presets Section (Create Preset + Saved Presets)
 *
 * Included from configurations-page.php when pro/templates/ exists.
 *
 * @package Switchboard
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Variables expected from parent template:
// $presets - array of saved presets
// $active - array of active modules
?>

<!-- Create Preset -->
<div class="sb-module-card">
    <div class="sb-module-card-header">
        <div class="sb-module-card-header-left">
            <h3 class="sb-module-card-title">Create Preset</h3>
            <p class="sb-module-card-desc">Save your current module configuration</p>
        </div>
    </div>
    <form method="post" id="preset-form">
        <?php wp_nonce_field('switchboard_preset_save'); ?>
        <div class="sb-module-card-body">
            <p class="sb-config-help">Presets are saved in your database. Use them to quickly switch between configurations on this site.</p>
            <div class="sb-config-form">
                <div class="sb-config-form-field">
                    <label for="preset_name_inline">Preset Name</label>
                    <input type="text" name="preset_name" id="preset_name_inline" required placeholder="e.g., My Production Setup">
                </div>
                <div class="sb-config-form-field">
                    <label for="preset_description_inline">Description (optional)</label>
                    <textarea name="preset_description" id="preset_description_inline" rows="2" placeholder="Brief description of this configuration..."></textarea>
                </div>
            </div>
        </div>
        <div class="sb-module-card-footer">
            <span class="sb-config-meta"><?php echo (int) count($active); ?> active modules</span>
            <button type="submit" name="switchboard_save_preset" class="sb-module-button sb-module-button-primary">Save Preset</button>
        </div>
    </form>
</div>

<!-- Saved Presets -->
<div class="sb-module-card">
    <div class="sb-module-card-header">
        <div class="sb-module-card-header-left">
            <h3 class="sb-module-card-title">Saved Presets</h3>
            <p class="sb-module-card-desc"><?php echo !empty($presets) ? count($presets) . ' preset' . (count($presets) !== 1 ? 's' : '') . ' saved' : 'No presets saved yet'; ?></p>
        </div>
    </div>
    <div class="sb-module-card-body">
        <?php if (empty($presets)): ?>
            <div class="sb-config-empty">
                <p>Save your current configuration above to create your first preset.</p>
            </div>
        <?php else: ?>
            <div class="sb-preset-list">
                <?php
                foreach ($presets as $preset_id => $preset):
                    $modules_data = $preset['modules'];
                    if (isset($modules_data['meta']) && isset($modules_data['modules'])) {
                        $module_count = count($modules_data['modules']);
                    } else {
                        $module_count = count($modules_data);
                    }
                ?>
                    <div class="sb-preset-item">
                        <div class="sb-preset-info">
                            <div class="sb-preset-header">
                                <span class="sb-preset-name"><?php echo esc_html($preset['name']); ?></span>
                            </div>
                            <?php if (!empty($preset['description'])): ?>
                                <p class="sb-preset-desc"><?php echo esc_html($preset['description']); ?></p>
                            <?php endif; ?>
                            <div class="sb-preset-meta">
                                <span><?php echo (int) $module_count; ?> modules</span>
                                <span>&bull;</span>
                                <span><?php echo esc_html(date('M j, Y', strtotime($preset['created']))); ?></span>
                            </div>
                        </div>
                        <div class="sb-preset-actions">
                            <form method="post" style="display:inline;">
                                <?php wp_nonce_field('switchboard_preset_load'); ?>
                                <input type="hidden" name="preset_id" value="<?php echo esc_attr($preset_id); ?>">
                                <button type="submit" name="switchboard_load_preset" class="sb-module-button sb-module-button-secondary sb-module-button-sm">
                                    Load
                                </button>
                            </form>
                            <form method="post" style="display:inline;">
                                <?php wp_nonce_field('switchboard_preset_delete'); ?>
                                <input type="hidden" name="preset_id" value="<?php echo esc_attr($preset_id); ?>">
                                <button type="submit" name="switchboard_delete_preset" class="sb-module-button sb-module-button-danger sb-module-button-sm" onclick="return confirm('Delete this preset?');">Delete</button>
                            </form>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</div>
