<?php
/**
 * Conditional module loader — core (frontend-safe) vs admin/cron/ajax bundle.
 *
 * @package Newsroom_AI_Assistant
 */

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

if ( ! function_exists( 'newsai_load_plugin_module' ) ) {
	/**
	 * @param string $file Basename under plugin root or includes/.
	 */
	function newsai_load_plugin_module( $file ) {
		$file = ltrim( (string) $file, '/' );
		if ( file_exists( NEWSAI_PATH . $file ) ) {
			require_once NEWSAI_PATH . $file;
			return true;
		}
		if ( file_exists( NEWSAI_PATH . 'includes/' . $file ) ) {
			require_once NEWSAI_PATH . 'includes/' . $file;
			return true;
		}
		return false;
	}
}

if ( ! function_exists( 'newsai_is_plugin_runtime_admin' ) ) {
	/**
	 * Load dashboards, AI Assist, wizard, weekly email builders, etc.
	 */
	function newsai_is_plugin_runtime_admin() {
		if ( is_admin() ) {
			return true;
		}
		if ( wp_doing_cron() ) {
			return true;
		}
		if ( wp_doing_ajax() ) {
			return true;
		}
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
			return true;
		}
		if ( function_exists( 'newsai_is_http_rest_request' ) && newsai_is_http_rest_request() ) {
			$method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( (string) $_SERVER['REQUEST_METHOD'] ) : '';
			if ( in_array( $method, [ 'POST', 'PUT', 'PATCH', 'DELETE' ], true ) ) {
				return true;
			}
		}
		return (bool) apply_filters( 'newsai_force_admin_modules', false );
	}
}

if ( ! function_exists( 'newsai_get_core_modules' ) ) {
	/**
	 * Always loaded: schema, indexing, crons tied to frontend hooks, save_post, REST.
	 *
	 * @return string[]
	 */
	function newsai_get_core_modules() {
		$modules = [
			'newsai-secrets.php',
			'lite-mode.php',
			'large-site-performance.php',
			'geo-targeting.php',
			'publisher-profile.php',
			'pro-licensing.php',
			'pro-indexnow.php',
			'class-schema-engine.php',
			'breaking-mode.php',
			'claim-review.php',
			'admin/settings-registry.php',
			'admin/react-ui-policy.php',
			'admin/dashboard-registry.php',
			'admin/insights-registry.php',
			'admin/author-perf-registry.php',
			'admin/pro-registry.php',
			'rest/settings-rest.php',
			'rest/dashboard-rest.php',
			'rest/insights-rest.php',
			'rest/author-perf-rest.php',
			'rest/pro-rest.php',
		];
		return apply_filters( 'newsai_core_modules', $modules );
	}
}

if ( ! function_exists( 'newsai_get_admin_modules' ) ) {
	/**
	 * Admin UI, editor AI, dashboards — skipped on anonymous frontend pageviews.
	 *
	 * @return string[]
	 */
	function newsai_get_admin_modules() {
		$modules = [
			'google-trends-rss.php',
			'google-news-rss.php',
			'usage-registry.php',
			'pro-features.php',
			'pro-gsc-sync.php',
			'gsc-traffic-drop.php',
			'seasonal-radar.php',
			'discover-score.php',
			'ai-readiness-engine.php',
			'tag-entity-enrichment.php',
			'transient-cleanup.php',
			'pro-author-performance.php',
			'pro-ai-providers.php',
			'pro-ai-features.php',
			'pro-ai-alt-text.php',
			'pro-task-comm.php',
			'weekly-report-email.php',
			'chief-insights-helpers.php',
			'kanban-tasks.php',
			'war-room.php',
			'pro-crux.php',
			'wizard.php',
			'admin/settings-react.php',
			'admin-ui.php',
			'alerts-manager.php',
			'author-meta.php',
		];
		return apply_filters( 'newsai_admin_modules', $modules );
	}
}

if ( ! function_exists( 'newsai_bootstrap_plugin_modules' ) ) {
	function newsai_bootstrap_plugin_modules() {
		static $done = false;
		if ( $done ) {
			return;
		}
		$done = true;

		foreach ( newsai_get_core_modules() as $file ) {
			newsai_load_plugin_module( $file );
		}

		if ( newsai_is_plugin_runtime_admin() ) {
			foreach ( newsai_get_admin_modules() as $file ) {
				newsai_load_plugin_module( $file );
			}
		}
	}
}

newsai_bootstrap_plugin_modules();
