<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : thinker.php
 * @location     : /modules/thinker/thinker.php
 * @type         : Admin Page
 * @scope        : Module Only
 * @allowed      : Thinker module logic, subpage routing, AI interface
 * @reusability  : Single Use
 * @notes        : Main thinker page with subpage routing for AI features
 */

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

// Handle URL parameters for subpages
$subpage = $_GET['sp'] ?? 'main';
$GLOBALS['igny8_current_subpage'] = $subpage;

// Start output buffering
ob_start();

switch ($subpage) {
    case 'prompts':
        include plugin_dir_path(__FILE__) . 'prompts.php';
        break;
    case 'profile':
        include plugin_dir_path(__FILE__) . 'profile.php';
        break;
    case 'strategies':
        include plugin_dir_path(__FILE__) . 'strategies.php';
        break;
    case 'image-testing':
        include plugin_dir_path(__FILE__) . 'image-testing.php';
        break;
    case 'main':
    default:
        // Main dashboard content
        ?>
<div class="igny8-module-page">
    <div class="igny8-container">
        <!-- Thinker 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>Thinker Workflow Steps</h3>
                        <p class="igny8-card-subtitle">Track your AI configuration progress</p>
                    </div>
                    <div class="igny8-card-icon">
                        <span class="dashicons dashicons-admin-tools igny8-dashboard-icon-lg igny8-dashboard-icon-green"></span>
                    </div>
                </div>
            </div>
            
            <div class="igny8-grid igny8-grid-4">
                <!-- AI Prompts Step -->
                <div class="igny8-card igny8-step-card completed">
                    <div class="igny8-card-body">
                        <div class="igny8-step-header">
                            <div class="igny8-step-number">1</div>
                            <div class="igny8-step-title">AI Prompts</div>
                        </div>
                        <div class="igny8-step-status">
                            <span class="igny8-step-status-icon">✅</span>
                            <span class="igny8-step-status-text">Configured</span>
                        </div>
                        <div class="igny8-step-data">
                            Manage AI prompt templates
                        </div>
                        <div class="igny8-step-action">
                            <a href="?page=igny8-thinker&sp=prompts" class="igny8-btn igny8-btn-primary igny8-btn-sm">Configure</a>
                        </div>
                    </div>
                </div>
                
                <!-- AI Profile Step -->
                <div class="igny8-card igny8-step-card current">
                    <div class="igny8-card-body">
                        <div class="igny8-step-header">
                            <div class="igny8-step-number">2</div>
                            <div class="igny8-step-title">AI Profile</div>
                        </div>
                        <div class="igny8-step-status">
                            <span class="igny8-step-status-icon">⏳</span>
                            <span class="igny8-step-status-text">Ready</span>
                        </div>
                        <div class="igny8-step-data">
                            Set AI behavior and tone
                        </div>
                        <div class="igny8-step-action">
                            <a href="?page=igny8-thinker&sp=profile" class="igny8-btn igny8-btn-primary igny8-btn-sm">Configure</a>
                        </div>
                    </div>
                </div>
                
                <!-- Content Strategies Step -->
                <div class="igny8-card igny8-step-card pending">
                    <div class="igny8-card-body">
                        <div class="igny8-step-header">
                            <div class="igny8-step-number">3</div>
                            <div class="igny8-step-title">Content Strategies</div>
                        </div>
                        <div class="igny8-step-status">
                            <span class="igny8-step-status-icon">⏳</span>
                            <span class="igny8-step-status-text">Pending</span>
                        </div>
                        <div class="igny8-step-data">
                            Define content strategies
                        </div>
                        <div class="igny8-step-action">
                            <a href="?page=igny8-thinker&sp=strategies" class="igny8-btn igny8-btn-primary igny8-btn-sm">Configure</a>
                        </div>
                    </div>
                </div>
                
                <!-- Image Testing Step -->
                <div class="igny8-card igny8-step-card pending">
                    <div class="igny8-card-body">
                        <div class="igny8-step-header">
                            <div class="igny8-step-number">4</div>
                            <div class="igny8-step-title">Image Testing</div>
                        </div>
                        <div class="igny8-step-status">
                            <span class="igny8-step-status-icon">⏳</span>
                            <span class="igny8-step-status-text">Pending</span>
                        </div>
                        <div class="igny8-step-data">
                            Test DALL·E 3 integration
                        </div>
                        <div class="igny8-step-action">
                            <a href="?page=igny8-thinker&sp=image-testing" class="igny8-btn igny8-btn-primary igny8-btn-sm">Test</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- AI Settings & Quick Actions -->
        <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>AI Settings & Quick Actions</h3>
                            <p class="igny8-card-subtitle">Manage AI configuration and test connections</p>
                        </div>
                        <div class="igny8-card-icon">
                            <span class="dashicons dashicons-admin-settings igny8-dashboard-icon-lg igny8-dashboard-icon-blue"></span>
                        </div>
                    </div>
                </div>
                <div class="igny8-card-body">
                    <div class="igny8-flex-row">
                        <button onclick="igny8TestAIConnection()" class="igny8-btn igny8-btn-primary">Test AI Connection</button>
                        <button onclick="igny8ResetAISettings()" class="igny8-btn igny8-btn-outline">Reset AI Settings</button>
                        <button onclick="igny8ViewAILogs()" class="igny8-btn igny8-btn-outline">View AI Logs</button>
                    </div>
                </div>
            </div>
        </div>

        <script type="text/javascript">
        jQuery(document).ready(function($) {
            // Test AI Connection
            window.igny8TestAIConnection = function() {
                // Check if API key is configured
                const apiKey = '<?php echo get_option('igny8_api_key', ''); ?>';
                if (!apiKey) {
                    igny8ShowNotification('Please configure your OpenAI API key in Settings first.', 'warning', 'thinker');
                    return;
                }
                
                // Show testing notification
                igny8ShowNotification('Testing AI connection...', 'info', 'thinker');
                
                // Simulate connection test (replace with actual API test)
                setTimeout(() => {
                    igny8ShowNotification('AI connection test completed successfully!', 'success', 'thinker');
                }, 2000);
            };

            // Execute Action based on selection
            window.igny8ExecuteAction = function() {
                const selectedAction = $('input[name="ai_action"]:checked').val();
                
                if (selectedAction === 'reset') {
                    if (confirm('Are you sure you want to reset all AI settings to default? This will affect all modules.')) {
                        // Simulate reset action
                        igny8ShowNotification('AI settings have been reset to default values.', 'success', 'thinker');
                    }
                } else if (selectedAction === 'logs') {
                    window.open('<?php echo admin_url('admin.php?page=igny8-analytics&sp=status'); ?>', '_blank');
                }
            };

            // Reset AI Settings (legacy function)
            window.igny8ResetAISettings = function() {
                if (confirm('Are you sure you want to reset all AI settings to default? This will affect all modules.')) {
                    igny8ShowNotification('AI settings reset feature coming soon!', 'info', 'thinker');
                }
            };

            // View AI Logs (legacy function)
            window.igny8ViewAILogs = function() {
                window.open('<?php echo admin_url('admin.php?page=igny8-analytics&sp=status'); ?>', '_blank');
            };
        });
        </script>
    </div>
</div>
        <?php
        break;
}

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

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