<?php
/**
 * Theme Compatibility Notice
 *
 * Displays admin notice for theme compatibility issues.
 * Warns users if their theme version is outdated and requires update.
 *
 * @package Forumax
 */

defined( 'ABSPATH' ) || exit;

/**
 * Display admin notice for theme compatibility
 *
 * Checks if the active theme meets the minimum required version
 * for Forumax compatibility and displays a warning notice if not.
 * 
 * The notice is not shown if:
 * - The theme is up to date
 * - A child theme is active and its parent theme (Docy, Docly, or Ama) meets the required version
 *
 * @return void
 */
function forumax_theme_compatibility_notice() {
	$theme            = wp_get_theme();
	$theme_name       = $theme->get( 'Name' );
	$current_theme_v  = $theme->get( 'Version' );
	$parent_theme     = $theme->parent();

	// Set required version for each theme
	$my_themes = array(
		'Docy'  => '4.4.0',
		'Docly' => '2.3.2',
		'Ama'   => '1.7.1',
	);

	foreach ( $my_themes as $name => $required_v ) {
		// Check if theme name contains one of our themes
		if ( stripos( $theme_name, $name ) !== false ) {

			// Don't show notice if a child theme is active and parent theme meets the required version
			if ( $parent_theme ) {
				$parent_theme_name    = $parent_theme->get( 'Name' );
				$parent_theme_version = $parent_theme->get( 'Version' );

				// If parent theme matches and meets the required version, don't show the notice
				if ( $name === $parent_theme_name && version_compare( $parent_theme_version, $required_v, '>=' ) ) {
					return; // Don't show the notice
				}
			}

			// Check if current version is less than required version
			if ( version_compare( $current_theme_v, $required_v, '<' ) ) {
				?>
				<div class="notice notice-error" style="border-left-color: #dc3232; padding: 15px;">
				<div style="display: flex; align-items: flex-start; gap: 15px;">
					<span style="font-size: 40px; line-height: 1;">⚠️</span>
					<div style="flex: 1;">
						<h2 style="margin: 0 0 10px 0; color: #dc3232; font-size: 18px;">
							<?php esc_html_e( 'CRITICAL: Immediate Action Required!', 'forumax' ); ?>
						</h2>
						<p style="font-size: 15px; margin: 0 0 10px 0; line-height: 1.6;">
							<strong><?php esc_html_e( 'Your site may break or malfunction!', 'forumax' ); ?></strong>
						</p>
					<p style="font-size: 14px; margin: 0 0 15px 0; line-height: 1.6;">
						<?php
						printf(
							/* translators: 1: Theme name, 2: Current version, 3: Required version */
							esc_html__( 'Your %1$s theme is outdated (version %2$s). Please update to version %3$s or higher. We have made significant changes to the Forumax plugin, and using an outdated theme will cause compatibility issues and may break your website.', 'forumax' ),
							'<strong>' . esc_html( $theme_name ) . '</strong>',
							'<code style="background: #fff3cd; padding: 2px 6px; border-radius: 3px; color: #856404;">' . esc_html( $current_theme_v ) . '</code>',
							'<code style="background: #d4edda; padding: 2px 6px; border-radius: 3px; color: #155724; font-weight: bold;">' . esc_html( $required_v ) . '</code>'
						);
						?>
					</p>
					<div style="background: #fff3cd; border-left: 4px solid #ffc107; padding: 12px; margin-bottom: 15px;">
						<p style="margin: 0; font-size: 13px; color: #856404;">
							<strong><?php esc_html_e( '⚡ What has changed:', 'forumax' ); ?></strong><br>
							<?php esc_html_e( 'We have completely restructured the Forumax plugin with new features, improved performance, and enhanced security. Your current theme version is not compatible with these updates.', 'forumax' ); ?>
						</p>
					</div>
					<p style="margin: 0;">
						<a href="<?php echo esc_url( admin_url( 'update-core.php' ) ); ?>" class="button button-primary" style="background: #dc3232; border-color: #dc3232; font-size: 14px; height: auto; padding: 8px 20px;">
							<?php esc_html_e( '🔄 Update Theme Now', 'forumax' ); ?>
						</a>
						<span style="margin-left: 15px; color: #666; font-size: 13px;">
							<?php esc_html_e( 'This update is mandatory for your site to function properly.', 'forumax' ); ?>
						</span>
					</p>
				</div>
			</div>
				</div>
				<style>
					.notice.notice-error h2 {
						font-size: 18px !important;
					}
				</style>
				<?php
			}
			break; // Stop loop once theme is found
		}
	}
}
add_action( 'admin_notices', 'forumax_theme_compatibility_notice' );
