<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : system-testing.php
 * @location     : /modules/help/system-testing.php
 * @type         : Admin Page
 * @scope        : Module Only
 * @allowed      : System testing, functionality verification, debugging tools
 * @reusability  : Single Use
 * @notes        : System testing interface for help module
 */

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

// Dev-only access guard
if (!defined('IGNY8_DEV') || IGNY8_DEV !== true) {
    return;
}

?>
<div class="notice notice-info">
    <p><strong>Testing Interface:</strong> Basic functionality verification for taxonomy, schema, and UI components.</p>
</div>

<div class="igny8-test-container" style="display: flex; gap: 20px; margin-top: 20px;">
    
    <!-- Schema Tests -->
    <div class="igny8-test-section" style="flex: 1; border: 1px solid #ddd; padding: 20px; border-radius: 5px;">
        <h2>Database Schema Tests</h2>
        
        <div class="test-buttons">
            <button class="button button-primary" onclick="testDatabaseConnection()">Test DB Connection</button>
            <button class="button" onclick="testTableExists()">Check Tables</button>
            <button class="button" onclick="testSchemaIntegrity()">Schema Integrity</button>
        </div>
        
        <div id="schema-results" class="test-results" style="margin-top: 15px; padding: 10px; background: #f9f9f9; border-radius: 3px; min-height: 100px;">
            <p><em>Click a test button to see results...</em></p>
        </div>
    </div>

    <!-- Taxonomy Tests -->
    <div class="igny8-test-section" style="flex: 1; border: 1px solid #ddd; padding: 20px; border-radius: 5px;">
        <h2>Taxonomy Tests</h2>
        
        <div class="test-buttons">
            <button class="button button-primary" onclick="testTaxonomyRegistration()">Test Taxonomy</button>
            <button class="button" onclick="testCreateTerm()">Create Test Term</button>
            <button class="button" onclick="testTermQueries()">Query Terms</button>
        </div>
        
        <div id="taxonomy-results" class="test-results" style="margin-top: 15px; padding: 10px; background: #f9f9f9; border-radius: 3px; min-height: 100px;">
            <p><em>Click a test button to see results...</em></p>
        </div>
    </div>

    <!-- AJAX Tests -->
    <div class="igny8-test-section" style="flex: 1; border: 1px solid #ddd; padding: 20px; border-radius: 5px;">
        <h2>AJAX & API Tests</h2>
        
        <div class="test-buttons">
            <button class="button button-primary" onclick="testAjaxConnection()">Test AJAX</button>
            <button class="button" onclick="testTableData()">Load Table Data</button>
            <button class="button" onclick="testWorkflowTriggers()">Test Workflows</button>
        </div>
        
        <div id="ajax-results" class="test-results" style="margin-top: 15px; padding: 10px; background: #f9f9f9; border-radius: 3px; min-height: 100px;">
            <p><em>Click a test button to see results...</em></p>
        </div>
    </div>
</div>

<!-- Quick Record Test -->
<div style="margin-top: 30px; border: 1px solid #ddd; padding: 20px; border-radius: 5px;">
    <h2>Quick Record Test</h2>
    <p>Test basic record operations:</p>
    
    <div style="display: flex; gap: 10px; align-items: center; margin-top: 10px;">
        <input type="text" id="test-record-name" placeholder="Test record name" value="Test Record <?php echo date('Y-m-d H:i:s'); ?>" style="flex: 1;">
        <button class="button button-primary" onclick="createTestRecord()">Create Test Record</button>
        <button class="button" onclick="listTestRecords()">List Records</button>
        <button class="button" onclick="clearTestRecords()" style="color: #d63638;">Clear All</button>
    </div>
    
    <div id="record-results" class="test-results" style="margin-top: 15px; padding: 10px; background: #f9f9f9; border-radius: 3px; min-height: 50px;">
        <p><em>Create or list records to see results...</em></p>
    </div>
</div>

<script>
// Test functions
function testDatabaseConnection() {
    updateResults('schema-results', 'Testing database connection...', 'info');
    
    fetch(ajaxurl, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=igny8_test_db_connection&nonce=' + igny8_ajax.nonce
    })
    .then(response => response.json())
    .then(data => {
        updateResults('schema-results', JSON.stringify(data, null, 2), data.success ? 'success' : 'error');
    })
    .catch(error => {
        updateResults('schema-results', 'Error: ' + error.message, 'error');
    });
}

function testTableExists() {
    updateResults('schema-results', 'Checking table existence...', 'info');
    
    fetch(ajaxurl, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=igny8_test_table_exists&nonce=' + igny8_ajax.nonce
    })
    .then(response => response.json())
    .then(data => {
        updateResults('schema-results', JSON.stringify(data, null, 2), data.success ? 'success' : 'error');
    })
    .catch(error => {
        updateResults('schema-results', 'Error: ' + error.message, 'error');
    });
}

function testSchemaIntegrity() {
    updateResults('schema-results', 'Testing schema integrity...', 'info');
    
    fetch(ajaxurl, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=igny8_test_schema_integrity&nonce=' + igny8_ajax.nonce
    })
    .then(response => response.json())
    .then(data => {
        updateResults('schema-results', JSON.stringify(data, null, 2), data.success ? 'success' : 'error');
    })
    .catch(error => {
        updateResults('schema-results', 'Error: ' + error.message, 'error');
    });
}

function testTaxonomyRegistration() {
    updateResults('taxonomy-results', 'Testing taxonomy registration...', 'info');
    
    fetch(ajaxurl, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=igny8_test_taxonomy&nonce=' + igny8_ajax.nonce
    })
    .then(response => response.json())
    .then(data => {
        updateResults('taxonomy-results', JSON.stringify(data, null, 2), data.success ? 'success' : 'error');
    })
    .catch(error => {
        updateResults('taxonomy-results', 'Error: ' + error.message, 'error');
    });
}

function testCreateTerm() {
    updateResults('taxonomy-results', 'Creating test term...', 'info');
    
    fetch(ajaxurl, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=igny8_test_create_term&nonce=' + igny8_ajax.nonce
    })
    .then(response => response.json())
    .then(data => {
        updateResults('taxonomy-results', JSON.stringify(data, null, 2), data.success ? 'success' : 'error');
    })
    .catch(error => {
        updateResults('taxonomy-results', 'Error: ' + error.message, 'error');
    });
}

function testTermQueries() {
    updateResults('taxonomy-results', 'Querying terms...', 'info');
    
    fetch(ajaxurl, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=igny8_test_term_queries&nonce=' + igny8_ajax.nonce
    })
    .then(response => response.json())
    .then(data => {
        updateResults('taxonomy-results', JSON.stringify(data, null, 2), data.success ? 'success' : 'error');
    })
    .catch(error => {
        updateResults('taxonomy-results', 'Error: ' + error.message, 'error');
    });
}

function testAjaxConnection() {
    updateResults('ajax-results', 'Testing AJAX connection...', 'info');
    
    fetch(ajaxurl, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=igny8_test_ajax&nonce=' + igny8_ajax.nonce
    })
    .then(response => response.json())
    .then(data => {
        updateResults('ajax-results', JSON.stringify(data, null, 2), data.success ? 'success' : 'error');
    })
    .catch(error => {
        updateResults('ajax-results', 'Error: ' + error.message, 'error');
    });
}

function testTableData() {
    updateResults('ajax-results', 'Testing table data loading...', 'info');
    
    fetch(ajaxurl, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=igny8_get_table_data&nonce=' + igny8_ajax.nonce + '&table=planner_keywords&page=1&per_page=5'
    })
    .then(response => response.json())
    .then(data => {
        updateResults('ajax-results', JSON.stringify(data, null, 2), data.success ? 'success' : 'error');
    })
    .catch(error => {
        updateResults('ajax-results', 'Error: ' + error.message, 'error');
    });
}

function testWorkflowTriggers() {
    updateResults('ajax-results', 'Testing workflow triggers...', 'info');
    
    fetch(ajaxurl, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=igny8_test_workflows&nonce=' + igny8_ajax.nonce
    })
    .then(response => response.json())
    .then(data => {
        updateResults('ajax-results', JSON.stringify(data, null, 2), data.success ? 'success' : 'error');
    })
    .catch(error => {
        updateResults('ajax-results', 'Error: ' + error.message, 'error');
    });
}

function createTestRecord() {
    const name = document.getElementById('test-record-name').value;
    if (!name.trim()) {
        updateResults('record-results', 'Please enter a record name', 'error');
        return;
    }
    
    updateResults('record-results', 'Creating test record...', 'info');
    
    fetch(ajaxurl, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=igny8_test_create_record&nonce=' + igny8_ajax.nonce + '&name=' + encodeURIComponent(name)
    })
    .then(response => response.json())
    .then(data => {
        updateResults('record-results', JSON.stringify(data, null, 2), data.success ? 'success' : 'error');
    })
    .catch(error => {
        updateResults('record-results', 'Error: ' + error.message, 'error');
    });
}

function listTestRecords() {
    updateResults('record-results', 'Listing test records...', 'info');
    
    fetch(ajaxurl, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=igny8_test_list_records&nonce=' + igny8_ajax.nonce
    })
    .then(response => response.json())
    .then(data => {
        updateResults('record-results', JSON.stringify(data, null, 2), data.success ? 'success' : 'error');
    })
    .catch(error => {
        updateResults('record-results', 'Error: ' + error.message, 'error');
    });
}

function clearTestRecords() {
    if (!confirm('Are you sure you want to clear all test records?')) return;
    
    updateResults('record-results', 'Clearing test records...', 'info');
    
    fetch(ajaxurl, {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'action=igny8_test_clear_records&nonce=' + igny8_ajax.nonce
    })
    .then(response => response.json())
    .then(data => {
        updateResults('record-results', JSON.stringify(data, null, 2), data.success ? 'success' : 'error');
    })
    .catch(error => {
        updateResults('record-results', 'Error: ' + error.message, 'error');
    });
}

function updateResults(elementId, content, type) {
    const element = document.getElementById(elementId);
    const timestamp = new Date().toLocaleTimeString();
    const prefix = type === 'success' ? '✅' : type === 'error' ? '❌' : 'ℹ️';
    
    element.innerHTML = `<div style="margin-bottom: 10px;"><strong>${prefix} [${timestamp}]</strong></div><pre style="white-space: pre-wrap; font-size: 12px;">${content}</pre>`;
    
    if (type === 'success') {
        element.style.borderLeft = '4px solid #00a32a';
    } else if (type === 'error') {
        element.style.borderLeft = '4px solid #d63638';
    } else {
        element.style.borderLeft = '4px solid #0073aa';
    }
}
</script>

<?php
// Note: This file is included by help.php, so no need to capture content or include layout
// The content will be captured by the parent help.php file
?>