<?php
/**
 * ProQyz Migration Diagnostic Page
 * 
 * Shows all constants, variables, and states for debugging menu issues
 * 
 * @package Spacetree
 * @since   2.1.0
 */

defined( 'ABSPATH' ) || exit;

// Get all relevant data
$db_version_key = 'ST_DB_VERSION';
$current_db_version = get_option( $db_version_key, '1.0' );
$target_db_version = defined('ST_DB_VERSION') ? ST_DB_VERSION : '2.0';
$plugin_version = defined('ST_VERSION') ? ST_VERSION : 'NOT DEFINED';

$migration_required = version_compare( $current_db_version, $target_db_version, '<' );
$migration_completed = version_compare( $current_db_version, $target_db_version, '>=' );
$plugin_disabled = defined('ST_PLUGIN_DISABLED') && ST_PLUGIN_DISABLED;

// Calculate show_menu condition (same as class-st-admin-menus.php)
$show_menu = $migration_required || $plugin_disabled || $migration_completed;

?>
<div class="wrap">
	<h1>🔍 ProQyz Migration Diagnostic</h1>
	<p>Use this page to debug why the menu might not be showing.</p>

	<div class="notice notice-info" style="margin: 20px 0;">
		<p><strong>💡 If menu is not showing, check the values below and the debug log.</strong></p>
	</p>
	</div>

	<h2>Database Version</h2>
	<table class="widefat" style="max-width: 800px;">
		<tr>
			<th style="width: 300px;">Option Key</th>
			<td><code><?php echo esc_html( $db_version_key ); ?></code></td>
		</tr>
		<tr>
			<th>Current DB Version (from options)</th>
			<td><strong><?php echo esc_html( $current_db_version ); ?></strong></td>
		</tr>
		<tr>
			<th>Target DB Version (ST_DB_VERSION constant)</th>
			<td><strong><?php echo esc_html( $target_db_version ); ?></strong></td>
		</tr>
		<tr>
			<th>Plugin Version (ST_VERSION constant)</th>
			<td><strong><?php echo esc_html( $plugin_version ); ?></strong></td>
		</tr>
	</table>

	<h2>Migration States</h2>
	<table class="widefat" style="max-width: 800px;">
		<tr>
			<th style="width: 300px;">Migration Required</th>
			<td>
				<span style="padding: 4px 8px; border-radius: 3px; <?php echo $migration_required ? 'background: #dc3232; color: white;' : 'background: #46b450; color: white;'; ?>">
					<?php echo $migration_required ? '✓ YES' : '✗ NO'; ?>
				</span>
				<span style="color: #646970; font-size: 13px; margin-left: 10px;">
					(<?php echo esc_html( $current_db_version ); ?> &lt; <?php echo esc_html( $target_db_version ); ?>)
				</span>
			</td>
		</tr>
		<tr>
			<th>Migration Completed</th>
			<td>
				<span style="padding: 4px 8px; border-radius: 3px; <?php echo $migration_completed ? 'background: #46b450; color: white;' : 'background: #dc3232; color: white;'; ?>">
					<?php echo $migration_completed ? '✓ YES' : '✗ NO'; ?>
				</span>
				<span style="color: #646970; font-size: 13px; margin-left: 10px;">
					(<?php echo esc_html( $current_db_version ); ?> &gt;= <?php echo esc_html( $target_db_version ); ?>)
				</span>
			</td>
		</tr>
		<tr>
			<th>Plugin Disabled</th>
			<td>
				<span style="padding: 4px 8px; border-radius: 3px; <?php echo $plugin_disabled ? 'background: #dc3232; color: white;' : 'background: #46b450; color: white;'; ?>">
					<?php echo $plugin_disabled ? '✓ YES' : '✗ NO'; ?>
				</span>
				<span style="color: #646970; font-size: 13px; margin-left: 10px;">
					(ST_PLUGIN_DISABLED constant)
				</span>
			</td>
		</tr>
	</table>

	<h2>Menu Visibility Logic</h2>
	<table class="widefat" style="max-width: 800px;">
		<tr>
			<th style="width: 300px;">Show Menu Condition</th>
			<td>
				<code>$migration_required || $plugin_disabled || $migration_completed</code>
			</td>
		</tr>
		<tr>
			<th>Evaluation</th>
			<td>
				<code><?php echo $migration_required ? 'true' : 'false'; ?> || <?php echo $plugin_disabled ? 'true' : 'false'; ?> || <?php echo $migration_completed ? 'true' : 'false'; ?></code>
			</td>
		</tr>
		<tr>
			<th>Result (Should Show Menu?)</th>
			<td>
				<span style="padding: 4px 12px; border-radius: 3px; font-weight: bold; <?php echo $show_menu ? 'background: #46b450; color: white;' : 'background: #dc3232; color: white;'; ?>">
					<?php echo $show_menu ? '✓ YES - MENU SHOULD BE VISIBLE' : '✗ NO - MENU HIDDEN'; ?>
				</span>
			</td>
		</tr>
	</table>

	<h2>All Relevant Constants</h2>
	<table class="widefat" style="max-width: 800px;">
		<thead>
			<tr>
				<th>Constant Name</th>
				<th>Defined?</th>
				<th>Value</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td><code>ST_DB_VERSION</code></td>
				<td><?php echo defined('ST_DB_VERSION') ? '<span style="color: green;">✓ Yes</span>' : '<span style="color: red;">✗ No</span>'; ?></td>
				<td><?php echo defined('ST_DB_VERSION') ? esc_html( ST_DB_VERSION ) : 'N/A'; ?></td>
			</tr>
			<tr>
				<td><code>ST_VERSION</code></td>
				<td><?php echo defined('ST_VERSION') ? '<span style="color: green;">✓ Yes</span>' : '<span style="color: red;">✗ No</span>'; ?></td>
				<td><?php echo defined('ST_VERSION') ? esc_html( ST_VERSION ) : 'N/A'; ?></td>
			</tr>
			<tr>
				<td><code>ST_MIGRATION_REQUIRED</code></td>
				<td><?php echo defined('ST_MIGRATION_REQUIRED') ? '<span style="color: green;">✓ Yes</span>' : '<span style="color: red;">✗ No</span>'; ?></td>
				<td><?php echo defined('ST_MIGRATION_REQUIRED') ? (ST_MIGRATION_REQUIRED ? 'true' : 'false') : 'N/A'; ?></td>
			</tr>
			<tr>
				<td><code>ST_MIGRATION_COMPLETED</code></td>
				<td><?php echo defined('ST_MIGRATION_COMPLETED') ? '<span style="color: green;">✓ Yes</span>' : '<span style="color: red;">✗ No</span>'; ?></td>
				<td><?php echo defined('ST_MIGRATION_COMPLETED') ? (ST_MIGRATION_COMPLETED ? 'true' : 'false') : 'N/A'; ?></td>
			</tr>
			<tr>
				<td><code>ST_PLUGIN_DISABLED</code></td>
				<td><?php echo defined('ST_PLUGIN_DISABLED') ? '<span style="color: green;">✓ Yes</span>' : '<span style="color: red;">✗ No</span>'; ?></td>
				<td><?php echo defined('ST_PLUGIN_DISABLED') ? (ST_PLUGIN_DISABLED ? 'true' : 'false') : 'N/A'; ?></td>
			</tr>
			<tr>
				<td><code>ST_ABSPATH</code></td>
				<td><?php echo defined('ST_ABSPATH') ? '<span style="color: green;">✓ Yes</span>' : '<span style="color: red;">✗ No</span>'; ?></td>
				<td><?php echo defined('ST_ABSPATH') ? esc_html( ST_ABSPATH ) : 'N/A'; ?></td>
			</tr>
			<tr>
				<td><code>ST_PLUGIN_FILE</code></td>
				<td><?php echo defined('ST_PLUGIN_FILE') ? '<span style="color: green;">✓ Yes</span>' : '<span style="color: red;">✗ No</span>'; ?></td>
				<td><?php echo defined('ST_PLUGIN_FILE') ? esc_html( ST_PLUGIN_FILE ) : 'N/A'; ?></td>
			</tr>
		</tbody>
	</table>

	<h2>Debug Log Location</h2>
	<?php
	$error_log_path = ini_get('error_log');
	if (empty($error_log_path) || $error_log_path === 'syslog') {
		$error_log_path = WP_CONTENT_DIR . '/debug.log';
	}
	$error_log_exists = file_exists($error_log_path);
	?>
	<table class="widefat" style="max-width: 800px;">
		<tr>
			<th style="width: 300px;">Debug Log File</th>
			<td><code><?php echo esc_html( $error_log_path ); ?></code></td>
		</tr>
		<tr>
			<th>File Exists?</th>
			<td><?php echo $error_log_exists ? '<span style="color: green;">✓ Yes</span>' : '<span style="color: red;">✗ No (will be created on first log)</span>'; ?></td>
		</tr>
		<tr>
			<th>WP_DEBUG Enabled?</th>
			<td><?php echo defined('WP_DEBUG') && WP_DEBUG ? '<span style="color: green;">✓ Yes - Logs are being written</span>' : '<span style="color: orange;">✗ No - Enable in wp-config.php</span>'; ?></td>
		</tr>
	</table>

	<?php if ( defined('WP_DEBUG') && WP_DEBUG ) : ?>
	<div class="notice notice-success" style="margin: 20px 0;">
		<p><strong>✓ WP_DEBUG is enabled</strong></p>
		<p>Check your debug log file for detailed menu registration logs:</p>
		<pre style="background: #2b2b2b; color: #f8f8f2; padding: 10px; border-radius: 4px; overflow-x: auto;">ST_Admin_Menus: Class instantiated
ProQyz Menu Debug:
  - Current DB Version: <?php echo esc_html( $current_db_version ); ?>

  - Target DB Version: <?php echo esc_html( $target_db_version ); ?>

  - Migration Required: <?php echo $migration_required ? 'yes' : 'no'; ?>

  - Migration Completed: <?php echo $migration_completed ? 'yes' : 'no'; ?>

  - Plugin Disabled: <?php echo $plugin_disabled ? 'yes' : 'no'; ?>

  - Menu Added: ProQyz [Migrate/Update/Status]</pre>
	</div>
	<?php else : ?>
	<div class="notice notice-warning" style="margin: 20px 0;">
		<p><strong>⚠️ WP_DEBUG is disabled</strong></p>
		<p>To see detailed debug logs, add this to your <code>wp-config.php</code>:</p>
		<pre style="background: #2b2b2b; color: #f8f8f2; padding: 10px; border-radius: 4px; overflow-x: auto;">define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);</pre>
	</div>
	<?php endif; ?>

	<h2>Quick Actions</h2>
	<p>
		<a href="<?php echo esc_url( admin_url() ); ?>" class="button button-secondary">
			← Back to Dashboard
		</a>
		<a href="<?php echo esc_url( admin_url( 'admin.php?page=st-proqyz-migrate' ) ); ?>" class="button button-primary">
			View Migration Page
		</a>
		<?php if ( $error_log_exists ) : ?>
		<a href="#" onclick="alert('File location:\n<?php echo esc_js( $error_log_path ); ?>\n\nOpen this file in a text editor to view logs.'); return false;" 
		   class="button button-secondary">
			📄 View Log File Location
		</a>
		<?php endif; ?>
	</p>
</div>

