<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : planner.php
 * @location     : /modules/planner/planner.php
 * @type         : Admin Page
 * @scope        : Module Only
 * @allowed      : Planner module logic, subpage routing, step data functions
 * @reusability  : Single Use
 * @notes        : Main planner page with subpage routing and step data functions
 */
 

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

// Component render functions are loaded centrally via global-layout.php

/**
 * Get step data for Planner workflow guide
 * 
 * @return array Array of step data with status and counts
 */
function igny8_get_planner_step_data() {
    global $wpdb;
    
    // Get counts for each step
    $keywords_count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_keywords");
    $unmapped_keywords = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_keywords WHERE cluster_id IS NULL OR cluster_id = 0");
    $clusters_count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_clusters");
    $ideas_count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_content_ideas");
    $queued_ideas = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_content_ideas WHERE status = 'new'");
    
    // Check sector selection
    $sector_selected = !empty(igny8_get_saved_sector_selection());
    
    return [
        'keywords' => [
            'count' => $keywords_count,
            'unmapped' => $unmapped_keywords,
            'status' => $keywords_count > 0 ? 'completed' : 'pending'
        ],
        'sector' => [
            'selected' => $sector_selected,
            'status' => $sector_selected ? 'completed' : 'current'
        ],
        'clusters' => [
            'count' => $clusters_count,
            'unmapped_keywords' => $unmapped_keywords,
            'status' => $unmapped_keywords == 0 && $clusters_count > 0 ? 'completed' : ($unmapped_keywords > 0 ? 'current' : 'pending')
        ],
        'ideas' => [
            'count' => $ideas_count,
            'status' => $ideas_count > 0 ? 'completed' : 'pending'
        ],
        'queue' => [
            'queued_ideas' => $queued_ideas,
            'status' => $queued_ideas == 0 && $ideas_count > 0 ? 'completed' : ($queued_ideas > 0 ? 'current' : 'pending')
        ]
    ];
}

// Handle URL parameters for subpages
$subpage = $_GET['sm'] ?? 'home';
$GLOBALS['current_submodule'] = $subpage;
$GLOBALS['current_module'] = 'planner';

// Start output buffering
ob_start();

switch ($subpage) {
    case 'keywords':
        include plugin_dir_path(__FILE__) . 'keywords.php';
        break;
    case 'clusters':
        include plugin_dir_path(__FILE__) . 'clusters.php';
        break;
    case 'ideas':
        include plugin_dir_path(__FILE__) . 'ideas.php';
        break;
    case 'home':
    default:
        // Home dashboard content
        ?>
    <div class="igny8-module-home">
        
        <?php
        // Check if sector is selected - show alert if not (reuse existing function)
        $sector_options = igny8_get_sector_options();
        if (empty($sector_options)):
        ?>
        <!-- Sector Selection Required Alert -->
        <div class="igny8-card igny8-card-warning" style="margin-bottom: 20px; border-left: 4px solid var(--amber); background: linear-gradient(135deg, #fff8f0 0%, #fef3e7 100%);">
            <div class="igny8-card-header">
                <span style="font-size: 20px; margin-right: 12px;">⚠</span>
                <strong>Please select a Sector to continue.</strong> Sector selection is required to start AI-based workflows.
            </div>
        </div>
        <?php endif; ?>
        
    <?php
    // Load KPI data for dashboard calculations
    if (!defined('IGNY8_INCLUDE_CONFIG')) {
        define('IGNY8_INCLUDE_CONFIG', true);
    }
    $kpi_config = $GLOBALS['igny8_kpi_config'] ?? []; // Loaded globally in igny8.php
    $kpi_data = igny8_get_kpi_data_safe('planner_home', $kpi_config['planner_home'] ?? []);
    
    // Calculate dashboard metrics
    $total_keywords = $kpi_data['total_keywords'] ?? 0;
    $mapped_keywords = $kpi_data['mapped_keywords'] ?? 0;
    $total_clusters = $kpi_data['total_clusters'] ?? 0;
    $clusters_with_ideas = $kpi_data['clusters_with_ideas'] ?? 0;
    $total_ideas = $kpi_data['total_ideas'] ?? 0;
    $queued_ideas = $kpi_data['queued_ideas'] ?? 0;
    
    // Calculate percentages for progress bars
    $keyword_mapping_pct = $total_keywords > 0 ? round(($mapped_keywords / $total_keywords) * 100) : 0;
    $clusters_ideas_pct = $total_clusters > 0 ? round(($clusters_with_ideas / $total_clusters) * 100) : 0;
    $ideas_queued_pct = $total_ideas > 0 ? round(($queued_ideas / $total_ideas) * 100) : 0;
    
    // Use fixed colors matching top metric cards
    $keyword_color = 'blue';    // Keywords Ready card is blue
    $cluster_color = 'green';   // Clusters Built card is green  
    $idea_color = 'amber';      // Ideas Generated card is amber
    ?>
        
    <!-- Planning Status Cards -->
    <div class="igny8-dashboard-section">
        <div class="igny8-grid-3 igny8-status-cards">
            <!-- Keywords Ready Card -->
            <div class="igny8-card igny8-status-card igny8-clickable-card igny8-status-blue" onclick="window.location.href='<?php echo admin_url('admin.php?page=igny8-planner&sm=keywords'); ?>'">
                <div class="igny8-card-body">
                    <div class="igny8-status-metric">
                        <span class="igny8-status-count"><?php echo $total_keywords; ?></span>
                        <span class="igny8-status-label">Keywords Ready</span>
                        <span class="igny8-status-desc">Research, analyze, and manage keywords strategy</span>
                    </div>
                    <div class="igny8-status-icon">
                        <span class="dashicons dashicons-search igny8-dashboard-icon-sm"></span>
                    </div>
                </div>
            </div>
            
            <!-- Clusters Built Card -->
            <div class="igny8-card igny8-status-card igny8-clickable-card igny8-status-green" onclick="window.location.href='<?php echo admin_url('admin.php?page=igny8-planner&sm=clusters'); ?>'">
                <div class="igny8-card-body">
                    <div class="igny8-status-metric">
                        <span class="igny8-status-count"><?php echo $total_clusters; ?></span>
                        <span class="igny8-status-label">Clusters Built</span>
                        <span class="igny8-status-desc">Organize keywords into strategic topical clusters</span>
                    </div>
                    <div class="igny8-status-icon">
                        <span class="dashicons dashicons-groups igny8-dashboard-icon-sm"></span>
                    </div>
                </div>
            </div>
            
            <!-- Ideas Generated Card -->
            <div class="igny8-card igny8-status-card igny8-clickable-card igny8-status-amber" onclick="window.location.href='<?php echo admin_url('admin.php?page=igny8-planner&sm=ideas'); ?>'">
                <div class="igny8-card-body">
                    <div class="igny8-status-metric">
                        <span class="igny8-status-count"><?php echo $total_ideas; ?></span>
                        <span class="igny8-status-label">Ideas Generated</span>
                        <span class="igny8-status-desc">Generate creative content ideas based on semantic strategy</span>
                    </div>
                    <div class="igny8-status-icon">
                        <span class="dashicons dashicons-lightbulb igny8-dashboard-icon-sm"></span>
                    </div>
                </div>
            </div>
        </div>
    </div>
        
        <!-- Planner Workflow Steps -->
        <div class="igny8-dashboard-section">
            <div class="igny8-standard-header">
                <div class="igny8-card-header-content">
                    <div class="igny8-card-title-text">
                        <h3>Planner Workflow Steps</h3>
                        <p class="igny8-card-subtitle">Track your planning progress</p>
                    </div>
                    <div class="igny8-card-icon">
                        <span class="dashicons dashicons-update igny8-dashboard-icon-lg igny8-dashboard-icon-purple"></span>
                    </div>
                </div>
            </div>
            
            <div class="igny8-grid igny8-grid-4">
                <?php
                // Step-by-Step UX Guide
                $step_data = igny8_get_planner_step_data();
                ?>
                <div class="igny8-card igny8-step-card <?php echo $step_data['keywords']['status']; ?>">
                    <div class="igny8-card-body">
                        <div class="igny8-step-header">
                            <div class="igny8-step-number">1</div>
                            <div class="igny8-step-title">Add Keywords</div>
                        </div>
                        <div class="igny8-step-status">
                            <span class="igny8-step-status-icon"><?php echo $step_data['keywords']['status'] === 'completed' ? '✅' : '⏳'; ?></span>
                            <span class="igny8-step-status-text"><?php echo $step_data['keywords']['status'] === 'completed' ? 'Completed' : 'Pending'; ?></span>
                        </div>
                        <div class="igny8-step-data">
                            <?php if ($step_data['keywords']['count'] > 0): ?>
                                <?php echo $step_data['keywords']['count']; ?> keywords added
                            <?php else: ?>
                                No keywords yet
                            <?php endif; ?>
                        </div>
                        <?php if ($step_data['keywords']['status'] === 'pending'): ?>
                            <div class="igny8-step-action">
                                <a href="?page=igny8-planner&sm=keywords" class="igny8-btn igny8-btn-primary igny8-btn-sm">Start Now</a>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="igny8-card igny8-step-card <?php echo $step_data['sector']['status']; ?>">
                    <div class="igny8-card-body">
                        <div class="igny8-step-header">
                            <div class="igny8-step-number">2</div>
                            <div class="igny8-step-title">Select Sector</div>
                        </div>
                        <div class="igny8-step-status">
                            <span class="igny8-step-status-icon"><?php echo $step_data['sector']['status'] === 'completed' ? '✅' : '⚠'; ?></span>
                            <span class="igny8-step-status-text"><?php echo $step_data['sector']['status'] === 'completed' ? 'Selected' : 'Required'; ?></span>
                        </div>
                        <div class="igny8-step-data">
                            <?php if ($step_data['sector']['selected']): ?>
                                Sector configured
                            <?php else: ?>
                                Required for AI workflows
                            <?php endif; ?>
                        </div>
                        <?php if ($step_data['sector']['status'] === 'current'): ?>
                            <div class="igny8-step-action">
                                <a href="?page=igny8-planner" class="igny8-btn igny8-btn-primary igny8-btn-sm">Configure</a>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="igny8-card igny8-step-card <?php echo $step_data['clusters']['status']; ?>">
                    <div class="igny8-card-body">
                        <div class="igny8-step-header">
                            <div class="igny8-step-number">3</div>
                            <div class="igny8-step-title">Auto Cluster</div>
                        </div>
                        <div class="igny8-step-status">
                            <span class="igny8-step-status-icon"><?php echo $step_data['clusters']['status'] === 'completed' ? '✅' : ($step_data['clusters']['status'] === 'current' ? '⏳' : '⏳'); ?></span>
                            <span class="igny8-step-status-text"><?php echo $step_data['clusters']['status'] === 'completed' ? 'Completed' : ($step_data['clusters']['status'] === 'current' ? 'Ready' : 'Pending'); ?></span>
                        </div>
                        <div class="igny8-step-data">
                            <?php if ($step_data['clusters']['unmapped_keywords'] > 0): ?>
                                <?php echo $step_data['clusters']['unmapped_keywords']; ?> unmapped keywords
                            <?php elseif ($step_data['clusters']['count'] > 0): ?>
                                <?php echo $step_data['clusters']['count']; ?> clusters created
                            <?php else: ?>
                                No clusters yet
                            <?php endif; ?>
                        </div>
                        <?php if ($step_data['clusters']['status'] === 'current'): ?>
                            <div class="igny8-step-action">
                                <a href="?page=igny8-planner&sm=clusters" class="igny8-btn igny8-btn-primary igny8-btn-sm">Start Now</a>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="igny8-card igny8-step-card <?php echo $step_data['ideas']['status']; ?>">
                    <div class="igny8-card-body">
                        <div class="igny8-step-header">
                            <div class="igny8-step-number">4</div>
                            <div class="igny8-step-title">Generate Ideas</div>
                        </div>
                        <div class="igny8-step-status">
                            <span class="igny8-step-status-icon"><?php echo $step_data['ideas']['status'] === 'completed' ? '✅' : '⏳'; ?></span>
                            <span class="igny8-step-status-text"><?php echo $step_data['ideas']['status'] === 'completed' ? 'Completed' : 'Pending'; ?></span>
                        </div>
                        <div class="igny8-step-data">
                            <?php if ($step_data['ideas']['count'] > 0): ?>
                                <?php echo $step_data['ideas']['count']; ?> ideas generated
                            <?php else: ?>
                                No ideas yet
                            <?php endif; ?>
                        </div>
                        <?php if ($step_data['ideas']['status'] === 'pending' && $step_data['clusters']['count'] > 0): ?>
                            <div class="igny8-step-action">
                                <a href="?page=igny8-planner&sm=ideas" class="igny8-btn igny8-btn-primary igny8-btn-sm">Start Now</a>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Planner Settings & AI Integration -->
        <div class="igny8-dashboard-section">
            <div class="igny8-flex-row">
                <!-- AI Integration Settings Card -->
                <div class="igny8-card">
                    <div class="igny8-flex-row">
                        <div class="igny8-card-header-content">
                            <div class="igny8-card-title-text">
                                <h3>Workflow Status & AI Settings</h3>
                                <p class="igny8-card-subtitle igny8-centered">Workflow Status & AI Settings</p>
                            </div>
                        </div>
                        
                        <form id="igny8-ai-integration-form">
                            
                                <div class="igny8-form-group">
                                    
                                        <div class="igny8-mode-toggle-label">
                                            <span class="igny8-mode-label">Manual</span>
                                            <label class="igny8-toggle">
                                                <input type="checkbox" name="igny8_planner_mode" value="ai" <?php checked(igny8_get_ai_setting('planner_mode', 'manual'), 'ai'); ?>>
                                                <span class="igny8-toggle-slider"></span>
                                            </label>
                                            <span class="igny8-mode-label">AI Mode</span>
                                        </div>
                                    
                                </div>
                                <div class="igny8-form-actions">
                                    <button type="submit" class="igny8-btn igny8-btn-success">Save AI Integration Settings</button>
                                </div>
                            
                        </form>
                    </div>
                </div>
                
                <!-- Sector Selection Settings Card -->
                <div class="igny8-card">
                    <div class="igny8-flex-row">
                        <div class="igny8-card-header-content">
                            <div class="igny8-card-title-text">
                                <h3>Workflow Status & AI Settings</h3>
                                <p class="igny8-card-subtitle igny8-centered">Workflow Status & AI Settings</p>
                            </div>
                        </div>
                        
                        <div id="igny8-sector-selection-container">
                            <!-- Initial state: Parent sector selection -->
                            <div id="igny8-parent-selection" class="igny8-selection-step">
                                <label for="igny8-parent-sector">Select Parent Sector:</label>
                                <select id="igny8-parent-sector" class="igny8-form-control">
                                    <option value="">Choose a parent sector...</option>
                                </select>
                                <button id="igny8-lock-parent" class="igny8-btn igny8-btn-primary" style="display: none;">Lock Selection</button>
                            </div>
                            
                            <!-- Child sectors selection -->
                            <div id="igny8-child-selection" class="igny8-selection-step" style="display: none;">
                                <h4>Select Child Sectors:</h4>
                                <div id="igny8-child-checkboxes" class="igny8-checkbox-group">
                                    <!-- Child checkboxes will be populated here -->
                                </div>
                                <button id="igny8-save-selection" class="igny8-btn igny8-btn-success">Save Selection</button>
                            </div>
                            
                            <!-- Final selection display -->
                            <div id="igny8-final-selection" class="igny8-selection-step" style="display: none;">
                                
                                <div id="igny8-selected-sectors-display">
                                    <!-- Selected sectors will be displayed here -->
                                </div>
                                <button id="igny8-edit-selection" class="igny8-btn igny8-btn-outline">Remove or Edit My Selection</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
       
    <div class="igny8-grid-3">
 <!-- Progress / Readiness Summary -->
        <div class="igny8-dashboard-section">
            <div class="igny8-card">
                <div class="igny8-standard-header">
                    <div class="igny8-card-header-content">
                        <div class="igny8-card-title-text">
                            <h3>Progress & Readiness Summary</h3>
                            <p class="igny8-card-subtitle">Planning workflow progress tracking</p>
                        </div>
                        <div class="igny8-card-icon">
                            <span class="dashicons dashicons-chart-area igny8-dashboard-icon-lg igny8-dashboard-icon-blue"></span>
                        </div>
                    </div>
                </div>
                <div class="igny8-card-body">
                    
                    <!-- Keyword Mapping Progress -->
                    <div class="igny8-progress-item">
                        <div class="igny8-progress-header">
                            <span class="igny8-progress-label">Keyword Mapping</span>
                            <span class="igny8-progress-percent"><?php echo $keyword_mapping_pct; ?>%</span>
                        </div>
                        <div class="igny8-progress-bar">
                            <div class="igny8-progress-fill igny8-progress-<?php echo $keyword_color; ?>" style="width: <?php echo $keyword_mapping_pct; ?>%"></div>
                        </div>
                        <div class="igny8-progress-details"><?php echo $mapped_keywords; ?> of <?php echo $total_keywords; ?> keywords mapped</div>
                    </div>
                    
                    <!-- Clusters With Ideas Progress -->
                    <div class="igny8-progress-item">
                        <div class="igny8-progress-header">
                            <span class="igny8-progress-label">Clusters With Ideas</span>
                            <span class="igny8-progress-percent"><?php echo $clusters_ideas_pct; ?>%</span>
                        </div>
                        <div class="igny8-progress-bar">
                            <div class="igny8-progress-fill igny8-progress-<?php echo $cluster_color; ?>" style="width: <?php echo $clusters_ideas_pct; ?>%"></div>
                        </div>
                        <div class="igny8-progress-details"><?php echo $clusters_with_ideas; ?> of <?php echo $total_clusters; ?> clusters have ideas</div>
                    </div>
                    
                    <!-- Ideas Queued to Writer Progress -->
                    <div class="igny8-progress-item">
                        <div class="igny8-progress-header">
                            <span class="igny8-progress-label">Ideas Queued to Writer</span>
                            <span class="igny8-progress-percent"><?php echo $ideas_queued_pct; ?>%</span>
                        </div>
                        <div class="igny8-progress-bar">
                            <div class="igny8-progress-fill igny8-progress-<?php echo $idea_color; ?>" style="width: <?php echo $ideas_queued_pct; ?>%"></div>
                        </div>
                        <div class="igny8-progress-details"><?php echo $queued_ideas; ?> of <?php echo $total_ideas; ?> ideas queued</div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Top 5 Clusters by Volume -->
        <div class="igny8-dashboard-section">
            <div class="igny8-card">
                <div class="igny8-standard-header">
                    <div class="igny8-card-header-content">
                        <div class="igny8-card-title-text">
                            <h3>Top 5 Clusters by Volume</h3>
                            <p class="igny8-card-subtitle">Highest volume keyword clusters</p>
                        </div>
                        <div class="igny8-card-icon">
                            <span class="dashicons dashicons-chart-bar igny8-dashboard-icon-lg igny8-dashboard-icon-green"></span>
                        </div>
                    </div>
                </div>
                <div class="igny8-card-body">
                    <?php
                    // Get top 5 clusters by volume
                    global $wpdb;
                    $top_clusters = $wpdb->get_results("
                        SELECT 
                            c.cluster_name,
                            c.total_volume,
                            c.keyword_count
                        FROM {$wpdb->prefix}igny8_clusters c
                        WHERE c.status = 'active' AND c.total_volume > 0
                        ORDER BY c.total_volume DESC 
                        LIMIT 5
                    ");
                    
                    if ($top_clusters):
                        $max_volume = $top_clusters[0]->total_volume; // Highest volume for percentage calculation
                        $color_classes = ['igny8-progress-blue', 'igny8-progress-green', 'igny8-progress-amber', 'igny8-progress-purple', 'igny8-progress-text-dim'];
                    ?>
                    <div class="igny8-analytics-list">
                        <?php foreach ($top_clusters as $index => $cluster): 
                            $percentage = $max_volume > 0 ? round(($cluster->total_volume / $max_volume) * 100) : 0;
                            $color_class = $color_classes[$index % 5];
                        ?>
                        <div class="igny8-analytics-item">
                            <div class="igny8-item-info">
                                <div class="igny8-item-label"><?php echo esc_html($cluster->cluster_name); ?></div>
                                <div class="igny8-item-value"><?php echo number_format($cluster->total_volume); ?></div>
                            </div>
                            <div class="igny8-item-progress">
                                <div class="igny8-progress-track">
                                    <div class="igny8-progress-fill <?php echo $color_class; ?>" style="width: <?php echo $percentage; ?>%"></div>
                                </div>
                                <span class="igny8-progress-percent"><?php echo $percentage; ?>%</span>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <?php else: ?>
                    <div class="igny8-empty-analytics">
                        <span class="dashicons dashicons-info igny8-dashboard-icon-dim"></span>
                        <p>No clusters found yet</p>
                        <a href="<?php echo admin_url('admin.php?page=igny8-planner&sm=clusters'); ?>" class="igny8-btn igny8-btn-outline igny8-btn-sm">View Clusters</a>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <!-- Ideas by Status -->
        <div class="igny8-dashboard-section">
            <div class="igny8-card">
                <div class="igny8-standard-header">
                    <div class="igny8-card-header-content">
                        <div class="igny8-card-title-text">
                            <h3>Ideas by Status</h3>
                            <p class="igny8-card-subtitle">Content ideas workflow status</p>
                        </div>
                        <div class="igny8-card-icon">
                            <span class="dashicons dashicons-lightbulb igny8-dashboard-icon-lg igny8-dashboard-icon-amber"></span>
                        </div>
                    </div>
                </div>
                <div class="igny8-card-body">
                    <?php
                    // Get ideas by status
                    $ideas_by_status = $wpdb->get_results("
                        SELECT 
                            status,
                            COUNT(*) as count
                        FROM {$wpdb->prefix}igny8_content_ideas 
                        GROUP BY status 
                        ORDER BY count DESC
                    ");
                    
                    if ($ideas_by_status):
                        $total_ideas_status = array_sum(array_column($ideas_by_status, 'count'));
                        $status_colors = [
                            'new' => 'igny8-progress-blue',
                            'scheduled' => 'igny8-progress-amber', 
                            'published' => 'igny8-progress-green',
                            'draft' => 'igny8-progress-purple',
                            'completed' => 'igny8-progress-green'
                        ];
                    ?>
                    <div class="igny8-analytics-list">
                        <?php foreach ($ideas_by_status as $status): 
                            $percentage = $total_ideas_status > 0 ? round(($status->count / $total_ideas_status) * 100) : 0;
                            $color_class = $status_colors[$status->status] ?? 'igny8-progress-text-dim';
                            $status_display = ucfirst(str_replace('_', ' ', $status->status));
                        ?>
                        <div class="igny8-analytics-item">
                            <div class="igny8-item-info">
                                <div class="igny8-item-label"><?php echo esc_html($status_display); ?></div>
                                <div class="igny8-item-value"><?php echo number_format($status->count); ?></div>
                            </div>
                            <div class="igny8-item-progress">
                                <div class="igny8-progress-track">
                                    <div class="igny8-progress-fill <?php echo $color_class; ?>" style="width: <?php echo $percentage; ?>%"></div>
                                </div>
                                <span class="igny8-progress-percent"><?php echo $percentage; ?>%</span>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <?php else: ?>
                    <div class="igny8-empty-analytics">
                        <span class="dashicons dashicons-info igny8-dashboard-icon-dim"></span>
                        <p>No ideas found yet</p>
                        <a href="<?php echo admin_url('admin.php?page=igny8-planner&sm=ideas'); ?>" class="igny8-btn igny8-btn-outline igny8-btn-sm">View Ideas</a>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
       </div>
     <!-- Next Actions Panel -->
        <div class="igny8-dashboard-section">
            <div class="igny8-card">
                <div class="igny8-standard-header">
                    <div class="igny8-card-header-content">
                        <div class="igny8-card-title-text">
                            <h3>Next Actions</h3>
                            <p class="igny8-card-subtitle">Actionable items requiring attention</p>
                        </div>
                        <div class="igny8-card-icon">
                            <span class="dashicons dashicons-arrow-right-alt igny8-dashboard-icon-lg igny8-dashboard-icon-purple"></span>
                        </div>
                    </div>
                </div>
                <div class="igny8-card-body">
                    <div class="igny8-next-actions">
                        <?php
                        $unmapped_keywords = $kpi_data['unmapped_keywords'] ?? 0;
                        $clusters_without_ideas = $total_clusters - $clusters_with_ideas;
                        $ideas_not_queued = $total_ideas - $queued_ideas;
                        ?>
                        
                        <?php if ($unmapped_keywords > 0): ?>
                        <div class="igny8-action-item">
                            <span class="igny8-action-text"><?php echo $unmapped_keywords; ?> keywords unmapped</span>
                            <a href="<?php echo admin_url('admin.php?page=igny8-planner&sm=keywords&filter_status=unmapped'); ?>" class="igny8-btn igny8-btn-text">Map Keywords</a>
                        </div>
                        <?php endif; ?>
                        
                        <?php if ($clusters_without_ideas > 0): ?>
                        <div class="igny8-action-item">
                            <span class="igny8-action-text"><?php echo $clusters_without_ideas; ?> clusters without ideas</span>
                            <a href="<?php echo admin_url('admin.php?page=igny8-planner&sm=ideas'); ?>" class="igny8-btn igny8-btn-text">Generate Ideas</a>
                        </div>
                        <?php endif; ?>
                        
                        <?php if ($ideas_not_queued > 0): ?>
                        <div class="igny8-action-item">
                            <span class="igny8-action-text"><?php echo $ideas_not_queued; ?> ideas not queued to writer</span>
                            <a href="<?php echo admin_url('admin.php?page=igny8-planner&sm=ideas&filter_status=new'); ?>" class="igny8-btn igny8-btn-text">Queue to Writer</a>
                        </div>
                        <?php endif; ?>
                        
                        <div class="igny8-action-item">
                            <span class="igny8-action-text">Import new keywords to expand your strategy</span>
                            <a href="<?php echo admin_url('admin.php?page=igny8-import-export'); ?>" class="igny8-btn igny8-btn-text">Import Keywords</a>
                        </div>
                        
                        <?php if ($unmapped_keywords == 0 && $clusters_without_ideas == 0 && $ideas_not_queued == 0): ?>
                        <div class="igny8-action-item igny8-action-complete">
                            <span class="igny8-action-text">All planning tasks completed!</span>
                            <span class="igny8-action-status">✓ Ready for content creation</span>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
	

    <?php
    
    // Set submodule for subheader display - show keywords charts on home page
    $current_submodule = 'keywords';
    
    
    // Home page - add sector selection functionality
    // Only load CRON key if this page needs CRON functionality
    $cron_key = igny8_needs_cron_functionality() ? igny8_get_or_generate_cron_key() : null;
    
    // Debug logging (remove in production)
    // if (defined('WP_DEBUG') && WP_DEBUG) {
    //     error_log('Igny8 Planner: cronKey being passed to JS: ' . $cron_key);
    // }
    
    wp_localize_script('igny8-admin-js', 'IGNY8_PAGE', [
        'module' => 'planner',
        'submodule' => 'home',
        'ajaxUrl' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('igny8_planner_settings'),
        'cronKey' => $cron_key
    ]);
        break;
}

// Capture page content
$igny8_page_content = ob_get_clean();

// Include global layout
include_once plugin_dir_path(__FILE__) . '../../core/global-layout.php';
