<?php

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

if ( ! isset( $module_id ) || empty( $module_id ) ) {
	$module_id = 'quick-debug-toggle';
}

use WP_PowerSuite\Core\WpConfigHelper;

$mod = null;
try {
	$mod = new \QuickDebugToggle\Module();
} catch ( \Throwable $e ) {
	$mod = null;
}

$config_status        = WpConfigHelper::getConfigStatus();
$status               = $mod ? $mod->getDebugStatus() : [ 'wp_debug' => false, 'wp_debug_log' => false, 'wp_debug_display' => false ];
$writable             = $mod ? $mod->isConfigWritable() : false;
$our_block            = $mod ? $mod->hasOurDebugBlock() : false;
$existing             = $mod ? $mod->hasExistingDebugDefines() : false;
$manual               = $mod ? $mod->getDebugBlock() : "define( 'WP_DEBUG', true );\ndefine( 'WP_DEBUG_LOG', true );\ndefine( 'WP_DEBUG_DISPLAY', false );\n@ini_set( 'display_errors', 0 );";
$log_info             = $mod ? $mod->getDebugLogInfo() : [ 'exists' => false, 'size' => '0 B' ];
$log_path             = WpConfigHelper::resolveDebugLogPath();
$can_toggle_constants = $writable && ( $our_block || $existing );
$is_production        = WpConfigHelper::isProductionEnvironment();
?>

<div class="wp-powersuite-module-settings">
	<?php if ( $is_production ) : ?>
	<div class="notice notice-warning" style="margin: 0 0 16px 0; padding: 12px 16px;">
		<p style="margin: 0;"><strong><?php esc_html_e( 'Production environment:', 'wp-powersuite' ); ?></strong>
		<?php esc_html_e( 'Debug settings can expose errors and paths to visitors. Prefer logging to a file and keep WP_DEBUG_DISPLAY off unless you accept that risk.', 'wp-powersuite' ); ?></p>
	</div>
	<?php endif; ?>

	<p class="description">
		<?php esc_html_e( 'Enable or disable WordPress debug mode directly from the admin panel without editing wp-config.php. Perfect for developers who need to quickly troubleshoot issues without FTP access or file editing.', 'wp-powersuite' ); ?>
	</p>

	<?php if ( ! empty( $config_status['requires_network_admin'] ) ) : ?>
	<p class="notice notice-info inline" style="margin: 0 0 16px 0;">
		<?php esc_html_e( 'Multisite: only network administrators can modify wp-config.php from the dashboard.', 'wp-powersuite' ); ?>
	</p>
	<?php endif; ?>

	<?php if ( $config_status['layout_message'] !== '' ) : ?>
	<p class="notice notice-warning inline" style="margin: 0 0 16px 0;">
		<?php echo esc_html( $config_status['layout_message'] ); ?>
	</p>
	<?php endif; ?>

	<form class="wp-powersuite-module-settings-form" data-module-id="<?php echo esc_attr( $module_id ); ?>">
		<?php wp_nonce_field( 'wp_powersuite_save_module_settings_' . $module_id, 'wp_powersuite_nonce' ); ?>
		<div class="wpps-debug-settings" data-module-id="<?php echo esc_attr( $module_id ); ?>" data-is-production="<?php echo $is_production ? '1' : '0'; ?>">

		<div class="wpps-debug-log-card">
			<div class="wpps-debug-log-card-inner">
				<div>
					<strong><?php esc_html_e( 'Debug Log', 'wp-powersuite' ); ?></strong>
					<span class="description" style="margin-left: 8px;">
						<?php
						echo esc_html(
							$log_info['exists']
								? sprintf( __( 'Size: %s', 'wp-powersuite' ), $log_info['size'] )
								: __( 'No log file yet', 'wp-powersuite' )
						);
						?>
					</span>
					<p class="description" style="margin: 6px 0 0;">
						<code><?php echo esc_html( $log_path ); ?></code>
					</p>
				</div>
				<div style="display: flex; gap: 8px; flex-wrap: wrap;">
					<button type="button" class="button button-small wpps-debug-view-log" <?php disabled( ! $log_info['exists'] ); ?>><?php esc_html_e( 'View Log', 'wp-powersuite' ); ?></button>
					<button type="button" class="button button-small wpps-debug-clear-log" <?php disabled( ! $log_info['exists'] ); ?>><?php esc_html_e( 'Clear Log', 'wp-powersuite' ); ?></button>
					<button type="button" class="button button-small wpps-debug-protect-log"><?php esc_html_e( 'Protect Log', 'wp-powersuite' ); ?></button>
				</div>
			</div>
		</div>

		<div class="wpps-debug-status-card">
		<h3 style="margin-top: 0;"><?php esc_html_e( 'Current Status', 'wp-powersuite' ); ?></h3>
		<table class="widefat striped wpps-debug-status-table">
			<tbody>
				<tr>
					<td class="wpps-debug-status-label"><strong><?php esc_html_e( 'WP_DEBUG', 'wp-powersuite' ); ?></strong></td>
					<td>
						<div class="wpps-debug-status-row">
							<span class="wpps-debug-status-badge wpps-debug-<?php echo ! empty( $status['wp_debug'] ) ? 'on' : 'off'; ?>">
								<?php echo ! empty( $status['wp_debug'] ) ? esc_html__( 'ON', 'wp-powersuite' ) : esc_html__( 'OFF', 'wp-powersuite' ); ?>
							</span>
							<span class="description wpps-debug-status-desc"><?php esc_html_e( 'Main debug mode switch', 'wp-powersuite' ); ?></span>
							<?php if ( $can_toggle_constants ) : ?>
							<button type="button" class="button button-small wpps-debug-toggle-const wpps-debug-status-action" data-constant="WP_DEBUG" data-value="<?php echo ! empty( $status['wp_debug'] ) ? '0' : '1'; ?>"><?php echo ! empty( $status['wp_debug'] ) ? esc_html__( 'Disable', 'wp-powersuite' ) : esc_html__( 'Enable', 'wp-powersuite' ); ?></button>
							<?php endif; ?>
						</div>
					</td>
				</tr>
				<tr>
					<td class="wpps-debug-status-label"><strong><?php esc_html_e( 'WP_DEBUG_LOG', 'wp-powersuite' ); ?></strong></td>
					<td>
						<div class="wpps-debug-status-row">
							<span class="wpps-debug-status-badge wpps-debug-<?php echo ! empty( $status['wp_debug_log'] ) ? 'on' : 'off'; ?>">
								<?php echo ! empty( $status['wp_debug_log'] ) ? esc_html__( 'ON', 'wp-powersuite' ) : esc_html__( 'OFF', 'wp-powersuite' ); ?>
							</span>
							<span class="description wpps-debug-status-desc"><?php esc_html_e( 'Error logging to file', 'wp-powersuite' ); ?></span>
							<?php if ( $can_toggle_constants ) : ?>
							<button type="button" class="button button-small wpps-debug-toggle-const wpps-debug-status-action" data-constant="WP_DEBUG_LOG" data-value="<?php echo ! empty( $status['wp_debug_log'] ) ? '0' : '1'; ?>"><?php echo ! empty( $status['wp_debug_log'] ) ? esc_html__( 'Disable', 'wp-powersuite' ) : esc_html__( 'Enable', 'wp-powersuite' ); ?></button>
							<?php endif; ?>
						</div>
					</td>
				</tr>
				<tr>
					<td class="wpps-debug-status-label"><strong><?php esc_html_e( 'WP_DEBUG_DISPLAY', 'wp-powersuite' ); ?></strong></td>
					<td>
						<div class="wpps-debug-status-row">
							<span class="wpps-debug-status-badge wpps-debug-<?php echo ! empty( $status['wp_debug_display'] ) ? 'on' : 'off'; ?>">
								<?php echo ! empty( $status['wp_debug_display'] ) ? esc_html__( 'ON', 'wp-powersuite' ) : esc_html__( 'OFF', 'wp-powersuite' ); ?>
							</span>
							<span class="description wpps-debug-status-desc"><?php esc_html_e( 'Show errors on page', 'wp-powersuite' ); ?></span>
							<?php if ( $can_toggle_constants ) : ?>
							<button type="button" class="button button-small wpps-debug-toggle-const wpps-debug-status-action" data-constant="WP_DEBUG_DISPLAY" data-value="<?php echo ! empty( $status['wp_debug_display'] ) ? '0' : '1'; ?>" data-risky="<?php echo ( $is_production && empty( $status['wp_debug_display'] ) ) ? '1' : '0'; ?>"><?php echo ! empty( $status['wp_debug_display'] ) ? esc_html__( 'Disable', 'wp-powersuite' ) : esc_html__( 'Enable', 'wp-powersuite' ); ?></button>
							<?php endif; ?>
						</div>
					</td>
				</tr>
				<?php if ( $is_production ) : ?>
				<tr class="wpps-debug-status-notice-row">
					<td colspan="2">
						<p class="wpps-debug-production-notice">
							<strong><?php esc_html_e( 'Production site:', 'wp-powersuite' ); ?></strong>
							<?php esc_html_e( 'Enabling WP_DEBUG_DISPLAY exposes errors, paths, and queries to visitors. Keep this off unless you accept that risk.', 'wp-powersuite' ); ?>
						</p>
					</td>
				</tr>
				<?php endif; ?>
			</tbody>
		</table>
		</div>
		<?php if ( $existing && ! $our_block ) : ?>
			<p class="description" style="margin-top: 12px; padding: 10px; background: #f0f6fc; border-left: 4px solid #2271b1;">
				<strong><?php esc_html_e( 'External debug defines:', 'wp-powersuite' ); ?></strong>
				<?php esc_html_e( 'Debug constants are already defined in wp-config.php. You can toggle them individually above, or use Option 2 to edit the file manually.', 'wp-powersuite' ); ?>
			</p>
		<?php endif; ?>

		<hr style="margin: 24px 0;" />

		<h3><?php esc_html_e( 'Option 1: Automatic Toggle', 'wp-powersuite' ); ?></h3>
		<?php if ( ! $writable ) : ?>
			<p class="notice notice-warning inline" style="margin: 0;">
				<?php esc_html_e( 'wp-config.php is not writable. Use manual mode below.', 'wp-powersuite' ); ?>
			</p>
		<?php elseif ( $existing && ! $our_block ) : ?>
			<p class="notice notice-warning inline" style="margin: 0;">
				<?php esc_html_e( 'The Quick Debug block cannot be inserted because debug constants already exist. Toggle individual constants above or use Option 2.', 'wp-powersuite' ); ?>
			</p>
		<?php else : ?>
			<p class="description" style="margin-bottom: 12px;">
				<?php esc_html_e( 'Click to enable or disable debug mode. A backup of wp-config.php is created before enabling.', 'wp-powersuite' ); ?>
			</p>
			<p>
				<button type="button" class="button button-primary wpps-debug-enable" <?php disabled( $our_block ); ?>>
					<?php esc_html_e( 'Enable Debug Mode', 'wp-powersuite' ); ?>
				</button>
				<button type="button" class="button wpps-debug-disable" <?php disabled( ! $our_block ); ?>>
					<?php esc_html_e( 'Disable Debug Mode', 'wp-powersuite' ); ?>
				</button>
			</p>
			<p class="description" style="margin-top: 8px; padding: 10px; background: #fff3cd; border-left: 4px solid #ffc107;">
				<strong><?php esc_html_e( 'Warning:', 'wp-powersuite' ); ?></strong>
				<?php esc_html_e( 'Automatic modification of wp-config.php carries risk. A backup is created, but always ensure you have FTP/SSH access as a fallback.', 'wp-powersuite' ); ?>
			</p>
		<?php endif; ?>

		<hr style="margin: 24px 0;" />

		<h3><?php esc_html_e( 'Option 2: Manual Instructions', 'wp-powersuite' ); ?></h3>
		<p class="description">
			<?php esc_html_e( 'Add this code to your wp-config.php file before the line: ', 'wp-powersuite' ); ?>
			<code>/* That's all, stop editing! */</code>
		</p>
		<div class="wpps-debug-manual-wrap" style="position: relative;">
			<button type="button" class="button button-small wpps-debug-copy-code" style="position: absolute; top: 8px; right: 8px;"><?php esc_html_e( 'Copy', 'wp-powersuite' ); ?></button>
			<pre class="wpps-debug-manual-code" style="background: #f6f7f7; padding: 16px 80px 16px 16px; overflow-x: auto; border: 1px solid #c3c4c7; border-radius: 4px; font-size: 13px;"><code><?php echo esc_html( "// Enable debugging\n" . $manual ); ?></code></pre>
		</div>
		<p class="description">
			<?php esc_html_e( 'To disable, simply remove or comment out these lines.', 'wp-powersuite' ); ?>
		</p>
	</div>
	</form>
</div>

<div id="wpps-debug-log-modal" class="wpps-debug-log-modal" style="display: none; position: fixed; inset: 0; z-index: 100000; background: rgba(0,0,0,0.5); align-items: center; justify-content: center;" role="presentation">
	<div class="wpps-debug-log-modal-panel" role="dialog" aria-modal="true" aria-labelledby="wpps-debug-log-modal-title" tabindex="-1" style="background: #fff; border-radius: 8px; box-shadow: 0 4px 24px rgba(0,0,0,0.15); max-width: 90vw; max-height: 85vh; width: 800px; display: flex; flex-direction: column;">
		<div style="padding: 16px 20px; border-bottom: 1px solid #dcdcde; display: flex; align-items: center; justify-content: space-between;">
			<strong id="wpps-debug-log-modal-title"><?php esc_html_e( 'Debug Log', 'wp-powersuite' ); ?></strong>
			<button type="button" class="wpps-debug-log-modal-close" style="background: none; border: none; cursor: pointer; padding: 4px; color: #50575e;" aria-label="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>"><span class="dashicons dashicons-no-alt" aria-hidden="true"></span></button>
		</div>
		<div class="wpps-debug-log-modal-body" style="padding: 16px; overflow: auto; flex: 1; min-height: 200px;" aria-live="polite" aria-busy="true">
			<p class="description" style="margin: 0;"><?php esc_html_e( 'Loading...', 'wp-powersuite' ); ?></p>
		</div>
		<div class="wpps-debug-log-modal-footer" style="padding: 12px 20px; border-top: 1px solid #dcdcde; background: #f6f7f7; border-radius: 0 0 8px 8px;"></div>
	</div>
</div>

<style>
.wpps-debug-log-card-inner {
	display: flex;
	align-items: center;
	justify-content: space-between;
	flex-wrap: wrap;
	gap: 12px;
}
.wpps-debug-log-card,
.wpps-debug-status-card {
	margin-bottom: 20px;
	padding: 12px 16px;
	background: #f6f7f7;
	border: 1px solid #c3c4c7;
	border-radius: 4px;
}
.wpps-debug-status-card .wpps-debug-status-table {
	width: 100%;
	margin: 0;
	border: 1px solid #c3c4c7;
	background: #fff;
}
.wpps-debug-status-card .wpps-debug-status-table td {
	vertical-align: middle;
}
.wpps-debug-status-label {
	width: 180px;
	white-space: nowrap;
}
.wpps-debug-status-row {
	display: flex;
	align-items: center;
	gap: 10px;
	flex-wrap: wrap;
}
.wpps-debug-status-desc {
	flex: 1 1 160px;
	margin: 0;
}
.wpps-debug-status-action {
	margin-left: auto;
	flex-shrink: 0;
}
.wpps-debug-status-notice-row td {
	padding-top: 0;
	border-top: none;
}
.wpps-debug-production-notice {
	margin: 0;
	padding: 10px 12px;
	background: #fcf0f1;
	border-left: 4px solid #d63638;
	font-size: 13px;
	line-height: 1.5;
}
.wpps-debug-status-badge { display: inline-block; padding: 2px 8px; border-radius: 4px; font-weight: 600; flex-shrink: 0; }
.wpps-debug-on { background: #00a32a; color: #fff; }
.wpps-debug-off { background: #dcdcde; color: #50575e; }
.wpps-debug-log-modal { align-items: center; justify-content: center; }
.wpps-debug-log-modal pre { margin: 0; padding: 12px; background: #1e1e1e; color: #d4d4d4; font-size: 12px; line-height: 1.5; overflow-x: auto; white-space: pre-wrap; word-break: break-all; }
</style>
