<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : actions-tpl.php
 * @location     : /modules/components/actions-tpl.php
 * @type         : Component
 * @scope        : Cross-Module
 * @allowed      : Action rendering, bulk operations
 * @reusability  : Shared
 * @notes        : Dynamic action component for all modules
 */

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

// Render table actions function
function igny8_render_table_actions($table_id, $actions = []) {
    
    // Set default actions if none provided
    $actions = $actions ?: ['export_selected', 'delete_selected', 'export_all', 'import', 'add_new'];
    
    // Check if AI mode is enabled
    $ai_mode = igny8_get_ai_setting('planner_mode', 'manual') === 'ai';
    
    // Start output buffering to capture HTML
    ob_start();
    ?>
<!-- Table Actions -->
<div class="igny8-table-actions" data-table="<?php echo esc_attr($table_id); ?>">
    <div class="left-actions">
        <span id="<?php echo esc_attr($table_id); ?>_count" class="igny8-count-hidden">0 selected</span>
        <?php if (in_array('export_selected', $actions)): ?>
            <button id="<?php echo esc_attr($table_id); ?>_export_btn" class="igny8-btn igny8-btn-success" disabled>Export Selected</button>
        <?php endif; ?>
        <?php if (in_array('delete_selected', $actions)): ?>
            <button id="<?php echo esc_attr($table_id); ?>_delete_btn" class="igny8-btn igny8-btn-danger" disabled>Delete Selected</button>
        <?php endif; ?>
        
        <!-- Publish Selected Button (for Drafts) -->
        <?php if ($table_id === 'writer_drafts'): ?>
            <button id="<?php echo esc_attr($table_id); ?>_generate_images_btn" class="igny8-btn igny8-btn-accent" disabled>
                <span class="dashicons dashicons-format-image"></span> Generate Images
            </button>
            <button id="<?php echo esc_attr($table_id); ?>_publish_btn" class="igny8-btn igny8-btn-primary" disabled>
                <span class="dashicons dashicons-yes-alt"></span> Publish Selected
            </button>
        <?php endif; ?>
        
        <!-- AI Action Buttons (only visible in AI mode) -->
        <?php if ($ai_mode): ?>
            <?php if ($table_id === 'planner_keywords' && igny8_get_ai_setting('clustering', 'enabled') === 'enabled'): ?>
                        <button id="<?php echo esc_attr($table_id); ?>_ai_cluster_btn" class="igny8-btn igny8-btn-accent" disabled>
                            <span class="dashicons dashicons-admin-generic"></span> Auto Cluster
                        </button>
            <?php endif; ?>
            
            <?php if ($table_id === 'planner_clusters' && igny8_get_ai_setting('ideas', 'enabled') === 'enabled'): ?>
                <button id="<?php echo esc_attr($table_id); ?>_ai_ideas_btn" class="igny8-btn igny8-btn-accent" disabled>
                    <span class="dashicons dashicons-lightbulb"></span> Generate Ideas
                </button>
            <?php endif; ?>
            
        <?php endif; ?>
        
        <!-- Queue to Writer Button (for Ideas) -->
        <?php if ($table_id === 'planner_ideas'): ?>
            <button id="<?php echo esc_attr($table_id); ?>_queue_writer_btn" class="igny8-btn igny8-btn-primary" disabled>
                <span class="dashicons dashicons-edit"></span> Queue to Writer
            </button>
        <?php endif; ?>
        
        <?php if ($table_id === 'writer_tasks' && igny8_get_ai_setting('writer_mode', 'manual') === 'ai' && igny8_get_ai_setting('content_generation', 'enabled') === 'enabled'): ?>
            <button id="<?php echo esc_attr($table_id); ?>_generate_content_btn" class="igny8-btn igny8-btn-success" disabled>
                <span class="dashicons dashicons-admin-generic"></span> Generate Content
            </button>
        <?php endif; ?>
    </div>
    <div class="right-actions">
        <div class="igny8-ml-auto igny8-flex igny8-flex-gap-10 igny8-align-center">
            <?php if (in_array('export_all', $actions)): ?>
                <button id="<?php echo esc_attr($table_id); ?>_export_all_btn" class="igny8-btn igny8-btn-outline" onclick="igny8ShowExportModal('<?php echo esc_attr($table_id); ?>')">Export All</button>
            <?php endif; ?>
            <?php if (in_array('import', $actions)): ?>
                <button id="<?php echo esc_attr($table_id); ?>_import_btn" class="igny8-btn igny8-btn-secondary">Import</button>
            <?php endif; ?>
            <?php if (in_array('add_new', $actions)): ?>
                <button 
                    class="igny8-btn igny8-btn-primary"
                    data-action="addRow"
                    data-table-id="<?php echo esc_attr($table_id); ?>"
                >
                    Add New
                </button>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Global notification system is handled by core.js -->
<?php
    return ob_get_clean();
}

// Set default values
$table_id = $table_id ?? 'data_table';
$actions = $actions ?? ['export_selected', 'delete_selected', 'export_all', 'import', 'add_new'];
?>
