<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : prompts.php
 * @location     : /modules/thinker/prompts.php
 * @type         : Admin Page
 * @scope        : Module Only
 * @allowed      : AI prompt management, prompt editing, AI configuration
 * @reusability  : Single Use
 * @notes        : AI prompt management interface for thinker module
 */

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

    // Prompts subpage - show AI prompts management
    ?>
    <div class="igny8-module-page">
        <div class="igny8-container">
            <!-- Page title removed - titles should not appear inside page content -->
            
            <!-- Planner Prompts Section -->
            <div class="igny8-dashboard-section">
                <div class="igny8-standard-header">
                    <div class="igny8-card-header-content">
                        <div class="igny8-card-title-text">
                            <h3>Planner Prompts</h3>
                            <p class="igny8-card-subtitle">Configure AI prompt templates for clustering and idea generation</p>
                        </div>
                        <div class="igny8-card-icon">
                            <span class="dashicons dashicons-edit igny8-dashboard-icon-lg igny8-dashboard-icon-amber"></span>
                        </div>
                    </div>
                </div>
                
                <?php if (igny8_get_ai_setting('planner_mode', 'manual') === 'ai'): ?>
                <!-- Clustering Prompt Card -->
                <div class="igny8-card">
                    <div class="igny8-standard-header">
                        <div class="igny8-card-header-content">
                            <div class="igny8-card-title-text">
                                <h4>Clustering Prompt</h4>
                                <p class="igny8-card-subtitle">Group keywords into topic clusters</p>
                            </div>
                            <div class="igny8-card-icon">
                                <span class="dashicons dashicons-networking igny8-dashboard-icon-lg igny8-dashboard-icon-green"></span>
                            </div>
                        </div>
                    </div>
                    <div class="igny8-card-body" style="text-align: center;">
                        <p class="description">This prompt is used to group keywords into topic clusters. Use [IGNY8_KEYWORDS] to inject keyword data.</p>
                        <textarea name="igny8_clustering_prompt" rows="8" class="igny8-textarea-green" style="width: 90%; margin: 0 auto; font-family: monospace; font-size: 14px; min-height: 300px; display: block;"><?php echo esc_textarea(wp_unslash(igny8_get_ai_setting('clustering_prompt', igny8_get_default_clustering_prompt()))); ?></textarea>
                        <div class="igny8-form-actions" style="margin-top: 15px;">
                            <button type="button" id="igny8-save-clustering-prompt" class="igny8-btn igny8-btn-success">Save Clustering Prompt</button>
                            <button type="button" id="igny8-reset-clustering-prompt" class="igny8-btn igny8-btn-outline">Reset to Default</button>
                        </div>
                    </div>
                </div>
                
                <!-- Ideas Generation Prompt Card -->
                <div class="igny8-card">
                    <div class="igny8-standard-header">
                        <div class="igny8-card-header-content">
                            <div class="igny8-card-title-text">
                                <h4>Ideas Generation Prompt</h4>
                                <p class="igny8-card-subtitle">Generate content ideas from clusters</p>
                            </div>
                            <div class="igny8-card-icon">
                                <span class="dashicons dashicons-lightbulb igny8-dashboard-icon-lg igny8-dashboard-icon-orange"></span>
                            </div>
                        </div>
                    </div>
                    <div class="igny8-card-body" style="text-align: center;">
                        <p class="description">This prompt generates content ideas from clusters. Use [IGNY8_CLUSTERS] to inject cluster data.</p>
                        <textarea name="igny8_ideas_prompt" rows="8" class="igny8-textarea-orange" style="width: 90%; margin: 0 auto; font-family: monospace; font-size: 14px; min-height: 300px; display: block;"><?php echo esc_textarea(wp_unslash(igny8_get_ai_setting('ideas_prompt', igny8_get_default_ideas_prompt()))); ?></textarea>
                        <div class="igny8-form-actions" style="margin-top: 15px;">
                            <button type="button" id="igny8-save-ideas-prompt" class="igny8-btn igny8-btn-success">Save Ideas Prompt</button>
                            <button type="button" id="igny8-reset-ideas-prompt" class="igny8-btn igny8-btn-outline">Reset to Default</button>
                        </div>
                    </div>
                </div>
                <?php else: ?>
                <div class="igny8-card">
                    <div class="igny8-card-body">
                        <div class="igny8-info-box">
                            <p><strong>Note:</strong> Planner prompts are only available when AI-Powered SEO Mode is enabled in the Planner module.</p>
                            <a href="<?php echo admin_url('admin.php?page=igny8-planner'); ?>" class="igny8-btn igny8-btn-primary">Go to Planner Settings</a>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
            </div>
            
            <!-- Writer Prompts Section -->
            <div class="igny8-dashboard-section">
                <div class="igny8-standard-header">
                    <div class="igny8-card-header-content">
                        <div class="igny8-card-title-text">
                            <h3>Writer Prompts</h3>
                            <p class="igny8-card-subtitle">Configure AI prompt templates for content writing</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>
                
                <?php if (igny8_get_ai_setting('writer_mode', 'manual') === 'ai'): ?>
                <!-- Content Generation Prompt Card -->
                <div class="igny8-card">
                    <div class="igny8-standard-header">
                        <div class="igny8-card-header-content">
                            <div class="igny8-card-title-text">
                                <h4>Content Generation Prompt</h4>
                                <p class="igny8-card-subtitle">Generate content from ideas</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-card-body" style="text-align: center;">
                        <!-- Content Generation Prompt -->
                        <p class="description">This prompt is used to generate content from ideas. Use [IGNY8_IDEA] to inject idea data, [IGNY8_CLUSTER] for cluster data, and [IGNY8_KEYWORDS] for keywords.</p>
                        <textarea name="igny8_content_generation_prompt" rows="8" class="igny8-textarea-blue" style="width: 90%; margin: 0 auto; font-family: monospace; font-size: 14px; min-height: 300px; display: block;"><?php echo esc_textarea(wp_unslash(igny8_get_ai_setting('content_generation_prompt', igny8_content_generation_prompt()))); ?></textarea>
                        <div class="igny8-form-actions" style="margin-top: 15px;">
                            <button type="button" id="igny8-save-content-generation-prompt" class="igny8-btn igny8-btn-success">Save Content Generation Prompt</button>
                            <button type="button" id="igny8-reset-content-generation-prompt" class="igny8-btn igny8-btn-outline">Reset to Default</button>
                        </div>
                    </div>
                </div>
                <?php else: ?>
                <div class="igny8-card">
                    <div class="igny8-card-body">
                        <div class="igny8-info-box">
                            <p><strong>Note:</strong> Writer prompts are only available when AI-Powered Mode is enabled in the Writer module.</p>
                            <a href="<?php echo admin_url('admin.php?page=igny8-writer'); ?>" class="igny8-btn igny8-btn-primary">Go to Writer Settings</a>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
            </div>
            
            <!-- Image Generation Section -->
            <div class="igny8-dashboard-section">
                <div class="igny8-standard-header">
                    <div class="igny8-card-header-content">
                        <div class="igny8-card-title-text">
                            <h3>Image Generation</h3>
                            <p class="igny8-card-subtitle">Test and configure AI image generation with DALL·E 3 and Runware</p>
                        </div>
                        <div class="igny8-card-icon">
                            <span class="dashicons dashicons-format-image igny8-dashboard-icon-lg igny8-dashboard-icon-purple"></span>
                        </div>
                    </div>
                </div>
                
                <div class="">
                <div class="igny8-card igny8-prompt-section"> <!-- Image Generation Form -->
                    <div class="igny8-dashboard-section">
                        
                    
                    
                    <!-- Prompt Settings -->
                    
                        
                            <div class="igny8-card-body">
                                <form method="POST">
                                    <?php wp_nonce_field('save_prompt', 'save_prompt_nonce'); ?>
                                    
                                    <div class="igny8-form-group">
                                        <label for="prompt_template">Prompt Template</label>
                                        <textarea id="prompt_template" name="prompt_template" rows="8" class="igny8-form-control" placeholder="Enter your custom prompt template..."><?php echo esc_textarea(wp_unslash(get_option('igny8_image_prompt_template', 'Create a high-quality {image_type} image to use as a featured photo for a blog post titled "{post_title}". The image should visually represent the theme, mood, and subject implied by the image prompt: {image_prompt}. Focus on a realistic, well-composed scene that naturally communicates the topic without text or logos. Use balanced lighting, pleasing composition, and photographic detail suitable for lifestyle or editorial web content. Avoid adding any visible or readable text, brand names, or illustrative effects. **And make sure image is not blurry.**'))); ?></textarea>
                                        <small class="form-help">
                                            <strong>Available placeholders:</strong><br>
                                            <code>{post_title}</code> - Post title<br>
                                            <code>{image_prompt}</code> - Image prompt (featured or in-article based on request)<br>
                                            <code>{image_type}</code> - Selected image type (from settings)
                                        </small>
                                        <div class="igny8-form-actions" style="margin-top: 10px;">
                                            <button type="button" id="igny8-save-image-prompt-template" class="igny8-btn igny8-btn-success">Save Prompt Template</button>
                                            <button type="button" id="igny8-reset-image-prompt-template" class="igny8-btn igny8-btn-outline">Reset to Default</button>
                                        </div>
                                    </div>
                                    
                                    <div class="igny8-form-group">
                                        <label for="negative_prompt">Negative Prompt</label>
                                        <textarea id="negative_prompt" name="negative_prompt" rows="2" class="igny8-form-control" placeholder="Describe what you DON'T want in the image..."><?php echo esc_textarea(wp_unslash(get_option('igny8_negative_prompt', 'text, watermark, logo, overlay, title, caption, writing on walls, writing on objects, UI, infographic elements, post title'))); ?></textarea>
                                        <small class="form-help">Specify elements to avoid in the generated image (text, watermarks, logos, etc.).</small>
                                        <div class="igny8-form-actions" style="margin-top: 10px;">
                                            <button type="button" id="igny8-save-negative-prompt" class="igny8-btn igny8-btn-success">Save Negative Prompt</button>
                                        </div>
                                    </div>
                                    
                                </form>
                            </div>
                       
                </div>
            </div>
        </div>
    </div>
    <?php
    
    // Localize script for prompts page
    wp_localize_script('igny8-admin-js', 'IGNY8_PAGE', [
        'module' => 'thinker',
        'subpage' => 'prompts',
        'ajaxUrl' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('igny8_thinker_settings')
    ]);
    
    // Add JavaScript for prompt management
    ?>
    <script type="text/javascript">
    jQuery(document).ready(function($) {
        // Save prompts
        $('#igny8-save-clustering-prompt').on('click', function() {
            savePrompt('clustering_prompt', $('textarea[name="igny8_clustering_prompt"]').val());
        });
        
        $('#igny8-save-ideas-prompt').on('click', function() {
            savePrompt('ideas_prompt', $('textarea[name="igny8_ideas_prompt"]').val());
        });
        
        $('#igny8-save-content-generation-prompt').on('click', function() {
            // Save content generation prompt
            savePrompt('content_generation_prompt', $('textarea[name="igny8_content_generation_prompt"]').val());
        });
        
        // Reset prompts
        $('#igny8-reset-clustering-prompt').on('click', function() {
            resetPrompt('clustering_prompt');
        });
        
        $('#igny8-reset-ideas-prompt').on('click', function() {
            resetPrompt('ideas_prompt');
        });
        
        $('#igny8-reset-content-generation-prompt').on('click', function() {
            // Reset content generation prompt to default
            if (confirm('Are you sure you want to reset the content generation prompt to default? This will overwrite any custom changes.')) {
                $.ajax({
                    url: IGNY8_PAGE.ajaxUrl,
                    type: 'POST',
                    data: {
                        action: 'igny8_reset_multiple_prompts',
                        prompt_types: ['content_generation_prompt'],
                        nonce: IGNY8_PAGE.nonce
                    },
                    success: function(response) {
                        if (response.success) {
                            if (typeof igny8ShowNotification === 'function') {
                                igny8ShowNotification('Content generation prompt reset to default successfully!', 'success', 'thinker');
                            }
                            // Update textarea with reset value
                            if (response.data && response.data.content_generation_prompt) {
                                $('textarea[name="igny8_content_generation_prompt"]').val(response.data.content_generation_prompt);
                            }
                        } else {
                            if (typeof igny8ShowNotification === 'function') {
                                igny8ShowNotification('Error resetting prompts: ' + (response.data || 'Unknown error'), 'error', 'thinker');
                            }
                        }
                    },
                    error: function() {
                        if (typeof igny8ShowNotification === 'function') {
                            igny8ShowNotification('Error resetting prompts. Please try again.', 'error', 'thinker');
                        }
                    }
                });
            }
        });
        
        // Reset image prompt template
        $('#igny8-reset-image-prompt-template').on('click', function() {
            resetImagePromptTemplate();
        });
        
        // Save image prompt template
        $('#igny8-save-image-prompt-template').on('click', function() {
            saveImagePromptTemplate();
        });
        
        // Save negative prompt
        $('#igny8-save-negative-prompt').on('click', function() {
            saveNegativePrompt();
        });
        
        
        
        function savePrompt(promptType, promptValue) {
            $.ajax({
                url: IGNY8_PAGE.ajaxUrl,
                type: 'POST',
                data: {
                    action: 'igny8_save_prompt',
                    prompt_type: promptType,
                    prompt_value: promptValue,
                    nonce: IGNY8_PAGE.nonce
                },
                success: function(response) {
                    if (response.success) {
                        // Show specific prompt name in notification
                        var promptName = promptType.replace('_', ' ').replace(/\b\w/g, l => l.toUpperCase());
                        if (typeof igny8ShowNotification === 'function') {
                            igny8ShowNotification(promptName + ' saved successfully!', 'success', 'thinker');
                        }
                    } else {
                        if (typeof igny8ShowNotification === 'function') {
                            igny8ShowNotification('Error saving prompt: ' + response.data, 'error', 'thinker');
                        }
                    }
                },
                error: function() {
                    if (typeof igny8ShowNotification === 'function') {
                        igny8ShowNotification('Error saving prompt', 'error', 'thinker');
                    }
                }
            });
        }
        
        function resetPrompt(promptType) {
            if (confirm('Are you sure you want to reset this prompt to default? This will overwrite any custom changes.')) {
                $.ajax({
                    url: IGNY8_PAGE.ajaxUrl,
                    type: 'POST',
                    data: {
                        action: 'igny8_reset_prompt',
                        prompt_type: promptType,
                        nonce: IGNY8_PAGE.nonce
                    },
                    success: function(response) {
                        if (response.success) {
                            // Show specific prompt name in notification
                            var promptName = promptType.replace('_', ' ').replace(/\b\w/g, l => l.toUpperCase());
                            if (typeof igny8ShowNotification === 'function') {
                                igny8ShowNotification(promptName + ' reset to default successfully!', 'success', 'thinker');
                            }
                            // Update the textarea with the reset value from response
                            if (response.data && response.data.prompt_value) {
                                $('textarea[name="igny8_' + promptType + '"]').val(response.data.prompt_value);
                            }
                        } else {
                            if (typeof igny8ShowNotification === 'function') {
                                igny8ShowNotification('Error resetting prompt: ' + response.data, 'error', 'thinker');
                            }
                        }
                    },
                    error: function() {
                        if (typeof igny8ShowNotification === 'function') {
                            igny8ShowNotification('Error resetting prompt', 'error', 'thinker');
                        }
                    }
                });
            }
        }
        
        function saveImagePromptTemplate() {
            const promptTemplate = $('#prompt_template').val();
            
            $.ajax({
                url: IGNY8_PAGE.ajaxUrl,
                type: 'POST',
                data: {
                    action: 'igny8_save_prompt',
                    prompt_type: 'image_prompt_template',
                    prompt_value: promptTemplate,
                    nonce: IGNY8_PAGE.nonce
                },
                success: function(response) {
                    if (response.success) {
                        if (typeof igny8ShowNotification === 'function') {
                            igny8ShowNotification('Image prompt template saved successfully!', 'success', 'thinker');
                        }
                    } else {
                        if (typeof igny8ShowNotification === 'function') {
                            igny8ShowNotification('Error saving image prompt template: ' + response.data, 'error', 'thinker');
                        }
                    }
                },
                error: function() {
                    if (typeof igny8ShowNotification === 'function') {
                        igny8ShowNotification('Error saving image prompt template', 'error', 'thinker');
                    }
                }
            });
        }
        
        function saveNegativePrompt() {
            const negativePrompt = $('#negative_prompt').val();
            
            $.ajax({
                url: IGNY8_PAGE.ajaxUrl,
                type: 'POST',
                data: {
                    action: 'igny8_save_prompt',
                    prompt_type: 'negative_prompt',
                    prompt_value: negativePrompt,
                    nonce: IGNY8_PAGE.nonce
                },
                success: function(response) {
                    if (response.success) {
                        if (typeof igny8ShowNotification === 'function') {
                            igny8ShowNotification('Negative prompt saved successfully!', 'success', 'thinker');
                        }
                    } else {
                        if (typeof igny8ShowNotification === 'function') {
                            igny8ShowNotification('Error saving negative prompt: ' + response.data, 'error', 'thinker');
                        }
                    }
                },
                error: function() {
                    if (typeof igny8ShowNotification === 'function') {
                        igny8ShowNotification('Error saving negative prompt', 'error', 'thinker');
                    }
                }
            });
        }
        
        function resetImagePromptTemplate() {
            if (confirm('Are you sure you want to reset the image prompt template to default? This will overwrite any custom changes.')) {
                $.ajax({
                    url: IGNY8_PAGE.ajaxUrl,
                    type: 'POST',
                    data: {
                        action: 'igny8_reset_image_prompt_template',
                        nonce: IGNY8_PAGE.nonce
                    },
                    success: function(response) {
                        if (response.success) {
                            if (typeof igny8ShowNotification === 'function') {
                                igny8ShowNotification('Image Prompt Template reset to default successfully!', 'success', 'thinker');
                            }
                            // Update the textarea with the reset value from response
                            if (response.data && response.data.prompt_value) {
                                $('#prompt_template').val(response.data.prompt_value);
                            }
                        } else {
                            if (typeof igny8ShowNotification === 'function') {
                                igny8ShowNotification('Error resetting image prompt template: ' + response.data, 'error', 'thinker');
                            }
                        }
                    },
                    error: function() {
                        if (typeof igny8ShowNotification === 'function') {
                            igny8ShowNotification('Error resetting image prompt template', 'error', 'thinker');
                        }
                    }
                });
            }
        }
        
    });
    </script>

// Global notification system is handled by core.js

