<?php
/**
 * Registers the Conflict Radar top-level admin menu and its four subpages,
 * and renders the (Phase 3 carry-forward) logging-disabled notice on CR screens.
 *
 * @package ConflictRadar\Admin
 */

declare( strict_types=1 );

namespace ConflictRadar\Admin;

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

class AdminMenu {

	private const CAP  = 'manage_options';
	private const SLUG = 'conflict-radar';

	public function register(): void {
		add_action( 'admin_menu', array( $this, 'registerMenu' ) );
		add_action( 'admin_notices', array( $this, 'maybeRenderLoggingDisabledNotice' ) );

		// Settings save handler (admin-post.php) — Pro only. The free build ships no
		// alert settings, so neither the menu nor the save handler are registered.
		if ( defined( 'CONFLICT_RADAR_PRO_VERSION' ) ) {
			( new \ConflictRadar\Pro\SettingsPage() )->register();
		}
	}

	/**
	 * Build the menu. Page objects are created here (on admin_menu), but each
	 * WP_List_Table is instantiated lazily inside the page's render(), never here.
	 */
	public function registerMenu(): void {
		$dashboard = new DashboardPage();

		add_menu_page(
			__( 'Conflict Radar', 'conflict-radar' ),
			__( 'Conflict Radar', 'conflict-radar' ),
			self::CAP,
			self::SLUG,
			array( $dashboard, 'render' ),
			'dashicons-shield-alt',
			80
		);

		// First submenu reuses the parent slug to relabel the top-level item "Dashboard".
		add_submenu_page(
			self::SLUG,
			__( 'Dashboard', 'conflict-radar' ),
			__( 'Dashboard', 'conflict-radar' ),
			self::CAP,
			self::SLUG,
			array( $dashboard, 'render' )
		);

		add_submenu_page(
			self::SLUG,
			__( 'Update History', 'conflict-radar' ),
			__( 'Update History', 'conflict-radar' ),
			self::CAP,
			'conflict-radar-updates',
			array( new UpdateHistoryPage(), 'render' )
		);

		add_submenu_page(
			self::SLUG,
			__( 'Error Log', 'conflict-radar' ),
			__( 'Error Log', 'conflict-radar' ),
			self::CAP,
			'conflict-radar-errors',
			array( new ErrorLogPage(), 'render' )
		);

		// Settings — Pro only. The free build configures no alerts, so it ships no
		// Settings submenu (the page class lives in src/Pro/, stripped from free).
		if ( defined( 'CONFLICT_RADAR_PRO_VERSION' ) ) {
			add_submenu_page(
				self::SLUG,
				__( 'Settings', 'conflict-radar' ),
				__( 'Settings', 'conflict-radar' ),
				self::CAP,
				'conflict-radar-settings',
				array( new \ConflictRadar\Pro\SettingsPage(), 'render' )
			);
		}

		// "Go Pro" — shown only to non-paying users; the menu slug is the Freemius
		// upgrade URL so the item links straight to the pricing page. Disappears
		// once the user upgrades (is_not_paying() returns false).
		if ( function_exists( 'conflict_radar_fs' ) && \conflict_radar_fs()->is_not_paying() ) {
			add_submenu_page(
				self::SLUG,
				__( 'Go Pro', 'conflict-radar' ),
				__( 'Go Pro', 'conflict-radar' ),
				self::CAP,
				\conflict_radar_fs()->get_upgrade_url()
			);
		}
	}

	/**
	 * F2 logging-disabled notice. Shown ONLY on Conflict Radar screens and ONLY
	 * when ErrorLogReader has stored an empty log path (the disabled state).
	 */
	public function maybeRenderLoggingDisabledNotice(): void {
		$screen = get_current_screen();
		if ( ! $screen || false === strpos( $screen->id, self::SLUG ) ) {
			return;
		}

		if ( ! current_user_can( self::CAP ) ) {
			return;
		}

		$log_path = (string) get_option( 'conflict_radar_log_path', '' );
		if ( '' !== $log_path ) {
			return;
		}

		printf(
			'<div class="notice notice-warning"><p>%s</p></div>',
			esc_html__( 'PHP error logging is disabled. Enable WP_DEBUG in wp-config.php to use error monitoring.', 'conflict-radar' )
		);
	}
}
