<?php
/**
 * Publisher performance profiles — Lite / Standard / Full (all sites).
 *
 * @package Newsroom_AI_Assistant
 */

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

if ( ! function_exists( 'newsai_publisher_profile_choices' ) ) {
	/**
	 * @return array<string, array{label:string,desc:string}>
	 */
	function newsai_publisher_profile_choices() {
		return [
			'lite'     => [
				'label' => __( 'Lite — maximum performance', 'newsroom-ai-assistant' ),
				'desc'  => __( 'Editor sidebar off, no Auto-Timeline or speakable on articles, minimal frontend injections. Schema, GSC, and indexing stay on.', 'newsroom-ai-assistant' ),
			],
			'standard' => [
				'label' => __( 'Standard — recommended default', 'newsroom-ai-assistant' ),
				'desc'  => __( 'Full editor sidebar, manual timeline (add milestones yourself), balanced schema richness. GSC traffic-drop alerts on; FAQ block off.', 'newsroom-ai-assistant' ),
			],
			'full'     => [
				'label' => __( 'Full — all editor & schema features', 'newsroom-ai-assistant' ),
				'desc'  => __( 'Maximum JSON-LD richness, Auto-Timeline, UX toggles on. Still no auto FAQ placeholders by default.', 'newsroom-ai-assistant' ),
			],
		];
	}
}

if ( ! function_exists( 'newsai_get_publisher_profile' ) ) {
	function newsai_get_publisher_profile() {
		$p = (string) get_option( 'newsai_publisher_profile', 'standard' );
		return array_key_exists( $p, newsai_publisher_profile_choices() ) ? $p : 'standard';
	}
}

if ( ! function_exists( 'newsai_apply_publisher_profile' ) ) {
	/**
	 * Apply performance preset (does not change newsroom vs blog site profile).
	 *
	 * @param string $profile lite|standard|full
	 */
	function newsai_apply_publisher_profile( $profile ) {
		$profile = (string) $profile;
		if ( ! array_key_exists( $profile, newsai_publisher_profile_choices() ) ) {
			$profile = 'standard';
		}

		switch ( $profile ) {
			case 'lite':
				update_option( 'newsai_lite_mode', 'yes', false );
				update_option( 'newsai_timeline_mode', 'manual', false );
				update_option( 'newsai_advanced_nlp', 'no', false );
				update_option( 'newsai_ui_onboarding_modal', 'no', false );
				update_option( 'newsai_gsc_traffic_drop_enabled', 'yes', false );
				update_option( 'newsai_editor_faq_enabled', 'no', false );
				update_option( 'nros_schema_richness', 'balanced', false );
				update_option( 'newsai_speakable_selectors', '', false );
				break;

			case 'full':
				update_option( 'newsai_lite_mode', 'no', false );
				update_option( 'newsai_timeline_mode', 'auto', false );
				update_option( 'newsai_advanced_nlp', 'yes', false );
				update_option( 'newsai_ui_onboarding_modal', 'yes', false );
				update_option( 'newsai_ui_circular_gauge', 'yes', false );
				update_option( 'newsai_mobile_bottom_sheet', 'yes', false );
				update_option( 'newsai_gsc_traffic_drop_enabled', 'yes', false );
				update_option( 'newsai_editor_faq_enabled', 'no', false );
				update_option( 'nros_schema_richness', 'full', false );
				break;

			case 'standard':
			default:
				update_option( 'newsai_lite_mode', 'no', false );
				update_option( 'newsai_timeline_mode', 'manual', false );
				update_option( 'newsai_advanced_nlp', 'yes', false );
				update_option( 'newsai_ui_onboarding_modal', 'yes', false );
				update_option( 'newsai_gsc_traffic_drop_enabled', 'yes', false );
				update_option( 'newsai_editor_faq_enabled', 'no', false );
				update_option( 'nros_schema_richness', 'balanced', false );
				break;
		}

		update_option( 'newsai_publisher_profile', $profile, false );

		if ( function_exists( 'newsai_retire_content_decay_radar' ) ) {
			newsai_retire_content_decay_radar();
		}
		if ( function_exists( 'newsai_schedule_seasonal_radar' ) ) {
			newsai_schedule_seasonal_radar();
		}
		if ( function_exists( 'newsai_clear_all_plugin_transients' ) ) {
			newsai_clear_all_plugin_transients();
		}
		if ( function_exists( 'newsai_lite_mode_bootstrap' ) ) {
			newsai_lite_mode_bootstrap();
		}
	}
}

if ( ! function_exists( 'newsai_publisher_profile_bootstrap_option' ) ) {
	/**
	 * New installs: lite when lite mode already on, else standard.
	 */
	function newsai_publisher_profile_bootstrap_option() {
		if ( false !== get_option( 'newsai_publisher_profile', false ) ) {
			return;
		}
		$default = ( get_option( 'newsai_lite_mode', 'no' ) === 'yes' ) ? 'lite' : 'standard';
		add_option( 'newsai_publisher_profile', $default, '', false );
	}
}
add_action( 'admin_init', 'newsai_publisher_profile_bootstrap_option', 3 );
