<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : writer.php
 * @location     : /modules/writer/writer.php
 * @type         : Admin Page
 * @scope        : Module Only
 * @allowed      : Writer module logic, subpage routing, step data functions
 * @reusability  : Single Use
 * @notes        : Main writer 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 Writer workflow guide
 * 
 * @return array Array of step data with status and counts
 */
function igny8_get_writer_step_data() {
    global $wpdb;
    
    // Get counts for each step
    $queued_tasks = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_tasks WHERE status IN ('pending', 'queued', 'new')");
    $draft_tasks = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_tasks WHERE status IN ('draft', 'in_progress', 'review')");
    $published_tasks = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_tasks WHERE status = 'completed'");
    $total_tasks = $queued_tasks + $draft_tasks + $published_tasks;
    
    return [
        'view_tasks' => [
            'queued' => $queued_tasks,
            'total' => $total_tasks,
            'status' => $total_tasks > 0 ? 'completed' : 'pending'
        ],
        'generate_drafts' => [
            'queued' => $queued_tasks,
            'status' => $queued_tasks > 0 ? 'current' : ($draft_tasks > 0 ? 'completed' : 'pending')
        ],
        'review_drafts' => [
            'drafts' => $draft_tasks,
            'status' => $draft_tasks > 0 ? 'current' : ($published_tasks > 0 ? 'completed' : 'pending')
        ],
        'publish' => [
            'published' => $published_tasks,
            'drafts' => $draft_tasks,
            'status' => $published_tasks > 0 ? 'completed' : ($draft_tasks > 0 ? 'current' : 'pending')
        ]
    ];
}

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

// Start output buffering
ob_start();

switch ($subpage) {
    case 'tasks':
        include plugin_dir_path(__FILE__) . 'tasks.php';
        break;
    case 'drafts':
        include plugin_dir_path(__FILE__) . 'drafts.php';
        break;
    case 'published':
        include plugin_dir_path(__FILE__) . 'published.php';
        break;
    case 'home':
    default:
        // Home dashboard content
        // 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('writer_home', $kpi_config['writer_home'] ?? []);
        
        // Calculate dashboard metrics - Updated queries for correct status
        global $wpdb;
        
        // Queued tasks - tasks that are pending/not started
        $queued_tasks = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_tasks WHERE status IN ('pending', 'queued', 'new')");
        
        // Draft tasks - content generated and saved as draft, awaiting review/publish
        $draft_tasks = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_tasks WHERE status IN ('draft', 'in_progress', 'review')");
        
        // Published tasks - status is 'completed' in tasks table
        $published_tasks = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_tasks WHERE status = 'completed'");
        
        $total_tasks = $queued_tasks + $draft_tasks + $published_tasks;
        
        // Calculate percentages for progress bars
        $queued_pct = $total_tasks > 0 ? round(($queued_tasks / $total_tasks) * 100) : 0;
        $draft_pct = $total_tasks > 0 ? round(($draft_tasks / $total_tasks) * 100) : 0;
        $published_pct = $total_tasks > 0 ? round(($published_tasks / $total_tasks) * 100) : 0;
        
        // Home page content
        ?>
    <div class="igny8-module-home">
        <!-- Writing Status Cards -->
        <div class="igny8-dashboard-section">
            <div class="igny8-grid-3 igny8-status-cards">
                <!-- Queued Tasks 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-writer&sm=tasks'); ?>'">
                    <div class="igny8-card-body">
                        <div class="igny8-status-metric">
                            <span class="igny8-status-count"><?php echo number_format($queued_tasks); ?></span>
                            <span class="igny8-status-label">Tasks Queued</span>
                            <span class="igny8-status-desc">awaiting writing start</span>
                        </div>
                        <div class="igny8-status-icon">
                            <span class="dashicons dashicons-clipboard igny8-dashboard-icon-sm"></span>
                        </div>
        </div>
                </div>
                
                <!-- Drafts 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-writer&sm=drafts'); ?>'">
                <div class="igny8-card-body">
                        <div class="igny8-status-metric">
                            <span class="igny8-status-count"><?php echo number_format($draft_tasks); ?></span>
                            <span class="igny8-status-label">Drafts</span>
                            <span class="igny8-status-desc">currently being written</span>
                        </div>
                        <div class="igny8-status-icon">
                            <span class="dashicons dashicons-edit-page igny8-dashboard-icon-sm"></span>
                </div>
            </div>
                </div>
                
                <!-- Published 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-writer&sm=published'); ?>'">
                <div class="igny8-card-body">
                        <div class="igny8-status-metric">
                            <span class="igny8-status-count"><?php echo number_format($published_tasks); ?></span>
                            <span class="igny8-status-label">Published</span>
                            <span class="igny8-status-desc">live on your site</span>
                        </div>
                        <div class="igny8-status-icon">
                            <span class="dashicons dashicons-yes-alt igny8-dashboard-icon-sm"></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Workflow Status & AI Settings -->


        <!-- Writer 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>Writer Workflow Steps</h3>
                        <p class="igny8-card-subtitle">Track your content creation progress</p>
                    </div>
                    <div class="igny8-card-icon">
                        <span class="dashicons dashicons-edit-page igny8-dashboard-icon-lg igny8-dashboard-icon-blue"></span>
                    </div>
                </div>
            </div>
            
            <div class="igny8-grid igny8-grid-4">
                        <?php
                        // Step-by-Step UX Guide
                        $step_data = igny8_get_writer_step_data();
                        ?>
                        <div class="igny8-card igny8-step-card <?php echo $step_data['view_tasks']['status']; ?>">
                            <div class="igny8-card-body">
                                <div class="igny8-step-header">
                                    <div class="igny8-step-number">1</div>
                                    <div class="igny8-step-title">View Queued Tasks</div>
                                </div>
                                <div class="igny8-step-status">
                                    <span class="igny8-step-status-icon"><?php echo $step_data['view_tasks']['status'] === 'completed' ? '✅' : '⏳'; ?></span>
                                    <span class="igny8-step-status-text"><?php echo $step_data['view_tasks']['status'] === 'completed' ? 'Available' : 'No Tasks'; ?></span>
                                </div>
                                <div class="igny8-step-data">
                                    <?php if ($step_data['view_tasks']['total'] > 0): ?>
                                        <?php echo $step_data['view_tasks']['queued']; ?> tasks queued
                                    <?php else: ?>
                                        No tasks available
                                    <?php endif; ?>
                                </div>
                                <?php if ($step_data['view_tasks']['status'] === 'completed'): ?>
                                    <div class="igny8-step-action">
                                        <a href="?page=igny8-writer&sm=tasks" class="igny8-btn igny8-btn-primary igny8-btn-sm">View Tasks</a>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                        
                        <div class="igny8-card igny8-step-card <?php echo $step_data['generate_drafts']['status']; ?>">
                            <div class="igny8-card-body">
                                <div class="igny8-step-header">
                                    <div class="igny8-step-number">2</div>
                                    <div class="igny8-step-title">Generate Drafts (AI)</div>
                                </div>
                                <div class="igny8-step-status">
                                    <span class="igny8-step-status-icon"><?php echo $step_data['generate_drafts']['status'] === 'completed' ? '✅' : ($step_data['generate_drafts']['status'] === 'current' ? '⏳' : '⏳'); ?></span>
                                    <span class="igny8-step-status-text"><?php echo $step_data['generate_drafts']['status'] === 'completed' ? 'Completed' : ($step_data['generate_drafts']['status'] === 'current' ? 'Ready' : 'Pending'); ?></span>
                                </div>
                                <div class="igny8-step-data">
                                    <?php if ($step_data['generate_drafts']['queued'] > 0): ?>
                                        <?php echo $step_data['generate_drafts']['queued']; ?> tasks ready for AI
                                    <?php elseif ($step_data['view_tasks']['total'] > 0): ?>
                                        All tasks processed
                                    <?php else: ?>
                                        No tasks to process
                                    <?php endif; ?>
                                </div>
                                <?php if ($step_data['generate_drafts']['status'] === 'current'): ?>
                                    <div class="igny8-step-action">
                                        <a href="?page=igny8-writer&sm=tasks" class="igny8-btn igny8-btn-primary igny8-btn-sm">Generate Now</a>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                        
                        <div class="igny8-card igny8-step-card <?php echo $step_data['review_drafts']['status']; ?>">
                            <div class="igny8-card-body">
                                <div class="igny8-step-header">
                                    <div class="igny8-step-number">3</div>
                                    <div class="igny8-step-title">Review Drafts</div>
                                </div>
                                <div class="igny8-step-status">
                                    <span class="igny8-step-status-icon"><?php echo $step_data['review_drafts']['status'] === 'completed' ? '✅' : ($step_data['review_drafts']['status'] === 'current' ? '⏳' : '⏳'); ?></span>
                                    <span class="igny8-step-status-text"><?php echo $step_data['review_drafts']['status'] === 'completed' ? 'Completed' : ($step_data['review_drafts']['status'] === 'current' ? 'Ready' : 'Pending'); ?></span>
                                </div>
                                <div class="igny8-step-data">
                                    <?php if ($step_data['review_drafts']['drafts'] > 0): ?>
                                        <?php echo $step_data['review_drafts']['drafts']; ?> drafts to review
                                    <?php elseif ($step_data['view_tasks']['total'] > 0): ?>
                                        All drafts reviewed
                                    <?php else: ?>
                                        No drafts to review
                                    <?php endif; ?>
                                </div>
                                <?php if ($step_data['review_drafts']['status'] === 'current'): ?>
                                    <div class="igny8-step-action">
                                        <a href="?page=igny8-writer&sm=drafts" class="igny8-btn igny8-btn-primary igny8-btn-sm">Review Now</a>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                        
                        <div class="igny8-card igny8-step-card <?php echo $step_data['publish']['status']; ?>">
                            <div class="igny8-card-body">
                                <div class="igny8-step-header">
                                    <div class="igny8-step-number">4</div>
                                    <div class="igny8-step-title">Publish Content</div>
                                </div>
                                <div class="igny8-step-status">
                                    <span class="igny8-step-status-icon"><?php echo $step_data['publish']['status'] === 'completed' ? '✅' : ($step_data['publish']['status'] === 'current' ? '⏳' : '⏳'); ?></span>
                                    <span class="igny8-step-status-text"><?php echo $step_data['publish']['status'] === 'completed' ? 'Completed' : ($step_data['publish']['status'] === 'current' ? 'Ready' : 'Pending'); ?></span>
                                </div>
                                <div class="igny8-step-data">
                                    <?php if ($step_data['publish']['drafts'] > 0): ?>
                                        <?php echo $step_data['publish']['drafts']; ?> drafts ready to publish
                                    <?php elseif ($step_data['publish']['published'] > 0): ?>
                                        <?php echo $step_data['publish']['published']; ?> content published
                                    <?php else: ?>
                                        No content to publish
                                    <?php endif; ?>
                                </div>
                                <?php if ($step_data['publish']['status'] === 'current'): ?>
                                    <div class="igny8-step-action">
                                        <a href="?page=igny8-writer&sm=drafts" class="igny8-btn igny8-btn-primary igny8-btn-sm">Publish Now</a>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>

                                        </div>








<div class="igny8-flex-row">

        <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>
                   
                   
                    
                    <form id="igny8-ai-integration-form">
                    <div class="igny8-flex-row">
                                    <div class="igny8-form-group">
                                    <div class="igny8-mode-toggle-row">
                                        <div class="igny8-mode-toggle-label">
                                            <span class="igny8-mode-label">Manual</span>
                                            <label class="igny8-toggle">
                                                <input type="checkbox" name="igny8_writer_mode" value="ai" <?php checked(igny8_get_ai_setting('writer_mode', 'manual'), 'ai'); ?>>
                                                <span class="igny8-toggle-slider"></span>
                                            </label>
                                            <span class="igny8-mode-label">AI Mode</span>
                                        </div>
                                    </div>
                                </div>
                                <div class="igny8-form-actions">
                                    <button type="button" id="igny8-save-writer-ai-settings" class="igny8-btn igny8-btn-success">Save AI Integration Settings</button>
                                </div>
                                </div>
                            </form>
                    
                </div>
            </div>
          </div>
            


          <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>


          
                     
                           

                            <form id="igny8-new-content-form">
                            <div class="igny8-flex-row">
                                <div class="igny8-form-group">
                                    <div class="igny8-radio-group">
                                        <label class="igny8-radio-label">
                                            <input type="radio" name="new_content_action" value="draft" <?php checked(get_option('igny8_new_content_action', 'draft'), 'draft'); ?>>
                                            <span class="igny8-radio-text">Save as Draft</span>
                                        </label>
                                        <label class="igny8-radio-label">
                                            <input type="radio" name="new_content_action" value="publish" <?php checked(get_option('igny8_new_content_action', 'draft'), 'publish'); ?>>
                                            <span class="igny8-radio-text">Publish Directly</span>
                                        </label>
                                    </div>
                                </div>
                                <div class="igny8-form-actions">
                                    <button type="button" id="igny8-save-content-decision" class="igny8-btn igny8-btn-success">Save Content Decision</button>
                                </div>
                                </div>
                            </form>


                
                            </div>
            </div>
          </div>
                
                                </div>     
            
       
    <div class="igny8-grid-3 igny8-equal-height">
        <!-- Content Published by Type -->
        <div class="igny8-dashboard-section igny8-analytics-card">
            <div class="igny8-card">
                <div class="igny8-card-header igny8-standard-header">
                    <div class="igny8-card-header-content">
                        <div class="igny8-card-title-text">
                            <h3>Content Published by Type</h3>
                            <p class="igny8-card-subtitle igny8-centered">Content distribution analysis</p>
                        </div>
                        <div class="igny8-card-icon">
                            <span class="dashicons dashicons-category igny8-dashboard-icon-lg igny8-dashboard-icon-blue"></span>
                        </div>
                    </div>
                    <div class="igny8-card-metric">
                        <?php
                        global $wpdb;
                        $total_published = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_tasks WHERE status = 'completed'");
                        echo '<span class="igny8-metric-value">' . number_format($total_published) . '</span>';
                        echo '<span class="igny8-metric-label">Total Published</span>';
                        ?>
                    </div>
                </div>
                <div class="igny8-card-body">
                    <?php
                    $content_by_type = $wpdb->get_results("
                        SELECT content_type, COUNT(*) as count 
                        FROM {$wpdb->prefix}igny8_tasks 
                        WHERE status = 'completed' 
                        GROUP BY content_type 
                        ORDER BY count DESC
                    ");
                    
                    if ($content_by_type):
                    ?>
                    <div class="igny8-analytics-list">
                        <?php 
                        $color_classes = ['igny8-progress-blue', 'igny8-progress-green', 'igny8-progress-amber', 'igny8-progress-purple'];
                        $color_index = 0;
                        foreach ($content_by_type as $type): 
                            $type_display = ucfirst(str_replace('_', ' ', $type->content_type));
                            $percentage = $total_published > 0 ? round(($type->count / $total_published) * 100) : 0;
                            $color_class = $color_classes[$color_index % 4];
                            $color_index++;
                        ?>
                        <div class="igny8-analytics-item">
                            <div class="igny8-item-info">
                                <div class="igny8-item-label"><?php echo esc_html($type_display); ?></div>
                                <div class="igny8-item-value"><?php echo number_format($type->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 published content found yet</p>
                        <a href="<?php echo admin_url('admin.php?page=igny8-writer&sm=tasks'); ?>" class="igny8-btn igny8-btn-outline igny8-btn-sm">View Tasks</a>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <!-- Publishing Activity -->
        <div class="igny8-dashboard-section igny8-analytics-card">
            <div class="igny8-card">
                <div class="igny8-card-header igny8-standard-header">
                    <div class="igny8-card-header-content">
                        <div class="igny8-card-title-text">
                            <h3>Publishing Activity</h3>
                            <p class="igny8-card-subtitle igny8-centered">Content publishing timeline</p>
                        </div>
                        <div class="igny8-card-icon">
                            <span class="dashicons dashicons-chart-line igny8-dashboard-icon-lg igny8-dashboard-icon-green"></span>
                        </div>
                    </div>
                    <div class="igny8-card-metric">
                        <?php
                        $recent_7_days = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_tasks WHERE status = 'completed' AND updated_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)");
                        echo '<span class="igny8-metric-value">' . number_format($recent_7_days) . '</span>';
                        echo '<span class="igny8-metric-label">This Week</span>';
                        ?>
                    </div>
                </div>
                <div class="igny8-card-body">
                    <?php
                    $yesterday_count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_tasks WHERE status = 'completed' AND DATE(updated_at) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)");
                    $last_7_days = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_tasks WHERE status = 'completed' AND updated_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)");
                    $last_20_days = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_tasks WHERE status = 'completed' AND updated_at >= DATE_SUB(NOW(), INTERVAL 20 DAY)");
                    $all_time = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}igny8_tasks WHERE status = 'completed'");
                    ?>
                    <div class="igny8-analytics-list">
                        <div class="igny8-analytics-item">
                            <div class="igny8-item-info">
                                <div class="igny8-item-label">Yesterday</div>
                                <div class="igny8-item-value"><?php echo number_format($yesterday_count); ?></div>
                            </div>
                            <div class="igny8-item-progress">
                                <div class="igny8-progress-track">
                                    <div class="igny8-progress-fill igny8-progress-blue" style="width: <?php echo $all_time > 0 ? round(($yesterday_count / $all_time) * 100) : 0; ?>%"></div>
                                </div>
                                <span class="igny8-progress-percent"><?php echo $all_time > 0 ? round(($yesterday_count / $all_time) * 100) : 0; ?>%</span>
                            </div>
                        </div>
                        
                        <div class="igny8-analytics-item">
                            <div class="igny8-item-info">
                                <div class="igny8-item-label">Last 7 Days</div>
                                <div class="igny8-item-value"><?php echo number_format($last_7_days); ?></div>
                            </div>
                            <div class="igny8-item-progress">
                                <div class="igny8-progress-track">
                                    <div class="igny8-progress-fill igny8-progress-green" style="width: <?php echo $all_time > 0 ? round(($last_7_days / $all_time) * 100) : 0; ?>%"></div>
                                </div>
                                <span class="igny8-progress-percent"><?php echo $all_time > 0 ? round(($last_7_days / $all_time) * 100) : 0; ?>%</span>
                            </div>
                        </div>
                        
                        <div class="igny8-analytics-item">
                            <div class="igny8-item-info">
                                <div class="igny8-item-label">Last 20 Days</div>
                                <div class="igny8-item-value"><?php echo number_format($last_20_days); ?></div>
                            </div>
                            <div class="igny8-item-progress">
                                <div class="igny8-progress-track">
                                    <div class="igny8-progress-fill igny8-progress-amber" style="width: <?php echo $all_time > 0 ? round(($last_20_days / $all_time) * 100) : 0; ?>%"></div>
                                </div>
                                <span class="igny8-progress-percent"><?php echo $all_time > 0 ? round(($last_20_days / $all_time) * 100) : 0; ?>%</span>
                            </div>
                    </div>
                    
                        <div class="igny8-analytics-item igny8-analytics-total">
                            <div class="igny8-item-info">
                                <div class="igny8-item-label">All Time</div>
                                <div class="igny8-item-value"><?php echo number_format($all_time); ?></div>
                            </div>
                            <div class="igny8-item-progress">
                                <div class="igny8-progress-track">
                                    <div class="igny8-progress-fill igny8-progress-purple" style="width: 100%"></div>
                                </div>
                                <span class="igny8-progress-percent">100%</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Content Workflow Summary -->
   <div class="igny8-dashboard-section">
       <div class="igny8-card">
           <div class="igny8-card-header igny8-standard-header">
               <div class="igny8-card-header-content">
                   <div class="igny8-card-title-text">
                       <h3>Content Workflow Summary</h3>
                       <p class="igny8-card-subtitle igny8-centered">Content distribution analysis</p>
                   </div>
                   <div class="igny8-card-icon">
                       <span class="dashicons dashicons-chart-line igny8-dashboard-icon-lg igny8-dashboard-icon-blue"></span>
                   </div>
               </div>
           </div>
           <div class="igny8-card-body">
               <div class="igny8-progress-item">
                   <div class="igny8-progress-header">
                       <span class="igny8-progress-label">Queued Tasks</span>
                       <span class="igny8-progress-percent"><?php echo $queued_pct; ?>%</span>
                   </div>
                   <div class="igny8-progress-bar">
                       <div class="igny8-progress-fill igny8-progress-blue" style="width: <?php echo $queued_pct; ?>%"></div>
                   </div>
                   <div class="igny8-progress-details"><?php echo number_format($queued_tasks); ?> tasks waiting to start</div>
               </div>
               
               <div class="igny8-progress-item">
                   <div class="igny8-progress-header">
                       <span class="igny8-progress-label">Draft / Review</span>
                       <span class="igny8-progress-percent"><?php echo $draft_pct; ?>%</span>
                   </div>
                   <div class="igny8-progress-bar">
                       <div class="igny8-progress-fill igny8-progress-amber" style="width: <?php echo $draft_pct; ?>%"></div>
                   </div>
                   <div class="igny8-progress-details"><?php echo number_format($draft_tasks); ?> drafts awaiting review and publish</div>
               </div>
               
               <div class="igny8-progress-item">
                   <div class="igny8-progress-header">
                       <span class="igny8-progress-label">Published</span>
                       <span class="igny8-progress-percent"><?php echo $published_pct; ?>%</span>
                   </div>
                   <div class="igny8-progress-bar">
                       <div class="igny8-progress-fill igny8-progress-green" style="width: <?php echo $published_pct; ?>%"></div>
                   </div>
                   <div class="igny8-progress-details"><?php echo number_format($published_tasks); ?> tasks completed and published</div>
               </div>
           </div>
           </div>
       </div>
       

    </div>
    
    <div class="igny8-grid-2">
       
       <!-- AI Integration Settings Card -->
      

   <!-- Pending Actions Panel -->
   <div class="igny8-dashboard-section">
       <div class="igny8-card">
           <div class="igny8-card-header igny8-standard-header">
               <div class="igny8-card-header-content">
                   <div class="igny8-card-title-text">
                       <h3>Pending Actions</h3>
                       <p class="igny8-card-subtitle igny8-centered">Action items requiring attention</p>
                   </div>
                   <div class="igny8-card-icon">
                       <span class="dashicons dashicons-clock igny8-dashboard-icon-lg igny8-dashboard-icon-amber"></span>
                   </div>
               </div>
           </div>
           <div class="igny8-card-body">
               <div class="igny8-next-actions">
                   <?php if ($queued_tasks > 0): ?>
                   <div class="igny8-action-item">
                       <span class="igny8-action-text"><?php echo number_format($queued_tasks); ?> tasks not yet started</span>
                       <a href="<?php echo admin_url('admin.php?page=igny8-writer&sm=tasks'); ?>" class="igny8-btn igny8-btn-text">Start Writing</a>
                   </div>
                   <?php endif; ?>
                   
                   <?php if ($draft_tasks > 0): ?>
                   <div class="igny8-action-item">
                       <span class="igny8-action-text"><?php echo number_format($draft_tasks); ?> drafts awaiting review and publish</span>
                       <a href="<?php echo admin_url('admin.php?page=igny8-writer&sm=drafts'); ?>" class="igny8-btn igny8-btn-text">Review Drafts</a>
                   </div>
                   <?php endif; ?>
                   
                   <?php if ($published_tasks > 0): ?>
                   <div class="igny8-action-item">
                       <span class="igny8-action-text"><?php echo number_format($published_tasks); ?> posts ready to publish</span>
                       <a href="<?php echo admin_url('admin.php?page=igny8-writer&sm=published'); ?>" class="igny8-btn igny8-btn-text">View Published</a>
                   </div>
                   <?php endif; ?>
                   
                   <?php if ($total_tasks === 0): ?>
                   <div class="igny8-action-item igny8-action-complete">
                       <span class="igny8-action-text">All caught up! Consider adding new content ideas.</span>
                       <a href="<?php echo admin_url('admin.php?page=igny8-planner'); ?>" class="igny8-btn igny8-btn-text">Plan New Content</a>
                   </div>
                   <?php endif; ?>
               </div>
           </div>
       </div>
   </div>
   
</div>
   
    
    <?php
    
    // Home page - add dashboard 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 Writer: cronKey being passed to JS: ' . $cron_key);
    // }
    
    wp_localize_script('igny8-admin-js', 'IGNY8_PAGE', [
        'module' => 'writer',
        'submodule' => 'home',
        'ajaxUrl' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('igny8_writer_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';
