<?php
/**
 * @package Hafenstudios\Wellenbrecher
 */

namespace Hafenstudios\Wellenbrecher\Admin;

defined( 'ABSPATH' ) || exit;

/**
 * Top-Level-Menue "Wellenbrecher" (Cap hs_manage_spam, dashicons-shield,
 * Position bei den Flotten-Geschwistern). Hauptseite ist seit W3 die
 * Uebersicht; darunter Quarantaene, Block-Log und Einstellungen. Muster
 * leadlotse/src/Admin/Menu.php.
 */
final class Menu {

	public const MENU_SLUG = OverviewPage::SLUG;

	private OverviewPage $overview_page;

	private QuarantinePage $quarantine_page;

	private LogPage $log_page;

	private SettingsPage $settings_page;

	/**
	 * Externe Dienste (Pro, W5b). Null = Menuepunkt entfaellt (Free/Pro-Gate aus).
	 */
	private ?ExternaPage $externa_page;

	/**
	 * @param OverviewPage      $overview_page   Uebersicht (Hauptseite).
	 * @param QuarantinePage    $quarantine_page Formular-Quarantaene.
	 * @param LogPage           $log_page        Block-Log.
	 * @param SettingsPage      $settings_page   Einstellungen.
	 * @param ExternaPage|null  $externa_page    Externe Dienste (nur bei Pro).
	 */
	public function __construct( OverviewPage $overview_page, QuarantinePage $quarantine_page, LogPage $log_page, SettingsPage $settings_page, ?ExternaPage $externa_page = null ) {
		$this->overview_page   = $overview_page;
		$this->quarantine_page = $quarantine_page;
		$this->log_page        = $log_page;
		$this->settings_page   = $settings_page;
		$this->externa_page    = $externa_page;
	}

	/**
	 * Registriert die Admin-Hooks.
	 *
	 * @return void
	 */
	public function register(): void {
		add_action( 'admin_menu', array( $this, 'register_menu' ) );
	}

	/**
	 * Baut das Menue auf.
	 *
	 * @return void
	 */
	public function register_menu(): void {
		add_menu_page(
			__( 'Wellenbrecher', 'wellenbrecher' ),
			__( 'Wellenbrecher', 'wellenbrecher' ),
			'hs_manage_spam',
			self::MENU_SLUG,
			array( $this->overview_page, 'render' ),
			'dashicons-shield',
			59
		);

		add_submenu_page(
			self::MENU_SLUG,
			__( 'Uebersicht', 'wellenbrecher' ),
			__( 'Uebersicht', 'wellenbrecher' ),
			'hs_manage_spam',
			self::MENU_SLUG,
			array( $this->overview_page, 'render' )
		);

		add_submenu_page(
			self::MENU_SLUG,
			__( 'Quarantaene', 'wellenbrecher' ),
			__( 'Quarantaene', 'wellenbrecher' ),
			'hs_manage_spam',
			QuarantinePage::SLUG,
			array( $this->quarantine_page, 'render' )
		);

		add_submenu_page(
			self::MENU_SLUG,
			__( 'Block-Log', 'wellenbrecher' ),
			__( 'Block-Log', 'wellenbrecher' ),
			'hs_manage_spam',
			LogPage::SLUG,
			array( $this->log_page, 'render' )
		);

		add_submenu_page(
			self::MENU_SLUG,
			__( 'Einstellungen', 'wellenbrecher' ),
			__( 'Einstellungen', 'wellenbrecher' ),
			'hs_manage_spam',
			SettingsPage::SLUG,
			array( $this->settings_page, 'render' )
		);

		// Externe Dienste nur bei aktivem Pro-Gate (Opt-in-Externa, W5b).
		if ( null !== $this->externa_page ) {
			add_submenu_page(
				self::MENU_SLUG,
				__( 'Externe Dienste', 'wellenbrecher' ),
				__( 'Externe Dienste', 'wellenbrecher' ),
				'hs_manage_spam',
				ExternaPage::SLUG,
				array( $this->externa_page, 'render' )
			);
		}
	}
}
