<?php
/**
 * React admin UI policy — classic is the supported default (2.7.0).
 *
 * @package Newsroom_AI_Assistant
 */

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

if ( ! defined( 'NEWSAI_ADMIN_UI_POLICY_VERSION' ) ) {
	define( 'NEWSAI_ADMIN_UI_POLICY_VERSION', 2 );
}

if ( ! function_exists( 'newsai_admin_ui_apply_policy' ) ) {
	/**
	 * One-time migration: disable experimental React shell for existing installs.
	 */
	function newsai_admin_ui_apply_policy() {
		if ( defined( 'NEWSAI_REACT_SETTINGS' ) ) {
			return;
		}
		$stored = (int) get_option( 'newsai_admin_ui_policy_version', 1 );
		if ( $stored >= NEWSAI_ADMIN_UI_POLICY_VERSION ) {
			return;
		}
		update_option( 'newsai_settings_ui', 'classic', false );
		update_option( 'newsai_admin_ui_policy_version', NEWSAI_ADMIN_UI_POLICY_VERSION, false );
		update_option( 'newsai_admin_ui_rollback_notice', '1', false );
	}
}
add_action( 'plugins_loaded', 'newsai_admin_ui_apply_policy', 5 );

if ( ! function_exists( 'newsai_admin_ui_rollback_notice' ) ) {
	function newsai_admin_ui_rollback_notice() {
		if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
			return;
		}
		if ( ! get_option( 'newsai_admin_ui_rollback_notice' ) ) {
			return;
		}
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		if ( ! $screen || strpos( (string) $screen->id, 'newsai-settings' ) === false ) {
			return;
		}
		delete_option( 'newsai_admin_ui_rollback_notice' );
		echo '<div class="notice notice-info is-dismissible"><p>';
		echo esc_html__(
			'Newsroom OS is using the classic admin again. The experimental React UI is off by default while we redesign it — you can try it later under Settings → Editorial → Editor UX.',
			'newsroom-ai-assistant'
		);
		echo '</p></div>';
	}
}
add_action( 'admin_notices', 'newsai_admin_ui_rollback_notice' );

if ( ! function_exists( 'newsai_admin_classic_body_class' ) ) {
	/**
	 * Body classes for classic Newsroom admin shell styling.
	 *
	 * @param string $classes Space-separated admin body classes.
	 * @return string
	 */
	function newsai_admin_classic_body_class( $classes ) {
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		if ( ! $screen || strpos( (string) $screen->id, 'newsai-settings' ) === false ) {
			return $classes;
		}
		if ( function_exists( 'newsai_settings_react_enabled' ) && newsai_settings_react_enabled() ) {
			return $classes;
		}
		$classes .= ' nros-classic-admin-shell';
		$tab = function_exists( 'newsai_admin_current_tab' ) ? newsai_admin_current_tab() : 'alerts';
		if ( $tab === 'settings' || ( isset( $_GET['tab'] ) && sanitize_key( wp_unslash( (string) $_GET['tab'] ) ) === 'settings' ) ) {
			$classes .= ' nros-classic-tab-settings';
		}
		return $classes;
	}
}
add_filter( 'admin_body_class', 'newsai_admin_classic_body_class' );

if ( ! function_exists( 'newsai_admin_shell_wpfooter_fix' ) ) {
	/**
	 * Inline fallback: hide core WP footer on classic Newsroom settings screens.
	 */
	function newsai_admin_shell_wpfooter_fix() {
		if ( ! is_admin() ) {
			return;
		}
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		if ( ! $screen || strpos( (string) $screen->id, 'newsai-settings' ) === false ) {
			return;
		}
		if ( function_exists( 'newsai_settings_react_enabled' ) && newsai_settings_react_enabled() ) {
			return;
		}
		echo '<style id="nros-classic-wpfooter-fix">body[class*="newsai-settings"] #wpfooter,body.nros-classic-admin-shell #wpfooter{display:none!important;visibility:hidden!important;height:0!important;overflow:hidden!important}body.nros-classic-tab-settings .nros-classic-main,body.nros-classic-tab-settings .nros-settings-bento{padding-bottom:48px}</style>';
	}
}
add_action( 'admin_head', 'newsai_admin_shell_wpfooter_fix', 99 );
