<?php

declare( strict_types=1 );

namespace StrykeWP\Modules\Branding;

use StrykeWP\Modules\AbstractModule;

/**
 * STRYKE Marketing Branding Module.
 *
 * White-Label WordPress-Admin: custom navigation shell (topbar + sidebar),
 * dark/light theme toggle, branded login page, support center page.
 * Migrated from stryke-branding v2.0.0 (BrandingLoader → BrandingModule).
 *
 * Asset path: stryke-wp/src/Modules/Branding/assets/
 *
 * @package StrykeWP\Modules\Branding
 */
class BrandingModule extends AbstractModule {

	private ThemePreference $theme_pref;
	private MenuManager     $menu_manager;
	private AdminShell      $admin_shell;
	private AdminStyles     $admin_styles;
	private LoginPage       $login_page;

	// -----------------------------------------------------------------------
	// ModuleInterface metadata
	// -----------------------------------------------------------------------

	public function get_id(): string {
		return 'branding';
	}

	public function get_name(): string {
		return __( 'Admin Branding', 'stryke-wp' );
	}

	public function get_description(): string {
		return __( 'White-Label Admin-Oberfläche — eigene Navigation, Dark-Theme, gebrandete Login-Seite und Support-Center für STRYKE-Kunden.', 'stryke-wp' );
	}

	public function get_version(): string {
		return '2.0.0';
	}

	public function get_icon(): string {
		return 'dashicons-art';
	}

	// -----------------------------------------------------------------------
	// Boot
	// -----------------------------------------------------------------------

	/**
	 * Instantiates Branding services and registers WordPress hooks.
	 *
	 * @return void
	 */
	protected function boot_module(): void {
		$assets_url = STRYKE_WP_URL . 'src/Modules/Branding/assets/';
		$assets_dir = STRYKE_WP_DIR . 'src/Modules/Branding/assets/';

		$this->theme_pref   = new ThemePreference();
		$this->menu_manager = new MenuManager();
		$this->admin_shell  = new AdminShell( $this->theme_pref, $assets_dir );
		$this->admin_styles = new AdminStyles( $this->theme_pref, $assets_url );
		$this->login_page   = new LoginPage( $assets_url, $assets_dir );

		// Theme preference (AJAX, color scheme) — runs everywhere.
		$this->theme_pref->register_hooks();

		// Admin-only hooks.
		if ( is_admin() ) {
			$this->menu_manager->register_hooks();
			$this->admin_shell->register_hooks();
			$this->admin_styles->register_hooks();
			add_action( 'admin_menu', array( $this, 'register_support_page' ) );
		}

		// Login page hooks.
		$this->login_page->register_hooks();

		$this->logger->info( 'BrandingModule booted.' );
	}

	// -----------------------------------------------------------------------
	// Support Page
	// -----------------------------------------------------------------------

	/**
	 * Registers the STRYKE Support page as a submenu under stryke-wp.
	 * Hooked to admin_menu from boot_module().
	 *
	 * @return void
	 */
	public function register_support_page(): void {
		add_submenu_page(
			'stryke-wp',
			__( 'STRYKE Support', 'stryke-wp' ),
			__( 'Support', 'stryke-wp' ),
			'read',
			'stryke-wp-support',
			array( $this, 'render_support_page' )
		);
	}

	/**
	 * Renders the STRYKE Support page content.
	 *
	 * @return void
	 */
	public function render_support_page(): void {
		$contact_whatsapp = '+49 163 7298401';
		$contact_mobile   = '+49 163 7298401';
		$website_url      = 'https://faq.strykemarketing.de';
		?>
		<div class="stryke-support-page">

			<div class="stryke-page-header">
				<h1><?php esc_html_e( 'STRYKE Marketing Support Center', 'stryke-wp' ); ?></h1>
				<p class="stryke-text-muted"><?php esc_html_e( 'Wählen Sie bitte den für Sie passendsten Kontaktweg.', 'stryke-wp' ); ?></p>
			</div>

			<div class="stryke-grid stryke-grid--3">

				<div class="stryke-card">
					<div class="stryke-card__icon">
						<span class="dashicons dashicons-whatsapp" aria-hidden="true"></span>
					</div>
					<h2><?php esc_html_e( 'WhatsApp Support', 'stryke-wp' ); ?></h2>
					<p><?php esc_html_e( 'Der schnellste Weg für kurze Fragen und Abstimmungen.', 'stryke-wp' ); ?></p>
					<a href="https://wa.me/<?php echo esc_attr( preg_replace( '/[^0-9+]/', '', $contact_whatsapp ) ); ?>"
					   target="_blank" rel="noopener noreferrer"
					   class="stryke-btn stryke-btn--primary">
						<?php esc_html_e( 'Chat starten', 'stryke-wp' ); ?>
					</a>
					<p class="stryke-text-muted"><?php echo esc_html( $contact_whatsapp ); ?></p>
				</div>

				<div class="stryke-card">
					<div class="stryke-card__icon">
						<span class="dashicons dashicons-phone" aria-hidden="true"></span>
					</div>
					<h2><?php esc_html_e( 'Telefonische Hotline', 'stryke-wp' ); ?></h2>
					<p><?php esc_html_e( 'Für detaillierte Besprechungen und dringende Anfragen.', 'stryke-wp' ); ?></p>
					<a href="tel:<?php echo esc_attr( preg_replace( '/[^0-9+]/', '', $contact_mobile ) ); ?>"
					   class="stryke-btn stryke-btn--primary">
						<?php esc_html_e( 'Jetzt anrufen', 'stryke-wp' ); ?>
					</a>
					<p class="stryke-text-muted"><?php echo esc_html( $contact_mobile ); ?></p>
				</div>

				<div class="stryke-card">
					<div class="stryke-card__icon">
						<span class="dashicons dashicons-external" aria-hidden="true"></span>
					</div>
					<h2><?php esc_html_e( 'Website &amp; Docs', 'stryke-wp' ); ?></h2>
					<p><?php esc_html_e( 'FAQ-Seite mit Antworten auf häufig gestellte Fragen.', 'stryke-wp' ); ?></p>
					<a href="<?php echo esc_url( $website_url ); ?>"
					   target="_blank" rel="noopener noreferrer"
					   class="stryke-btn stryke-btn--primary">
						<?php esc_html_e( 'Zur FAQ-Seite', 'stryke-wp' ); ?>
					</a>
					<p class="stryke-text-muted"><?php echo esc_html( wp_parse_url( $website_url, PHP_URL_HOST ) ); ?></p>
				</div>

			</div>

			<p class="stryke-text-muted" style="margin-top: 24px;">
				<?php
				printf(
					/* translators: %s: plugin version */
					esc_html__( 'STRYKE WP v%s', 'stryke-wp' ),
					esc_html( STRYKE_WP_VERSION )
				);
				?>
			</p>

		</div>
		<?php
	}

	// -----------------------------------------------------------------------
	// Lifecycle
	// -----------------------------------------------------------------------

	/**
	 * Runs when the module is first enabled.
	 * Sets STRYKE dark color scheme for all existing users.
	 *
	 * @return void
	 */
	public function on_enable(): void {
		$offset     = 0;
		$batch_size = 100;
		while ( true ) {
			$user_ids = get_users( array(
				'fields' => 'ID',
				'number' => $batch_size,
				'offset' => $offset,
			) );
			if ( empty( $user_ids ) ) {
				break;
			}
			foreach ( $user_ids as $user_id ) {
				update_user_meta( (int) $user_id, 'admin_color', 'stryke-dark' );
			}
			$offset += $batch_size;
		}
	}

	/**
	 * Runs when the module is disabled.
	 * Restores default WP color scheme for all users.
	 *
	 * @return void
	 */
	public function on_disable(): void {
		$offset     = 0;
		$batch_size = 100;
		while ( true ) {
			$user_ids = get_users( array(
				'fields' => 'ID',
				'number' => $batch_size,
				'offset' => $offset,
			) );
			if ( empty( $user_ids ) ) {
				break;
			}
			foreach ( $user_ids as $user_id ) {
				update_user_meta( (int) $user_id, 'admin_color', 'fresh' );
			}
			$offset += $batch_size;
		}
	}

	// -----------------------------------------------------------------------
	// Settings UI
	// -----------------------------------------------------------------------

	/**
	 * Renders Branding module settings inside the STRYKE WP dashboard.
	 * Branding has no user-configurable settings; shows status info.
	 *
	 * @return void
	 */
	public function render_settings(): void {
		?>
		<div class="stryke-card">
			<h2><?php esc_html_e( 'Admin Branding', 'stryke-wp' ); ?></h2>
			<div class="stryke-notice stryke-notice--success">
				<span class="dashicons dashicons-yes-alt" aria-hidden="true"></span>
				<?php esc_html_e( 'Admin Branding ist aktiv. Eigene Navigation, Dark-Theme und gebrandete Login-Seite sind aktiviert.', 'stryke-wp' ); ?>
			</div>
			<p class="stryke-text-muted" style="margin-top: 12px;">
				<?php esc_html_e( 'Dieses Modul hat keine weiteren Einstellungen. Zum Support-Center:', 'stryke-wp' ); ?>
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=stryke-wp-support' ) ); ?>">
					<?php esc_html_e( 'STRYKE Support öffnen', 'stryke-wp' ); ?>
				</a>
			</p>
		</div>
		<?php
	}
}
