<?php
/**
 * Lite mode — trims writer-facing overhead on high-traffic sites.
 * Keeps JSON-LD schema output and admin analytics.
 * Disables Auto-Timeline / Breaking / Speakable injections.
 * Background schema warm stops unless Nightly schema warm window is active.
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! function_exists( 'newsai_lite_mode_active' ) ) {
	/**
	 * @return bool
	 */
	function newsai_lite_mode_active() {
		return get_option( 'newsai_lite_mode', 'no' ) === 'yes';
	}
}

if ( ! function_exists( 'newsai_lite_mode_skips_post_editor_ui' ) ) {
	/**
	 * When true, skip the post editor sidebar metabox and its JS/CSS bundle.
	 *
	 * @return bool
	 */
	function newsai_lite_mode_skips_post_editor_ui() {
		return newsai_lite_mode_active();
	}
}

if ( ! function_exists( 'newsai_lite_mode_skips_frontend_injections' ) ) {
	/**
	 * When true, skip Auto-Timeline, Breaking badge, and Speakable snippet on the_content.
	 *
	 * @return bool
	 */
	function newsai_lite_mode_skips_frontend_injections() {
		return newsai_lite_mode_active();
	}
}

if ( ! function_exists( 'newsai_lite_mode_skips_admin_live_polling' ) ) {
	/**
	 * When true, skip War-room AJAX polling and other background admin refresh loops.
	 *
	 * @return bool
	 */
	function newsai_lite_mode_skips_admin_live_polling() {
		return newsai_lite_mode_active();
	}
}

if ( ! function_exists( 'newsai_admin_current_tab' ) ) {
	/**
	 * Active Newsroom OS settings tab (alerts|insights|perf|settings|pro).
	 *
	 * @return string
	 */
	function newsai_admin_current_tab() {
		if ( ! is_admin() ) {
			return 'alerts';
		}
		$tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( (string) $_GET['tab'] ) ) : 'alerts';
		return $tab !== '' ? $tab : 'alerts';
	}
}

if ( ! function_exists( 'newsai_admin_screen_needs_editor_bundle' ) ) {
	/**
	 * Whether to load the heavy editor script bundle (script.js + command palette).
	 *
	 * @param string $hook Admin page hook suffix.
	 * @return bool
	 */
	function newsai_admin_screen_needs_editor_bundle( $hook ) {
		$hook = (string) $hook;
		if ( $hook === 'post.php' || $hook === 'post-new.php' ) {
			return ! newsai_lite_mode_skips_post_editor_ui();
		}
		// Author inbox only — Newsroom settings tabs use palette + tab JS (kanban, insights, etc.).
		if ( strpos( $hook, 'newsai-inbox' ) !== false ) {
			return true;
		}
		// Dashboard (alerts tab) — Trending & News widget uses script.js radar handlers.
		if ( strpos( $hook, 'newsai-settings' ) !== false && function_exists( 'newsai_admin_current_tab' ) ) {
			if ( 'alerts' === newsai_admin_current_tab() && ! newsai_lite_mode_active() ) {
				return true;
			}
		}
		return false;
	}
}

if ( ! function_exists( 'newsai_lite_mode_bootstrap' ) ) {
	/**
	 * Detach Lite-only runtime hooks. Schema JSON-LD stays; background warm cron stops.
	 *
	 * @return void
	 */
	function newsai_lite_mode_bootstrap() {
		if ( ! newsai_lite_mode_active() ) {
			return;
		}
		remove_filter( 'the_content', 'newsai_timeline_auto_inject', 20 );
		remove_filter( 'the_content', 'newsai_breaking_frontend_badge', 3 );
		remove_filter( 'the_content', 'nros_speakable_prepend_concise_snippet', 4 );
		remove_action( 'add_meta_boxes', 'newsai_add_sidebar' );
		// Drop schema migration cron unless nightly warm window is active (Lite stays on).
		if ( function_exists( 'nros_schema_sync_lite_warm_cron_state' ) ) {
			nros_schema_sync_lite_warm_cron_state();
		} elseif ( function_exists( 'wp_clear_scheduled_hook' ) ) {
			wp_clear_scheduled_hook( 'nros_schema_legacy_warm_batch' );
		}
	}
}
add_action( 'plugins_loaded', 'newsai_lite_mode_bootstrap', 100 );
