<?php
/**
 * WPFAQBlock Admin
 *
 * Handles registration of admin menus, asset enqueueing, and page rendering.
 *
 * Menu structure:
 *   WPFAQBlock  (top-level)
 *   ├── FAQ Builder      → custom builder page (default)
 *   ├── All FAQs         → WP default CPT list table
 *   ├── Add New FAQ      → WP default new-post screen
 *   ├── FAQ Categories       → WP default taxonomy screen
 *   └── FAQ Tags         → WP default taxonomy screen (flat)
 *
 * @package WPFAQBlock
 * @since   2.0.0
 */

// Prevent direct file access.
if (!defined('ABSPATH')) {
	exit;
}

/**
 * Class WPFAQBlock_Admin
 */
class WPFAQBlock_Admin
{

	/**
	 * Required capability to access the plugin menus.
	 *
	 * @since 2.0.0
	 * @var   string
	 */
	private string $capability = 'manage_options';

	/**
	 * Top-level menu slug — also the FAQ Builder page slug.
	 *
	 * @since 2.0.0
	 * @var   string
	 */
	private string $menu_slug = 'wpfaqblock';

	/**
	 * The screen hook suffix returned by add_menu_page().
	 * Used to scope asset enqueueing to the builder page only.
	 *
	 * @since 2.0.0
	 * @var   string
	 */
	private string $builder_hook = '';

	// ──────────────────────────────────────────────
	// Hook Callbacks
	// ──────────────────────────────────────────────

	/**
	 * Register top-level menu and all submenus.
	 *
	 * Hooked into: admin_menu
	 *
	 * @since 2.0.0
	 * @return void
	 */
	public function register_menus(): void
	{
		// ── Top-level menu ──────────────────────────────────────────────────
		$this->builder_hook = add_menu_page(
			__('WPFAQBlock', 'wpfaqblock'),
			__('WPFAQBlock', 'wpfaqblock'),
			$this->capability,
			$this->menu_slug,
			array($this, 'render_builder_page'),
			WPFAQBLOCK_URL . 'assets/images/menuicon.svg',
			25
		);

		// ── Submenu 1: WPFAQBlock (replaces auto-generated duplicate) ──────
		add_submenu_page(
			$this->menu_slug,
			__('Dashboard', 'wpfaqblock'),
			__('Dashboard', 'wpfaqblock'),
			$this->capability,
			$this->menu_slug,
			array($this, 'render_builder_page')
		);

		// Enqueue builder assets only on the builder screen.
		add_action('admin_enqueue_scripts', array($this, 'enqueue_builder_assets'));

		// Fix menu icon size globally in admin.
		add_action('admin_head', array($this, 'fix_menu_icon_style'));

		// Keep main menu highlighted when on FAQ-related screens.
		add_filter('parent_file', array($this, 'highlight_parent_menu'));
	}

	/**
	 * Force the main WPFAQBlock menu to stay active on related post type screens.
	 *
	 * @param string $parent_file The parent file for the current page.
	 * @return string
	 */
	public function highlight_parent_menu(string $parent_file): string
	{
		global $current_screen;

		if (isset($current_screen->post_type) && WPFAQBlock_CPT::SLUG === $current_screen->post_type) {
			return $this->menu_slug;
		}

		return $parent_file;
	}

	/**
	 * Fix the custom SVG menu icon size in the sidebar.
	 *
	 * @since 2.0.0
	 */
	public function fix_menu_icon_style(): void
	{
		echo '<style>
			#adminmenu #toplevel_page_wpfaqblock .wp-menu-image img {
				width: 20px !important;
				height: 20px !important;
				padding: 7px 0 !important;
				object-fit: contain !important;
				opacity: 0.85;
			}
			#adminmenu #toplevel_page_wpfaqblock.current .wp-menu-image img,
			#adminmenu #toplevel_page_wpfaqblock:hover .wp-menu-image img {
				opacity: 1;
			}
			#adminmenu #toplevel_page_wpfaqblock.wp-has-current-submenu a.wp-has-current-submenu, 
			#adminmenu #toplevel_page_wpfaqblock > a.current {
				background-color: #FE8512 !important;
			}
		</style>';
	}

	/**
	 * Enqueue CSS and JS only on the FAQ Builder admin page.
	 *
	 * Hooked into: admin_enqueue_scripts
	 *
	 * @since 2.0.0
	 *
	 * @param string $hook_suffix The current admin page hook suffix.
	 * @return void
	 */
	public function enqueue_builder_assets(string $hook_suffix): void
	{
		// Enqueue on builder page and settings page.
		$settings_page = 'wpfaqblock_page_wpfaqblock-settings';

		if ($hook_suffix !== $this->builder_hook && $hook_suffix !== $settings_page) {
			return;
		}

		// ── Builder CSS ──────────────────────────────────────────────────────
		wp_enqueue_style(
			'wpfaqblock-admin-builder',
			WPFAQBLOCK_URL . 'assets/css/admin-builder.css',
			array(),
			file_exists(WPFAQBLOCK_DIR . 'assets/css/admin-builder.css') ? filemtime(WPFAQBLOCK_DIR . 'assets/css/admin-builder.css') : WPFAQBLOCK_VERSION
		);

		// ── Builder JS ───────────────────────────────────────────────────────
		wp_enqueue_script(
			'wpfaqblock-admin-builder',
			WPFAQBLOCK_URL . 'assets/js/admin-builder.js',
			array(),
			file_exists(WPFAQBLOCK_DIR . 'assets/js/admin-builder.js') ? filemtime(WPFAQBLOCK_DIR . 'assets/js/admin-builder.js') : WPFAQBLOCK_VERSION,
			true
		);

		wp_enqueue_script(
			'wpfaqblock-admin-dragdrop',
			WPFAQBLOCK_URL . 'assets/js/admin-dragdrop.js',
			array('wpfaqblock-admin-builder'),
			file_exists(WPFAQBLOCK_DIR . 'assets/js/admin-dragdrop.js') ? filemtime(WPFAQBLOCK_DIR . 'assets/js/admin-dragdrop.js') : WPFAQBLOCK_VERSION,
			true
		);

		wp_enqueue_script(
			'wpfaqblock-admin-ai',
			WPFAQBLOCK_URL . 'assets/js/admin-ai.js',
			array('wpfaqblock-admin-builder'),
			file_exists(WPFAQBLOCK_DIR . 'assets/js/admin-ai.js') ? filemtime(WPFAQBLOCK_DIR . 'assets/js/admin-ai.js') : WPFAQBLOCK_VERSION,
			true
		);

		// ── Localised data (used from Step 5 onward) ─────────────────────────
		wp_localize_script(
			'wpfaqblock-admin-builder',
			'wpfaqblockData',
			array(
				'ajaxUrl' => admin_url('admin-ajax.php'),
				'nonce'   => wp_create_nonce('wpfaqblock_builder_nonce'),
				'cptSlug' => WPFAQBlock_CPT::SLUG,
				'i18n'    => array(
					'saving'        => __('Saving…', 'wpfaqblock'),
					'saved'         => __('Saved!', 'wpfaqblock'),
					'error'         => __('Something went wrong. Please try again.', 'wpfaqblock'),
					'confirmDelete' => __('Are you sure you want to delete this FAQ?', 'wpfaqblock'),
					'noTitle'       => __('Question cannot be empty.', 'wpfaqblock'),
				),
			)
		);
	}

	// ──────────────────────────────────────────────
	// Page Renderers
	// ──────────────────────────────────────────────

	/**
	 * Render the FAQ Builder admin page.
	 *
	 * @since 2.0.0
	 * @return void
	 */
	public function render_builder_page(): void
	{
		if (!current_user_can($this->capability)) {
			wp_die(esc_html__('You do not have permission to access this page.', 'wpfaqblock'));
		}

		require_once WPFAQBLOCK_DIR . 'admin/builder-page.php';
	}

	/**
	 * Remove 'View' action link for FAQs since they are not viewed individually.
	 *
	 * Hooked into: post_row_actions
	 *
	 * @since 2.0.0
	 * @param array   $actions Array of row action links.
	 * @param WP_Post $post    The post object.
	 * @return array
	 */
	/**
	 * Remove 'View' action link for FAQs since they are not viewed individually.
	 *
	 * Hooked into: post_row_actions
	 *
	 * @since 2.0.0
	 * @param array   $actions Array of row action links.
	 * @param WP_Post $post    The post object.
	 * @return array
	 */
	public function remove_view_action(array $actions, WP_Post $post): array
	{
		if (WPFAQBlock_CPT::SLUG === $post->post_type) {
			unset($actions['view']);
		}
		return $actions;
	}

}
