<?php
/**
 * Test Script for Email Template Override Functionality
 * 
 * This file can be used to test the email template override system.
 * 
 * USAGE:
 * 1. Temporarily add this to your functions.php or a custom plugin:
 * 
 *    add_action('admin_init', function() {
 *        if (current_user_can('manage_options') && isset($_GET['test_guard_dog_email_template'])) {
 *            require_once WP_PLUGIN_DIR . '/guard-dog/includes/views/emails/test-template-override.php';
 *            guard_dog_test_email_template_override();
 *            exit;
 *        }
 *    });
 * 
 * 2. Visit: /wp-admin/?test_guard_dog_email_template=1
 * 
 * This will display which template is being used and show a preview.
 * 
 * SAFETY: This test does NOT send any actual emails.
 *
 * @package GuardDog
 * @since 1.8.5
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

function guard_dog_test_email_template_override() {
	// Security check
	if ( ! current_user_can( 'manage_options' ) ) {
		wp_die( 'You do not have permission to access this page.' );
	}

	// Get current user for testing
	$current_user = wp_get_current_user();
	
	// Test data
	$template_args = array(
		'code' => '123456',
		'user' => $current_user,
		'site_name' => get_bloginfo( 'name' ),
		'expiry_minutes' => 10
	);

	echo '<html><head><meta charset="UTF-8"><title>Guard Dog Email Template Test</title>';
	echo '<style>
		body { 
			font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
			margin: 0;
			padding: 20px;
			background: #f0f0f0;
		}
		.test-container {
			max-width: 1200px;
			margin: 0 auto;
			background: white;
			padding: 30px;
			border-radius: 8px;
			box-shadow: 0 2px 4px rgba(0,0,0,0.1);
		}
		h1 { 
			color: #0073aa;
			border-bottom: 3px solid #0073aa;
			padding-bottom: 10px;
		}
		h2 {
			color: #333;
			margin-top: 30px;
			border-left: 4px solid #0073aa;
			padding-left: 15px;
		}
		.status {
			padding: 15px;
			margin: 20px 0;
			border-radius: 4px;
			font-weight: 600;
		}
		.status-success {
			background: #d4edda;
			color: #155724;
			border: 1px solid #c3e6cb;
		}
		.status-info {
			background: #d1ecf1;
			color: #0c5460;
			border: 1px solid #bee5eb;
		}
		.status-warning {
			background: #fff3cd;
			color: #856404;
			border: 1px solid #ffeaa7;
		}
		.code {
			background: #f8f9fa;
			padding: 15px;
			border-left: 3px solid #6c757d;
			font-family: monospace;
			overflow-x: auto;
			margin: 15px 0;
		}
		.preview {
			border: 2px solid #dee2e6;
			padding: 20px;
			margin: 20px 0;
			background: #f8f9fa;
			border-radius: 4px;
		}
		.tabs {
			display: flex;
			gap: 10px;
			margin: 20px 0;
			border-bottom: 2px solid #dee2e6;
		}
		.tab {
			padding: 10px 20px;
			background: #e9ecef;
			border: none;
			cursor: pointer;
			border-radius: 4px 4px 0 0;
			font-weight: 600;
		}
		.tab.active {
			background: #0073aa;
			color: white;
		}
		.tab-content {
			display: none;
		}
		.tab-content.active {
			display: block;
		}
		table {
			width: 100%;
			border-collapse: collapse;
			margin: 20px 0;
		}
		th, td {
			padding: 12px;
			text-align: left;
			border-bottom: 1px solid #dee2e6;
		}
		th {
			background: #f8f9fa;
			font-weight: 600;
		}
		.back-link {
			display: inline-block;
			margin: 20px 0;
			padding: 10px 20px;
			background: #0073aa;
			color: white;
			text-decoration: none;
			border-radius: 4px;
		}
		.back-link:hover {
			background: #005a87;
		}
	</style>';
	echo '</head><body>';
	
	echo '<div class="test-container">';
	echo '<h1>🔒 Guard Dog Email Template Override Test</h1>';
	
	// Test HTML template
	echo '<h2>📧 HTML Template Test</h2>';
	
	$html_content = Guard_Dog_View_Loader::load_email_template( 'two-factor-code', $template_args );
	
	if ( ! empty( $html_content ) ) {
		echo '<div class="status status-success">✓ HTML template loaded successfully!</div>';
	} else {
		echo '<div class="status status-warning">⚠ HTML template failed to load</div>';
	}
	
	// Test plain text template
	echo '<h2>📄 Plain Text Template Test</h2>';
	
	$text_content = Guard_Dog_View_Loader::load_email_template( 'two-factor-code-plain', $template_args );
	
	if ( ! empty( $text_content ) ) {
		echo '<div class="status status-success">✓ Plain text template loaded successfully!</div>';
	} else {
		echo '<div class="status status-warning">⚠ Plain text template failed to load</div>';
	}
	
	// Check template locations
	echo '<h2>📁 Template Locations</h2>';
	
	$theme_dir = get_stylesheet_directory();
	$parent_theme_dir = get_template_directory();
	$plugin_dir = WP_PLUGIN_DIR . '/guard-dog';
	
	echo '<table>';
	echo '<tr><th>Location</th><th>Path</th><th>Status</th></tr>';
	
	// Check child theme
	$child_theme_path = $theme_dir . '/guard-dog/emails/two-factor-code.php';
	echo '<tr>';
	echo '<td><strong>Child Theme Override</strong></td>';
	echo '<td><code>' . esc_html( $child_theme_path ) . '</code></td>';
	echo '<td>' . ( file_exists( $child_theme_path ) ? '<span style="color:green;">✓ Found</span>' : '<span style="color:#999;">Not found</span>' ) . '</td>';
	echo '</tr>';
	
	// Check parent theme
	if ( $parent_theme_dir !== $theme_dir ) {
		$parent_theme_path = $parent_theme_dir . '/guard-dog/emails/two-factor-code.php';
		echo '<tr>';
		echo '<td><strong>Parent Theme Override</strong></td>';
		echo '<td><code>' . esc_html( $parent_theme_path ) . '</code></td>';
		echo '<td>' . ( file_exists( $parent_theme_path ) ? '<span style="color:green;">✓ Found</span>' : '<span style="color:#999;">Not found</span>' ) . '</td>';
		echo '</tr>';
	}
	
	// Check plugin default
	$plugin_path = $plugin_dir . '/includes/views/emails/two-factor-code.php';
	echo '<tr>';
	echo '<td><strong>Plugin Default</strong></td>';
	echo '<td><code>' . esc_html( $plugin_path ) . '</code></td>';
	echo '<td>' . ( file_exists( $plugin_path ) ? '<span style="color:green;">✓ Found</span>' : '<span style="color:red;">✗ Missing</span>' ) . '</td>';
	echo '</tr>';
	
	echo '</table>';
	
	// Template variables
	echo '<h2>📝 Template Variables</h2>';
	echo '<div class="code">';
	echo '<strong>Available to templates:</strong><br>';
	echo '$code = "' . esc_html( $template_args['code'] ) . '"<br>';
	echo '$user->display_name = "' . esc_html( $template_args['user']->display_name ) . '"<br>';
	echo '$user->user_email = "' . esc_html( $template_args['user']->user_email ) . '"<br>';
	echo '$site_name = "' . esc_html( $template_args['site_name'] ) . '"<br>';
	echo '$expiry_minutes = ' . absint( $template_args['expiry_minutes'] );
	echo '</div>';
	
	// Preview tabs
	echo '<h2>👁 Template Preview</h2>';
	echo '<div class="tabs">';
	echo '<button class="tab active" onclick="showTab(\'html\')">HTML Preview</button>';
	echo '<button class="tab" onclick="showTab(\'html-source\')">HTML Source</button>';
	echo '<button class="tab" onclick="showTab(\'text\')">Plain Text</button>';
	echo '</div>';
	
	// HTML preview
	echo '<div id="html-preview" class="tab-content active">';
	echo '<div class="preview">';
	// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Intentionally outputting HTML template preview
	echo $html_content;
	echo '</div>';
	echo '</div>';
	
	// HTML source
	echo '<div id="html-source-preview" class="tab-content">';
	echo '<div class="code">';
	echo '<pre>' . esc_html( $html_content ) . '</pre>';
	echo '</div>';
	echo '</div>';
	
	// Plain text preview
	echo '<div id="text-preview" class="tab-content">';
	echo '<div class="code">';
	echo '<pre>' . esc_html( $text_content ) . '</pre>';
	echo '</div>';
	echo '</div>';
	
	// Instructions
	echo '<h2>📚 How to Create a Theme Override</h2>';
	echo '<div class="status status-info">';
	echo '<p><strong>Step 1:</strong> Create the directory structure in your theme:</p>';
	echo '<div class="code">' . esc_html( $theme_dir ) . '/guard-dog/emails/</div>';
	
	echo '<p><strong>Step 2:</strong> Copy the template files from the plugin:</p>';
	echo '<div class="code">' . esc_html( $plugin_dir ) . '/includes/views/emails/two-factor-code.php<br>';
	echo esc_html( $plugin_dir ) . '/includes/views/emails/plain/two-factor-code-plain.php</div>';
	
	echo '<p><strong>Step 3:</strong> Customize the templates in your theme directory</p>';
	echo '<p><strong>Step 4:</strong> Reload this test page to see your customized templates!</p>';
	echo '</div>';
	
	// Back link
	echo '<a href="' . esc_url( admin_url() ) . '" class="back-link">← Back to Dashboard</a>';
	
	echo '</div>'; // .test-container
	
	echo '<script>
		function showTab(tabName) {
			// Hide all tabs
			var tabs = document.querySelectorAll(".tab-content");
			tabs.forEach(function(tab) {
				tab.classList.remove("active");
			});
			
			// Remove active from all buttons
			var buttons = document.querySelectorAll(".tab");
			buttons.forEach(function(button) {
				button.classList.remove("active");
			});
			
			// Show selected tab
			document.getElementById(tabName + "-preview").classList.add("active");
			event.target.classList.add("active");
		}
	</script>';
	
	echo '</body></html>';
}

