<?php
/**
 * Admin: the Guides menu, the reading UI (list + single views), and the
 * settings screen shell. Views live in admin/views/.
 */

defined( 'ABSPATH' ) || exit;

class BW_Guides_Admin {

	const MENU_SLUG     = 'bw-guides';
	const SETTINGS_SLUG = 'bw-guides-settings';

	/**
	 * The shipped capability names, mirrored here because a site-side integration
	 * may read the constant to detect whether this plugin offers its own
	 * capabilities yet. Behaviour lives in BW_Guides_Caps — including the
	 * `edit_posts` floor that makes this change invisible on upgrade.
	 */
	const READ_CAP   = BW_Guides_Caps::READ;
	const MANAGE_CAP = BW_Guides_Caps::MANAGE;

	/** @return string Capability to browse/open a guide (filterable). */
	public static function read_cap() {
		return BW_Guides_Caps::read();
	}

	/** @return string Capability to change guides (filterable). */
	public static function manage_cap() {
		return BW_Guides_Caps::manage();
	}

	private $browse_hook   = '';
	private $settings_hook = '';

	public function register() {
		add_action( 'admin_menu', array( $this, 'register_menu' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
	}

	public function register_menu() {
		$this->browse_hook = add_menu_page(
			__( 'Guides', 'bw-guides' ),
			__( 'Guides', 'bw-guides' ),
			self::read_cap(),
			self::MENU_SLUG,
			array( $this, 'render_browse_page' ),
			'dashicons-book-alt',
			59
		);

		add_submenu_page(
			self::MENU_SLUG,
			__( 'Browse Guides', 'bw-guides' ),
			__( 'Browse', 'bw-guides' ),
			self::read_cap(),
			self::MENU_SLUG,
			array( $this, 'render_browse_page' )
		);

		// The bw_guide CPT registers with show_in_menu=false; link its screens in
		// here so authoring lives under the same Guides menu as reading.
		add_submenu_page(
			self::MENU_SLUG,
			__( 'Add New Guide', 'bw-guides' ),
			__( 'Add New', 'bw-guides' ),
			self::manage_cap(),
			'post-new.php?post_type=' . BW_Guides_CPT::POST_TYPE
		);
		add_submenu_page(
			self::MENU_SLUG,
			__( 'Manage Guides', 'bw-guides' ),
			__( 'Manage', 'bw-guides' ),
			self::manage_cap(),
			'edit.php?post_type=' . BW_Guides_CPT::POST_TYPE
		);
		add_submenu_page(
			self::MENU_SLUG,
			__( 'Guide Tags', 'bw-guides' ),
			__( 'Tags', 'bw-guides' ),
			'manage_categories',
			'edit-tags.php?taxonomy=' . BW_Guides_CPT::TAXONOMY . '&post_type=' . BW_Guides_CPT::POST_TYPE
		);

		$this->settings_hook = add_submenu_page(
			self::MENU_SLUG,
			__( 'Guides Settings', 'bw-guides' ),
			__( 'Settings', 'bw-guides' ),
			'manage_options',
			self::SETTINGS_SLUG,
			array( $this, 'render_settings_page' )
		);
	}

	public function enqueue_assets( $hook ) {
		if ( $hook !== $this->browse_hook && $hook !== $this->settings_hook ) {
			return;
		}

		wp_enqueue_style( 'bw-guides-admin', BW_GUIDES_URL . 'assets/css/bw-guides-admin.css', array(), BW_GUIDES_VERSION );

		if ( $hook === $this->browse_hook ) {
			// Core block styles so hub-authored blocks render correctly in wp-admin.
			wp_enqueue_style( 'wp-block-library' );

			wp_enqueue_script( 'bw-guides-admin', BW_GUIDES_URL . 'assets/js/bw-guides-admin.js', array(), BW_GUIDES_VERSION, true );
			wp_localize_script(
				'bw-guides-admin',
				'bwGuidesAdmin',
				array(
					'ajaxUrl'      => admin_url( 'admin-ajax.php' ),
					'nonce'        => wp_create_nonce( BW_Guides_Ajax::NONCE_ACTION ),
					// A reader can't trigger a sync (it writes), so don't ask them to — the
					// request would 403. Their view still refreshes from whatever the
					// daily cron, or any manage-capable colleague, last pulled in.
					'checkUpdates' => (
						current_user_can( self::manage_cap() )
						&& '' !== BW_Guides_Settings::site_key()
						&& BW_Guides_Sync::needs_check()
					),
					'i18n'         => array(
						'listUpdated'   => __( 'Your guides were just updated.', 'bw-guides' ),
						'guideUpdated'  => __( 'This guide was just updated.', 'bw-guides' ),
						'guideRemoved'  => __( 'This guide is no longer available for this site.', 'bw-guides' ),
						'refresh'       => __( 'Refresh to see the latest', 'bw-guides' ),
						'backToGuides'  => __( 'Back to all guides', 'bw-guides' ),
					),
				)
			);
		}
	}

	public function render_browse_page() {
		if ( ! current_user_can( self::read_cap() ) ) {
			return;
		}

		$guide_id = isset( $_GET['guide'] ) ? absint( $_GET['guide'] ) : 0;
		if ( $guide_id ) {
			$post = get_post( $guide_id );
			if ( $post && BW_Guides_CPT::POST_TYPE === $post->post_type && 'publish' === $post->post_status ) {
				include BW_GUIDES_DIR . 'admin/views/guide-single.php';
				return;
			}
		}

		include BW_GUIDES_DIR . 'admin/views/guides-list.php';
	}

	public function render_settings_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		include BW_GUIDES_DIR . 'admin/views/settings.php';
	}

	public static function guide_url( $guide_id ) {
		return admin_url( 'admin.php?page=' . self::MENU_SLUG . '&guide=' . (int) $guide_id );
	}

	public static function list_url( $args = array() ) {
		return add_query_arg( $args, admin_url( 'admin.php?page=' . self::MENU_SLUG ) );
	}
}
