<?php
/**
 * Admin Dashboard Page Template - SEAZONIFY V7 REAL DATA INTEGRATION
 * Maintaining V6 Authentic Theme with Verified Plugin Data
 */

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

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

// Data Normalization (Authentic Site Data)
$seazonify_is_connected = !empty($seazonify_status_data);
$seazonify_user_name = isset($seazonify_status_data['user']['name']) ? $seazonify_status_data['user']['name'] : esc_html__('Explorer', 'seazonify');
$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'));
$seazonify_connected_at = isset($seazonify_status_data['connection']['connectedAt']) ? gmdate('M j, Y', strtotime($seazonify_status_data['connection']['connectedAt'])) : esc_html__('Unknown', 'seazonify');

// Plugin Specific Data (The "Correct" data from previous iterations)
$seazonify_engine_enabled = !empty($seazonify_settings['enabled']);

// Resolve Effect Names
$seazonify_visual_id = !empty($seazonify_settings['visual_effect']) ? $seazonify_settings['visual_effect'] : '';
$seazonify_audio_id  = !empty($seazonify_settings['audio_effect']) ? $seazonify_settings['audio_effect'] : '';

$seazonify_visual_effect = esc_html__('None selected', 'seazonify');
if ($seazonify_visual_id) {
    $seazonify_v_data = Seazonify_Effects::get_effect($seazonify_visual_id, 'visual');
    $seazonify_visual_effect = $seazonify_v_data ? $seazonify_v_data['name'] : $seazonify_visual_id;
}

$seazonify_audio_effect = esc_html__('None selected', 'seazonify');
if ($seazonify_audio_id) {
    $seazonify_a_data = Seazonify_Effects::get_effect($seazonify_audio_id, 'audio');
    $seazonify_audio_effect = $seazonify_a_data ? $seazonify_a_data['name'] : $seazonify_audio_id;
}

// Greeting logic from Dashboard.tsx
$seazonify_hour = (int)gmdate('H');
$seazonify_greeting = esc_html__('Good evening', 'seazonify');
if ($seazonify_hour < 12) $seazonify_greeting = esc_html__('Good morning', 'seazonify');
else if ($seazonify_hour < 17) $seazonify_greeting = esc_html__('Good afternoon', 'seazonify');
?>

<div class="sz-v6-workspace dashboard-view">
    <!-- Seazonify Authentic Header -->
    <header class="sz-v6-header">
        <div class="sz-v6-header-left">
            <h1 class="sz-v6-greeting"><?php echo esc_html($seazonify_greeting); ?>, <span class="sz-text-gradient"><?php echo esc_html($seazonify_user_name); ?></span>!</h1>
            <p class="sz-v6-performance-msg"><?php esc_html_e('Welcome back. Your Seazonify engine is currently', 'seazonify'); ?> <strong><?php echo $seazonify_engine_enabled ? esc_html__('Active', 'seazonify') : esc_html__('Paused', 'seazonify'); ?></strong>.</p>
        </div>
        <div class="sz-v6-header-actions">
            <div class="sz-v6-sync-pill">
                <span class="sz-v6-dot online"></span> <?php esc_html_e('Real-time Sync', 'seazonify'); ?>
            </div>
            <a href="<?php echo esc_url(admin_url('customize.php?autofocus[section]=seazonify_section')); ?>" class="sz-v6-btn-magical">
                <span class="dashicons dashicons-admin-settings"></span> <?php esc_html_e('Explore Effects', 'seazonify'); ?>
            </a>
        </div>
    </header>

    <!-- Metrics Grid (Plugin Specific Utility) -->
    <div class="sz-v6-metrics-grid">
        <div class="sz-v6-card">
            <div class="sz-v6-card-header">
                <h3><?php esc_html_e('Engine Status', 'seazonify'); ?></h3>
                <span class="dashicons dashicons-performance <?php echo $seazonify_engine_enabled ? 'sz-icon-green' : 'sz-icon-orange'; ?>"></span>
            </div>
            <div class="sz-v6-card-content">
                <div class="sz-v6-metric-value"><?php echo $seazonify_engine_enabled ? esc_html__('Enabled', 'seazonify') : esc_html__('Disabled', 'seazonify'); ?></div>
                <div class="sz-v6-metric-delta"><?php esc_html_e('Global toggle state', 'seazonify'); ?></div>
            </div>
        </div>

        <div class="sz-v6-card">
            <div class="sz-v6-card-header">
                <h3><?php esc_html_e('Visual Effect', 'seazonify'); ?></h3>
                <span class="dashicons dashicons-art sz-icon-blue"></span>
            </div>
            <div class="sz-v6-card-content">
                <div class="sz-v6-metric-value"><?php echo $seazonify_visual_effect !== esc_html__('None selected', 'seazonify') ? esc_html__('Active', 'seazonify') : esc_html__('None', 'seazonify'); ?></div>
                <div class="sz-v6-metric-delta"><?php echo esc_html($seazonify_visual_effect); ?></div>
            </div>
        </div>

        <div class="sz-v6-card">
            <div class="sz-v6-card-header">
                <h3><?php esc_html_e('Audio Effect', 'seazonify'); ?></h3>
                <span class="dashicons dashicons-format-audio sz-icon-purple"></span>
            </div>
            <div class="sz-v6-card-content">
                <div class="sz-v6-metric-value"><?php echo $seazonify_audio_effect !== esc_html__('None selected', 'seazonify') ? esc_html__('Active', 'seazonify') : esc_html__('None', 'seazonify'); ?></div>
                <div class="sz-v6-metric-delta"><?php echo esc_html($seazonify_audio_effect); ?></div>
            </div>
        </div>

        <div class="sz-v6-card">
            <div class="sz-v6-card-header">
                <h3><?php esc_html_e('Subscription', 'seazonify'); ?></h3>
                <span class="dashicons dashicons-awards sz-icon-gold"></span>
            </div>
            <div class="sz-v6-card-content">
                <div class="sz-v6-metric-value"><?php echo esc_html($seazonify_plan_name); ?></div>
                <div class="sz-v6-metric-delta"><?php esc_html_e('Your active subscription', 'seazonify'); ?></div>
            </div>
        </div>
    </div>

    <!-- Main Content Area -->
    <div class="sz-v6-main-grid">
        <!-- Configuration & Guide Column -->
        <div class="sz-v6-main-col">
            <div class="sz-v6-card">
                <div class="sz-v6-card-header">
                    <h3><?php esc_html_e('Quick Start Guide', 'seazonify'); ?></h3>
                    <span class="dashicons dashicons-lightbulb sz-icon-blue"></span>
                </div>
                <div class="sz-v6-card-content">
                    <div class="sz-v6-insight-pill sz-bg-purple-soft">
                        <div class="sz-v6-insight-icon sz-bg-purple">
                            <span class="dashicons dashicons-sos"></span>
                        </div>
                        <div class="sz-v6-insight-text">
                            <strong><?php esc_html_e('Setup in Seconds', 'seazonify'); ?></strong>
                            <p>1. <a href="<?php echo esc_url(admin_url('customize.php?autofocus[section]=seazonify_section')); ?>"><?php esc_html_e('Pick an effect', 'seazonify'); ?></a> &rarr; 2. <?php esc_html_e('Enable the engine', 'seazonify'); ?> &rarr; 3. <?php esc_html_e('Save & Publish', 'seazonify'); ?>.</p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="sz-v6-card">
                <div class="sz-v6-card-header">
                    <h3><?php esc_html_e('Support & Resources', 'seazonify'); ?></h3>
                    <span class="dashicons dashicons-sos sz-icon-blue"></span>
                </div>
                <div class="sz-v6-card-content">
                    <div class="sz-v6-actions-grid">
                        <a href="https://seazonify.com/aura/blogs/" target="_blank" class="sz-v6-action-link">
                            <span class="dashicons dashicons-rss"></span> <?php esc_html_e('Blog', 'seazonify'); ?>
                        </a>
                        <a href="https://discord.com/invite/HknBRy9h46" target="_blank" class="sz-v6-action-link">
                            <span class="dashicons dashicons-groups"></span> <?php esc_html_e('Discord Community', 'seazonify'); ?>
                        </a>
                        <a href="https://seazonify.com/aura/docs/" target="_blank" class="sz-v6-action-link">
                            <span class="dashicons dashicons-book"></span> <?php esc_html_e('Documentation', 'seazonify'); ?>
                        </a>
                        <a href="https://www.x.com/Seazonify" target="_blank" class="sz-v6-action-link">
                            <span class="dashicons dashicons-share"></span> <?php esc_html_e('Twitter', 'seazonify'); ?>
                        </a>
                        <a href="https://seazonify.com/feedback" target="_blank" class="sz-v6-action-link">
                            <span class="dashicons dashicons-testimonial"></span> <?php esc_html_e('Feedback', 'seazonify'); ?>
                        </a>
                        <?php 
                            $seazonify_app_token = isset($seazonify_settings['app_token']) ? $seazonify_settings['app_token'] : '';
                            $seazonify_download_url = "https://app.seazonify.com/plugins/wordpress?login=true&platform=wordpress&key=" . urlencode($seazonify_app_token);
                        ?>
                        <a href="<?php echo esc_url($seazonify_download_url); ?>" target="_blank" class="sz-v6-action-link sz-v6-action-upgrade">
                            <span class="dashicons dashicons-download"></span> <?php esc_html_e('Download Full version', 'seazonify'); ?>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <!-- Connection Details (Authentic Account Box) -->
        <div class="sz-v6-card">
            <div class="sz-v6-card-header">
                <h2><?php esc_html_e('Account', 'seazonify'); ?></h2>
            </div>
            <div class="sz-v6-card-content">
                <div class="sz-v6-account-meta">
                    <div class="sz-v6-avatar-box">
                        <span class="dashicons dashicons-admin-users"></span>
                    </div>
                    <div class="sz-v6-account-text">
                        <strong><?php echo esc_html($seazonify_user_name); ?></strong>
                        <span><?php echo esc_html($seazonify_user_email); ?></span>
                    </div>
                </div>
                <div class="sz-v6-divider"></div>
                <div class="sz-v6-license-status">
                    <span class="sz-v6-badge-pro"><?php echo esc_html($seazonify_plan_name); ?></span>
                    <p><?php esc_html_e('Verified on', 'seazonify'); ?> <?php echo esc_html($seazonify_connected_at); ?></p>
                </div>
                <div class="sz-v6-actions-list">
                    <?php 
                        $seazonify_app_token = isset($seazonify_settings['app_token']) ? $seazonify_settings['app_token'] : '';
                        $seazonify_profile_url = "https://app.seazonify.com/profile?login=true&platform=wordpress&key=" . urlencode($seazonify_app_token);
                    ?>
                    <a href="<?php echo esc_url($seazonify_profile_url); ?>" target="_blank" class="sz-v6-action-link">
                        <span class="dashicons dashicons-external"></span> <?php esc_html_e('View Account Profile', 'seazonify'); ?>
                    </a>
                    <a href="https://seazonify.com/aura/docs/" target="_blank" class="sz-v6-action-link">
                        <span class="dashicons dashicons-book"></span> <?php esc_html_e('Documentation', 'seazonify'); ?>
                    </a>
                </div>
            </div>
        </div>
    </div>
<?php
// Inline styles moved to admin.css
?>
</div>
