<?php
/**
 * Admin interface manager
 *
 * Registers menu items and pages, loads admin assets.
 *
 * @package BW_Schema
 * @since 3.0.0-babel
 */

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

class BW_Schema_Admin {

	/**
	 * Array of registered page instances
	 *
	 * @var BW_Schema_Page[]
	 */
	private $pages = array();

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->register_hooks();
		$this->register_pages();
	}

	/**
	 * Register WordPress hooks
	 *
	 * @return void
	 */
	private function register_hooks() {
		add_action( 'admin_menu', array( $this, 'register_admin_menu' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
		add_action( 'admin_init', array( $this, 'maybe_activation_redirect' ) );
	}

	/**
	 * Redirect to the setup wizard once after activation
	 *
	 * The activation hook sets a transient; the first admin page load after
	 * that lands on the wizard — unless setup was already completed (e.g.
	 * reactivation of a configured site) or this is a bulk activation.
	 *
	 * @return void
	 */
	public function maybe_activation_redirect() {
		if ( ! get_transient( 'bw_schema_activation_redirect' ) ) {
			return;
		}

		delete_transient( 'bw_schema_activation_redirect' );

		if ( wp_doing_ajax() || is_network_admin() || isset( $_GET['activate-multi'] ) ) {
			return;
		}

		if ( ! current_user_can( 'manage_options' ) || BW_Schema_Page_Setup::is_complete() ) {
			return;
		}

		wp_safe_redirect( admin_url( 'admin.php?page=bw-schema-setup' ) );
		exit;
	}

	/**
	 * Register admin pages
	 *
	 * Instantiate all page classes.
	 *
	 * @return void
	 */
	private function register_pages() {
		// Dashboard (top-level)
		$this->pages['dashboard'] = new BW_Schema_Page_Dashboard();

		// Settings pages (under Dashboard)
		// Note: Locations is a tab on the Organization page, not its own menu item
		$this->pages['organization'] = new BW_Schema_Page_Organization();
		$this->pages['people'] = new BW_Schema_Page_People();
		$this->pages['content'] = new BW_Schema_Page_Content();
		$this->pages['tools'] = new BW_Schema_Page_Tools();
		$this->pages['help'] = new BW_Schema_Page_Help();

		// Setup wizard — registered so its URL works, hidden from the menu
		$this->pages['setup'] = new BW_Schema_Page_Setup();
	}

	/**
	 * Register admin menu items
	 *
	 * Called by admin_menu hook.
	 *
	 * @return void
	 */
	public function register_admin_menu() {
		foreach ( $this->pages as $page ) {
			$page->register();
		}

		// The wizard is reached from tasks/links, never from the menu.
		// Removal must happen on admin_head — AFTER the page access check
		// (which needs the submenu entry present to resolve capability)
		// but BEFORE the menu HTML is rendered.
		add_action( 'admin_head', function () {
			remove_submenu_page( 'bw-schema', 'bw-schema-setup' );
		} );
	}

	/**
	 * Enqueue admin scripts and styles
	 *
	 * Loads only on Solomon Schema screens (never on unrelated admin pages).
	 * The dashboard gets its own stylesheet on top of the shared one.
	 *
	 * @param string $hook Current admin page hook suffix
	 * @return void
	 */
	public function enqueue_admin_assets( $hook ) {
		// Load on this plugin's screens, and on edit screens of post types
		// that carry the Solomon Schema metabox
		$is_plugin_screen = ( false !== strpos( (string) $hook, 'bw-schema' ) );

		$is_metabox_screen = false;
		if ( in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) {
			$screen            = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
			$is_metabox_screen = $screen
				&& in_array( $screen->post_type, BW_Schema_Metabox::get_target_post_types(), true );
		}

		if ( ! $is_plugin_screen && ! $is_metabox_screen ) {
			return;
		}

		// Shared admin CSS (all plugin screens)
		$shared_css = BW_SCHEMA_PLUGIN_DIR . 'admin/css/bw-schema-admin.css';
		wp_enqueue_style(
			'bw-schema-admin',
			BW_SCHEMA_PLUGIN_URL . 'admin/css/bw-schema-admin.css',
			array(),
			file_exists( $shared_css ) ? filemtime( $shared_css ) : BW_SCHEMA_VERSION
		);

		// Dashboard-specific CSS
		if ( 'toplevel_page_bw-schema' === $hook ) {
			$dash_css = BW_SCHEMA_PLUGIN_DIR . 'admin/css/dashboard.css';
			wp_enqueue_style(
				'bw-schema-dashboard',
				BW_SCHEMA_PLUGIN_URL . 'admin/css/dashboard.css',
				array( 'bw-schema-admin' ),
				file_exists( $dash_css ) ? filemtime( $dash_css ) : BW_SCHEMA_VERSION
			);
		}
	}

	/**
	 * Get a registered page instance
	 *
	 * @param string $page_slug Page slug
	 * @return BW_Schema_Page|null Page instance or null if not found
	 */
	public function get_page( $page_slug ) {
		return $this->pages[ $page_slug ] ?? null;
	}

	/**
	 * Get all registered pages
	 *
	 * @return BW_Schema_Page[]
	 */
	public function get_pages() {
		return $this->pages;
	}
}
