<?php
/**
 * Admin help and diagnostics page view.
 *
 * @package    100Prints
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

global $wpdb;

$php_version = PHP_VERSION;
$wp_version = get_bloginfo( 'version' );

// Check SDK file
$sdk_path = HUNDRED_PRINTS_PATH . 'assets/js/wordpress-sdk.umd.js';
$sdk_status = file_exists( $sdk_path ) ? '✓ Present' : '✗ Missing';

// Check DB Table
$table_name = $wpdb->prefix . '100p_generation_history';
$db_status = $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) === $table_name ? '✓ Present' : '✗ Missing';

// Check OpenSSL
$openssl_status = function_exists( 'openssl_encrypt' ) ? '✓ Available' : '✗ Unavailable';

// Build diagnostics dump
$diagnostics_dump = "=== 100PRINTS DOCUMENT DIAGNOSTICS ===\n" .
	"Plugin Version: 1.0.0\n" .
	"WordPress Version: " . $wp_version . "\n" .
	"PHP Version: " . $php_version . "\n" .
	"SDK Present: " . $sdk_status . "\n" .
	"DB Table Created: " . $db_status . "\n" .
	"OpenSSL Support: " . $openssl_status . "\n" .
	"Site URL: " . get_bloginfo( 'url' ) . "\n" .
	"Active Theme: " . wp_get_theme()->get( 'Name' ) . "\n" .
	"Database Prefix: " . $wpdb->prefix . "\n" .
	"====================================";
?>

<div class="wrap hundredp-wrap">
	<?php
	// Load shared page header
	$args = [ 'title' => 'Help & Support' ];
	require HUNDRED_PRINTS_PATH . 'admin/partials/header.php';
	?>

	<div class="hundredp-dashboard-body" style="grid-template-columns: 1.5fr 1fr; padding-top:24px;">
		<!-- Column 1: Diagnostics and Help Resources -->
		<section class="hundredp-panel">
			<h2>System Status</h2>
			
			<table class="hundredp-status-table">
				<tr>
					<td><strong>WordPress Version</strong></td>
					<td><?php echo esc_html( $wp_version ); ?> (Requires ≥ 6.3)</td>
					<td><span class="hundredp-badge hundredp-badge-success">Pass</span></td>
				</tr>
				<tr>
					<td><strong>PHP Version</strong></td>
					<td><?php echo esc_html( $php_version ); ?> (Requires ≥ 8.1)</td>
					<td>
						<span class="hundredp-badge <?php echo version_compare( $php_version, '8.1', '>=' ) ? 'hundredp-badge-success' : 'hundredp-badge-error'; ?>">
							<?php echo version_compare( $php_version, '8.1', '>=' ) ? 'Pass' : 'Fail'; ?>
						</span>
					</td>
				</tr>
				<tr>
					<td><strong>SDK UMD Script</strong></td>
					<td><?php echo esc_html( $sdk_status ); ?></td>
					<td>
						<span class="hundredp-badge <?php echo file_exists( $sdk_path ) ? 'hundredp-badge-success' : 'hundredp-badge-error'; ?>">
							<?php echo file_exists( $sdk_path ) ? 'Pass' : 'Fail'; ?>
						</span>
					</td>
				</tr>
				<tr>
					<td><strong>Database Table</strong></td>
					<td><?php echo esc_html( $db_status ); ?> (<?php echo esc_html( $table_name ); ?>)</td>
					<td>
						<span class="hundredp-badge <?php echo $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) === $table_name ? 'hundredp-badge-success' : 'hundredp-badge-error'; ?>">
							<?php echo $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) === $table_name ? 'Pass' : 'Fail'; ?>
						</span>
					</td>
				</tr>
				<tr>
					<td><strong>OpenSSL Support</strong></td>
					<td><?php echo esc_html( $openssl_status ); ?> (Required for credential storage)</td>
					<td>
						<span class="hundredp-badge <?php echo function_exists( 'openssl_encrypt' ) ? 'hundredp-badge-success' : 'hundredp-badge-error'; ?>">
							<?php echo function_exists( 'openssl_encrypt' ) ? 'Pass' : 'Fail'; ?>
						</span>
					</td>
				</tr>
			</table>

			<div style="margin-top:24px;">
				<h3 style="font-size:14px; font-weight:600; margin-bottom:8px;">Diagnostic Report Dump</h3>
				<p style="font-size:12px; color:var(--hundredp-muted); margin-bottom:12px;">Copy this diagnostic dump and provide it in support requests.</p>
				<button class="hundredp-btn hundredp-btn-ghost hundredp-btn-copy-diagnostics">Copy System Info</button>
				<pre class="hundredp-diagnostic-dump"><?php echo esc_html( $diagnostics_dump ); ?></pre>
			</div>
		</section>

		<!-- Column 2: Troubleshooting links -->
		<section class="hundredp-panel" style="display:flex; flex-direction:column; gap:16px;">
			<h2>Support Resources</h2>
			
			<div style="font-size:13px; line-height: 1.5; color:var(--hundredp-muted);">
				<p>For documentation, guides, or issues, please check our developer resources:</p>
			</div>

			<a href="https://100printswith.me/docs/wordpress-plugin" target="_blank" class="hundredp-btn hundredp-btn-ghost hundredp-btn-full" style="justify-content:flex-start;">
				<i class="ph-bold ph-question"></i> View Documentation
			</a>
			
			<a href="https://github.com/100printswithme/wordpress-plugin/issues" target="_blank" class="hundredp-btn hundredp-btn-ghost hundredp-btn-full" style="justify-content:flex-start;">
				<i class="ph-bold ph-warning"></i> Report an Issue
			</a>
			
			<a href="mailto:support@100printswith.me" class="hundredp-btn hundredp-btn-ghost hundredp-btn-full" style="justify-content:flex-start;">
				<i class="ph-bold ph-link"></i> Contact Support
			</a>
		</section>
	</div>
</div>
