<?php
/**
 * Power deactivation warning callbacks.
 *
 * @package WPAgentCompanion
 */

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

/**
 * Returns only local, non-invasive evidence that Power left durable control
 * state behind. The Companion must not inspect, alter or remove Power files
 * while it is being deactivated.
 *
 * @return array{power_enabled:bool,power_stages:string[],control_artifacts:bool,warning_required:bool}
 */
function wpagent_companion_deactivation_warning_state(): array {
	$stages = array();
	foreach ( array( 'p1', 'p2', 'p3', 'p4', 'backup', 'restore' ) as $stage ) {
		if ( (bool) get_option( 'wp_agent_power_stage_' . $stage, false ) ) {
			$stages[] = $stage;
		}
	}
	$control_artifacts = function_exists( 'wpagent_power_control_artifacts_present' ) && wpagent_power_control_artifacts_present();
	if ( ! $control_artifacts && defined( 'WP_CONTENT_DIR' ) && is_string( WP_CONTENT_DIR ) && '' !== WP_CONTENT_DIR ) {
		$content_dir = realpath( WP_CONTENT_DIR );
		if ( false !== $content_dir ) {
			$marker   = $content_dir . DIRECTORY_SEPARATOR . '.wp-agent-power-control-location.php';
			$fallback = $content_dir . DIRECTORY_SEPARATOR . '.wp-agent-power-control';
			$control_artifacts = file_exists( $marker ) || is_link( $marker ) || file_exists( $fallback ) || is_link( $fallback );
		}
	}
	$power_enabled = (bool) get_option( 'wp_agent_power_enabled', false );
	return array(
		'power_enabled'     => $power_enabled,
		'power_stages'      => $stages,
		'control_artifacts' => $control_artifacts,
		'warning_required'  => $power_enabled || ! empty( $stages ) || $control_artifacts,
	);
}

function wpagent_companion_deactivation_warning_notice(): void {
	if ( ! current_user_can( 'activate_plugins' ) || ! function_exists( 'get_current_screen' ) ) {
		return;
	}
	$screen = get_current_screen();
	if ( ! $screen || 'plugins' !== $screen->id || ! wpagent_companion_deactivation_warning_state()['warning_required'] ) {
		return;
	}
	printf(
		'<div class="notice notice-warning"><p><strong>%1$s</strong> %2$s</p></div>',
		esc_html__( 'Power-Hinweis vor der Deaktivierung.', 'wp-agent-companion' ),
		esc_html__( 'Power ist noch freigeschaltet oder es gibt Power-Kontroll- beziehungsweise Restore-Artefakte. Das Deaktivieren des Companion macht keine bisherigen Dateiänderungen rückgängig und entfernt diese Daten nicht.', 'wp-agent-companion' )
	);
}
add_action( 'admin_notices', 'wpagent_companion_deactivation_warning_notice' );

function wpagent_companion_deactivation_warning_confirmation(): void {
	if ( ! current_user_can( 'activate_plugins' ) || ! function_exists( 'get_current_screen' ) ) {
		return;
	}
	$screen = get_current_screen();
	if ( ! $screen || 'plugins' !== $screen->id || ! wpagent_companion_deactivation_warning_state()['warning_required'] ) {
		return;
	}
	$plugin       = plugin_basename( WPAGENT_COMPANION_DIR . 'wp-agent-companion.php' );
	$confirmation = __( 'Power ist noch freigeschaltet oder hat Kontroll- beziehungsweise Restore-Artefakte. Die Deaktivierung macht frühere Dateiänderungen nicht rückgängig und entfernt diese Daten nicht. Companion trotzdem deaktivieren?', 'wp-agent-companion' );
	?>
	<script>
		(() => {
			const plugin = <?php echo wp_json_encode( $plugin ); ?>;
			const confirmation = <?php echo wp_json_encode( $confirmation ); ?>;
			document.addEventListener('click', (event) => {
				if (!(event.target instanceof Element)) return;
				const link = event.target.closest('a[href]');
				if (!link) return;
				const url = new URL(link.href, window.location.href);
				if ('deactivate' !== url.searchParams.get('action') || plugin !== url.searchParams.get('plugin')) return;
				if (!window.confirm(confirmation)) event.preventDefault();
			});
		})();
	</script>
	<?php
}
add_action( 'admin_footer-plugins.php', 'wpagent_companion_deactivation_warning_confirmation' );
