<?php
/**
 * Admin Module Bootstrap
 *
 * @package AmbassadorHub\Admin
 */

namespace AmbassadorHub\Admin;

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

use AmbassadorHub\Admin\Dashboard\Dashboard;
use AmbassadorHub\Admin\Export\Export;
use AmbassadorHub\Admin\Settings\Manager as SettingsManager;
use AmbassadorHub\Admin\Settings\Registration as SettingsRegistration;
use AmbassadorHub\Admin\Setup\ClearCacheAction;
use AmbassadorHub\Admin\Setup\ClearSpamLogAction;
use AmbassadorHub\Admin\Setup\CreatePagesAction;
use AmbassadorHub\Admin\Setup\SendTestMailAction;
use AmbassadorHub\Admin\Wizard\SetupWizard;

/**
 * Admin module - handles WordPress admin UI components.
 *
 * Manages admin menus, pages, and WordPress admin interface.
 */
final class AdminModule {
	/**
	 * Initialize the Admin module.
	 *
	 * @return void
	 */
	public static function init(): void {
		// Initialize settings registration.
		SettingsRegistration::init();

		// Register admin-post action handlers.
		CreatePagesAction::init();
		ClearCacheAction::init();
		ClearSpamLogAction::init();
		SendTestMailAction::init();
		Export::init();

		// Initialize settings manager.
		SettingsManager::init();

		// Initialize dashboard.
		Dashboard::init();

		// Initialize setup wizard.
		SetupWizard::init();

		add_action( 'admin_menu', array( self::class, 'register_admin_menu' ) );
		add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_admin_assets' ) );
		add_action( 'admin_enqueue_scripts', array( self::class, 'print_menu_icon_style' ) );
		add_filter( 'admin_footer_text', array( self::class, 'filter_footer_text' ) );

		/**
		 * Fires when admin module is initialized.
		 *
		 * @since 0.1.0
		 */
		do_action( 'ambh_admin_module_init' );
	}

	/**
	 * Enqueue admin assets.
	 *
	 * @return void
	 */
	public static function enqueue_admin_assets(): void {
		wp_register_style( 'ambh-admin', AMBASSADOR_HUB_URL . 'assets/css/admin.css', array(), AMBASSADOR_HUB_VERSION );
		wp_register_style( 'ambh-admin-dashboard', AMBASSADOR_HUB_URL . 'assets/css/admin-dashboard.css', array(), AMBASSADOR_HUB_VERSION );

		// Register currency-input utility (enqueued per-screen; see SettingsManager::enqueue_currency_input_assets).
		wp_register_script(
			'ambh-currency-input',
			AMBASSADOR_HUB_URL . 'assets/js/currency-input.js',
			array(),
			AMBASSADOR_HUB_VERSION,
			true
		);
		wp_register_style(
			'ambh-currency-input',
			AMBASSADOR_HUB_URL . 'assets/css/currency-input.css',
			array(),
			AMBASSADOR_HUB_VERSION
		);
	}

	/**
	 * Print a tiny, tightly-scoped inline style fixing the top-level menu icon size.
	 *
	 * WordPress only auto-sizes the built-in dashicon font icons; a custom image
	 * URL passed to add_menu_page() is rendered as a plain <img> at its native
	 * pixel size with no constraint, so without this it renders oversized. Runs
	 * on every admin screen (the sidebar is present everywhere), so the selector
	 * is scoped to this plugin's own top-level menu item only.
	 *
	 * Must be hooked on admin_enqueue_scripts (not admin_head): WordPress prints
	 * all queued styles — including this inline block — via wp_print_styles()
	 * on the admin_print_styles action, which fires before admin_head. Attaching
	 * it any later means wp_add_inline_style() adds to a queue that's already
	 * been printed, so the CSS silently never reaches the page.
	 *
	 * @return void
	 */
	public static function print_menu_icon_style(): void {
		InlineAssets::style( '#toplevel_page_ambassadorhub .wp-menu-image img{width:20px;height:20px;}' );
	}

	/**
	 * Register admin menu.
	 *
	 * @return void
	 */
	public static function register_admin_menu(): void {
		// Add top-level menu (callback is null - no default page).
		add_menu_page(
			__( 'AmbassadorHub', 'ambassadorhub' ),
			__( 'AmbassadorHub', 'ambassadorhub' ),
			'ambh_manage_hub',
			'ambassadorhub',
			array( Dashboard::class, 'render' ),
			AMBASSADOR_HUB_URL . 'assets/images/menu-icon.png',
			30
		);

		// Rename the auto-generated first submenu item from "AmbassadorHub" to "Dashboard".
		remove_submenu_page( 'ambassadorhub', 'ambassadorhub' );
		add_submenu_page(
			'ambassadorhub',
			__( 'Dashboard', 'ambassadorhub' ),
			__( 'Dashboard', 'ambassadorhub' ),
			'ambh_manage_hub',
			'ambassadorhub',
			array( Dashboard::class, 'render' ),
			0
		);

		/**
		 * Fires after admin menu is registered.
		 * Feature plugins can add their own menu items.
		 *
		 * @since 0.1.0
		 */
		do_action( 'ambh_admin_menu_registered' );
	}

	/**
	 * Replace the wp-admin footer text with "Powered by Trybes" on plugin-owned pages.
	 *
	 * Scoped to ?page=ambassadorhub* so it never touches unrelated admin pages.
	 *
	 * @param string $text Default footer text.
	 * @return string
	 */
	public static function filter_footer_text( string $text ): string {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$page = isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : '';
		if ( ! str_starts_with( $page, 'ambassadorhub' ) ) {
			return $text;
		}
		return sprintf(
			/* translators: %s: link to trybes.nl */
			__( 'Powered by %s', 'ambassadorhub' ),
			'<a href="https://trybes.nl" target="_blank" rel="noopener noreferrer">Trybes</a>'
		);
	}

	/**
	 * Register dashboard widgets.
	 *
	 * @return void
	 */
	public static function register_widgets(): void {

		/**
		 * Fires after dashboard widgets are registered.
		 *
		 * @since 0.1.0
		 */
		do_action( 'ambh_register_dashboard_widgets' );
	}
}
