<?php
/**
 * Admin Settings Page Template - SEAZONIFY V6 AUTHENTIC ALIGNMENT
 * Precision matching with seasonify-web-glow-main
 */

if (!defined('ABSPATH')) {
    exit;
}

$seazonify_settings = get_option('seazonify_settings', array());
$seazonify_admin = new Seazonify_Admin();
$seazonify_status_data = $seazonify_admin->get_connection_status();

$seazonify_is_connected = !empty($seazonify_status_data);
$seazonify_user_email = isset($seazonify_status_data['user']['email']) ? $seazonify_status_data['user']['email'] : esc_html__('Not Connected', 'seazonify');
$seazonify_plan_name = isset($seazonify_status_data['subscription']['plan']) ? $seazonify_status_data['subscription']['plan'] : (isset($seazonify_status_data['user']['plan']) ? ucfirst($seazonify_status_data['user']['plan']) : esc_html__('Free', 'seazonify'));
?>

<div class="sz-v6-workspace settings-view">
    <!-- Authentic Header -->
    <header class="sz-v6-header">
        <div class="sz-v6-header-left">
            <h1 class="sz-v6-title"><?php esc_html_e('Settings', 'seazonify'); ?></h1>
            <p class="sz-v6-subtitle"><?php esc_html_e('Configure your site\'s atmosphere and engine limits', 'seazonify'); ?></p>
        </div>
        <div class="sz-v6-header-actions">
            <div id="sz-v6-save-msg" class="sz-v6-alert-pill hidden">
                <span class="dashicons dashicons-saved"></span> <?php esc_html_e('Changes Synced', 'seazonify'); ?>
            </div>
            <button type="button" id="sz-v6-save-btn" class="sz-v6-btn-primary">
                <?php esc_html_e('Save Changes', 'seazonify'); ?>
            </button>
        </div>
    </header>

    <div class="sz-v6-workspace-grid">
        <!-- Authentic Sidebar (Blue-700 Strategy) -->
        <aside class="sz-v6-sidebar">
            <nav class="sz-v6-nav">
                <button class="sz-v6-nav-item active" data-tab="general">
                    <div class="icon"><span class="dashicons dashicons-admin-settings"></span></div>
                    <div class="text">
                        <strong><?php esc_html_e('General', 'seazonify'); ?></strong>
                        <span><?php esc_html_e('Engine defaults', 'seazonify'); ?></span>
                    </div>
                </button>
                <button class="sz-v6-nav-item" data-tab="visibility">
                    <div class="icon"><span class="dashicons dashicons-visibility"></span></div>
                    <div class="text">
                        <strong><?php esc_html_e('Visibility', 'seazonify'); ?></strong>
                        <span><?php esc_html_e('Targeting rules', 'seazonify'); ?></span>
                    </div>
                </button>
                <button class="sz-v6-nav-item" data-tab="performance">
                    <div class="icon"><span class="dashicons dashicons-performance"></span></div>
                    <div class="text">
                        <strong><?php esc_html_e('Performance', 'seazonify'); ?></strong>
                        <span><?php esc_html_e('Optimization', 'seazonify'); ?></span>
                    </div>
                </button>
                <button class="sz-v6-nav-item" data-tab="license">
                    <div class="icon"><span class="dashicons dashicons-cloud"></span></div>
                    <div class="text">
                        <strong><?php esc_html_e('Connection', 'seazonify'); ?></strong>
                        <span><?php esc_html_e('App status', 'seazonify'); ?></span>
                    </div>
                </button>
            </nav>

            <div class="sz-v6-sidebar-footer">
                <div class="sz-v6-mini-status">
                    <span class="label"><?php esc_html_e('Primary User', 'seazonify'); ?></span>
                    <strong class="value"><?php echo esc_html($seazonify_user_email); ?></strong>
                </div>
            </div>
        </aside>

        <!-- Main Content -->
        <main class="sz-v6-main-stage">
            <form method="post" action="options.php" id="sz-v6-form">
                <?php settings_fields('seazonify_settings_group'); ?>
                
                <!-- General Section -->
                <section class="sz-v6-section active" id="tab-general">
                    <div class="sz-v6-card">
                        <div class="sz-v6-card-header">
                            <h2><?php esc_html_e('Engine Configuration', 'seazonify'); ?></h2>
                            <p><?php esc_html_e('Global switches for the Seazonify atmosphere engine.', 'seazonify'); ?></p>
                        </div>
                        <div class="sz-v6-card-content">
                            <div class="sz-v6-control-row">
                                <div class="info">
                                    <label><?php esc_html_e('Master Activation', 'seazonify'); ?></label>
                                    <p><?php esc_html_e('Globally enable or disable all seasonal effects on your website.', 'seazonify'); ?></p>
                                </div>
                                <div class="control">
                                    <label class="sz-v6-switch">
                                        <input type="checkbox" name="seazonify_settings[enabled]" value="1" <?php checked(!empty($seazonify_settings['enabled'])); ?>>
                                        <span class="sz-v6-slider"></span>
                                    </label>
                                </div>
                            </div>
                            <div class="sz-v6-divider"></div>
                            <div class="sz-v6-control-row">
                                <div class="info">
                                    <label><?php esc_html_e('Interactive Toggle', 'seazonify'); ?></label>
                                    <p><?php esc_html_e('Show a floating controller for visitors to customize their experience.', 'seazonify'); ?></p>
                                </div>
                                <div class="control">
                                    <label class="sz-v6-switch">
                                        <input type="checkbox" name="seazonify_settings[show_controller]" value="1" <?php checked(!empty($seazonify_settings['show_controller'])); ?>>
                                        <span class="sz-v6-slider"></span>
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>
                </section>

                <!-- Visibility Section -->
                <section class="sz-v6-section" id="tab-visibility">
                    <div class="sz-v6-card">
                        <div class="sz-v6-card-header">
                            <h2><?php esc_html_e('Display & Visibility', 'seazonify'); ?></h2>
                            <p><?php esc_html_e('Control where specific effects are rendered across your site.', 'seazonify'); ?></p>
                        </div>
                        <div class="sz-v6-card-content">
                            <div class="sz-v6-control-stack">
                                <label class="sz-v6-label-bold"><?php esc_html_e('Target Location', 'seazonify'); ?></label>
                                <select name="seazonify_settings[display_on]" id="sz-display-on" class="sz-v6-select">
                                    <option value="all" <?php selected(isset($seazonify_settings['display_on']) ? $seazonify_settings['display_on'] : 'all', 'all'); ?>><?php esc_html_e('Global (Entire Website)', 'seazonify'); ?></option>
                                    <option value="homepage" <?php selected(isset($seazonify_settings['display_on']) ? $seazonify_settings['display_on'] : 'all', 'homepage'); ?>><?php esc_html_e('Homepage Only', 'seazonify'); ?></option>
                                    <option value="pages" <?php selected(isset($seazonify_settings['display_on']) ? $seazonify_settings['display_on'] : 'all', 'pages'); ?>><?php esc_html_e('Specific Pages', 'seazonify'); ?></option>
                                </select>
                            </div>

                            <div class="sz-v6-conditional" data-show-if="pages">
                                <div class="sz-v6-divider"></div>
                                <label class="sz-v6-label-bold" style="margin-bottom: 12px; display: block;"><?php esc_html_e('Select Target Pages', 'seazonify'); ?></label>
                                <div class="sz-v6-list-grid">
                                    <?php
                                    $seazonify_pages = get_pages();
                                    $seazonify_include_pages = isset($seazonify_settings['include_pages']) ? (array)$seazonify_settings['include_pages'] : array();
                                    foreach ($seazonify_pages as $seazonify_page): ?>
                                    <label class="sz-v6-checkbox-item">
                                        <input type="checkbox" name="seazonify_settings[include_pages][]" value="<?php echo esc_attr($seazonify_page->ID); ?>" <?php checked(in_array($seazonify_page->ID, $seazonify_include_pages)); ?>>
                                        <span class="text"><?php echo esc_html($seazonify_page->post_title); ?></span>
                                    </label>
                                    <?php endforeach; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </section>

                <!-- Performance Section -->
                <section class="sz-v6-section" id="tab-performance">
                    <div class="sz-v6-card">
                        <div class="sz-v6-card-header">
                            <h2><?php esc_html_e('Optimization', 'seazonify'); ?></h2>
                            <p><?php esc_html_e('Manage how assets are served and cached for performance.', 'seazonify'); ?></p>
                        </div>
                        <div class="sz-v6-card-content">
                            <div class="sz-v6-control-row">
                                <div class="info">
                                    <label><?php esc_html_e('Cache Duration', 'seazonify'); ?></label>
                                    <p><?php esc_html_e('How long to store seasonal assets locally (in seconds).', 'seazonify'); ?></p>
                                </div>
                                <div class="control">
                                    <div class="sz-v6-input-group">
                                        <input type="number" name="seazonify_settings[cache_duration]" value="<?php echo esc_attr(isset($seazonify_settings['cache_duration']) ? $seazonify_settings['cache_duration'] : 3600); ?>" min="300">
                                        <span class="addon"><?php esc_html_e('SEC', 'seazonify'); ?></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </section>

                <!-- License Section -->
                <section class="sz-v6-section" id="tab-license">
                    <div class="sz-v6-card">
                        <div class="sz-v6-card-header">
                            <h2><?php esc_html_e('App Connection', 'seazonify'); ?></h2>
                        </div>
                        <div class="sz-v6-card-content">
                            <div class="sz-v6-status-box success">
                                <span class="dashicons dashicons-yes-alt"></span>
                                <div class="text">
                                    <strong><?php esc_html_e('Active Connection', 'seazonify'); ?></strong>
                                    <p><?php 
                                        /* translators: %s: Plan name */
                                        printf(esc_html__('Your site is on the %s plan.', 'seazonify'), '<strong>' . esc_html($seazonify_plan_name) . '</strong>'); 
                                    ?></p>
                                </div>
                            </div>
                            <div class="sz-v6-divider"></div>
                            <button type="button" id="sz-v6-deactivate" class="sz-v6-btn-danger"><?php esc_html_e('Log out', 'seazonify'); ?></button>
                        </div>
                    </div>
                </section>

                <input type="submit" name="submit" id="sz-actual-submit" style="display:none;">
            </form>
        </main>
    </div>
</div>

<form id="sz-v6-disconnect-form" method="post" action="" style="display:none;">
    <?php wp_nonce_field('seazonify_disconnect_action', 'seazonify_disconnect_nonce'); ?>
    <input type="hidden" name="seazonify_disconnect" value="1">
</form>
<?php
// Inline styles and scripts moved to admin.css and admin.js
?>
