<?php
/**
 * Configurations Page Template
 *
 * Displays Import/Export (shared) and Presets (pro-only via include).
 *
 * @package Switchboard
 */

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

// Variables expected to be passed:
// $presets - array of saved presets
// $active - array of active modules

// Get upsell URL for free version fallback
$switchboard_upsell_url = '';
$switchboard_json_path = SWITCHBOARD_PATH . 'pro-modules.json';
if (file_exists($switchboard_json_path)) {
    $switchboard_config = json_decode(file_get_contents($switchboard_json_path), true); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file read
    if (!empty($switchboard_config['upsell_url'])) {
        $switchboard_upsell_url = $switchboard_config['upsell_url'];
    }
}
if (empty($switchboard_upsell_url)) {
    $switchboard_upsell_url = 'https://wpswitchboard.com/pricing';
}

$switchboard_pro_presets_path = SWITCHBOARD_PATH . 'pro/templates/presets-section.php';
?>

<div class="switchboard-configurations-page">
    <div class="sb-config-grid">
        <!-- Left Column -->
        <div class="sb-config-column">
            <?php if (file_exists($switchboard_pro_presets_path)):
                include $switchboard_pro_presets_path;
            else: ?>
            <!-- Presets Upsell Card (Free only) -->
            <div class="sb-module-card sb-upsell-card">
                <div class="sb-module-card-header">
                    <div class="sb-module-card-header-left">
                        <h3 class="sb-module-card-title">Presets</h3>
                        <p class="sb-module-card-desc">Save &amp; load module configurations</p>
                    </div>
                    <span class="module-badge pro-badge-locked">PRO</span>
                </div>
                <div class="sb-module-card-body">
                    <div class="sb-upsell-features">
                        <div class="sb-upsell-feature">
                            <span class="sb-upsell-check">&#10003;</span>
                            <span>Save named presets of your active modules</span>
                        </div>
                        <div class="sb-upsell-feature">
                            <span class="sb-upsell-check">&#10003;</span>
                            <span>Instantly switch between configurations</span>
                        </div>
                        <div class="sb-upsell-feature">
                            <span class="sb-upsell-check">&#10003;</span>
                            <span>Perfect for staging vs production setups</span>
                        </div>
                    </div>
                </div>
                <div class="sb-module-card-footer">
                    <a href="<?php echo esc_url($switchboard_upsell_url); ?>" target="_blank" rel="noopener noreferrer" class="sb-module-button sb-module-button-primary sb-upsell-btn">
                        Upgrade to Pro
                    </a>
                </div>
            </div>
            <?php endif; ?>
        </div>

        <!-- Right Column -->
        <div class="sb-config-column">
            <!-- Export Configuration -->
            <div class="sb-module-card">
                <div class="sb-module-card-header">
                    <div class="sb-module-card-header-left">
                        <h3 class="sb-module-card-title">Export Configuration</h3>
                        <p class="sb-module-card-desc">Download as a .switchboard file</p>
                    </div>
                </div>
                <div class="sb-module-card-body">
                    <p class="sb-config-help">Export files can be used to backup your configuration or transfer it to another WordPress site.</p>
                    <p class="sb-config-text">Download your current module settings as a <code>.switchboard</code> file.</p>
                </div>
                <div class="sb-module-card-footer">
                    <span class="sb-config-meta"><?php echo (int) count($active); ?> active modules</span>
                    <button type="button" id="export-current-config" class="sb-module-button sb-module-button-primary">
                        Export Configuration
                    </button>
                </div>
            </div>

            <!-- Import Configuration -->
            <div class="sb-module-card">
                <div class="sb-module-card-header">
                    <div class="sb-module-card-header-left">
                        <h3 class="sb-module-card-title">Import Configuration</h3>
                        <p class="sb-module-card-desc">Restore from a .switchboard file</p>
                    </div>
                </div>
                <div class="sb-module-card-body">
                    <p class="sb-config-help">Import a configuration file from another site or restore from a backup.</p>
                    <input type="file" id="import-file-input-page" accept=".switchboard" style="position: absolute; left: -9999px;">

                    <div class="sb-config-dropzone" id="import-dropzone">
                        <div class="sb-config-dropzone-content">
                            <span class="sb-config-dropzone-icon">&#128193;</span>
                            <p>Drag & drop a .switchboard file here</p>
                            <span class="sb-config-dropzone-or">or</span>
                            <button type="button" class="sb-module-button sb-module-button-secondary sb-module-button-sm" id="browse-file-btn">Browse Files</button>
                        </div>
                    </div>

                    <!-- Import Preview (shown after file selected) -->
                    <div id="import-preview-page" class="sb-config-preview" style="display: none;">
                        <div class="sb-config-preview-header">
                            <h4>File Preview</h4>
                            <button type="button" class="sb-config-preview-clear" id="clear-import-file">&times;</button>
                        </div>
                        <div class="sb-config-preview-details">
                            <div class="sb-config-preview-row">
                                <span class="sb-config-preview-label">Name:</span>
                                <span class="sb-config-preview-value" id="preview-name-page"></span>
                            </div>
                            <div class="sb-config-preview-row">
                                <span class="sb-config-preview-label">Description:</span>
                                <span class="sb-config-preview-value" id="preview-description-page"></span>
                            </div>
                            <div class="sb-config-preview-row">
                                <span class="sb-config-preview-label">Modules:</span>
                                <span class="sb-config-preview-value" id="preview-modules-page"></span>
                            </div>
                        </div>
                        <div class="sb-config-preview-options">
                            <label class="sb-config-checkbox">
                                <input type="checkbox" id="import-apply-immediately-page" checked>
                                <span>Apply settings immediately after import</span>
                            </label>
                        </div>
                        <div class="sb-config-preview-actions">
                            <button type="button" id="confirm-import-page" class="sb-module-button sb-module-button-primary">
                                Import & Apply
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
