<?php

declare( strict_types=1 );

namespace StrykeWP\Modules\WipMode;

use StrykeWP\Modules\AbstractModule;

/**
 * STRYKE WIP Mode Module.
 *
 * Zeigt nicht eingeloggten Besuchern eine gebrandete Wartungsseite an.
 * Migrated from stryke-wip v1.6 (procedural → OOP).
 *
 * Color fix applied: #27B9C6 → #00BCD4 (STRYKE brand primary).
 * Options migrated: stryke_wip_active / stryke_wip_allowed_pages_ids
 *   → stryke_wp_module_wip_mode_active / stryke_wp_module_wip_mode_allowed_pages
 *
 * @package StrykeWP\Modules\WipMode
 */
class WipModeModule extends AbstractModule {

	private WipController $controller;
	private WipAdmin      $admin;

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

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

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

	public function get_description(): string {
		return __( 'Zeigt nicht eingeloggten Besuchern eine gebrandete Wartungsseite an. Mit konfigurierbarer Seiten-Whitelist.', 'stryke-wp' );
	}

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

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

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

	/**
	 * Instantiates WIP Mode services and registers WordPress hooks.
	 *
	 * @return void
	 */
	protected function boot_module(): void {

		$this->controller = new WipController( $this->options );
		$this->admin      = new WipAdmin( $this->options, $this->controller );

		// Frontend hook — intercept page loads to show WIP page.
		add_action( 'template_redirect', array( $this->controller, 'maybe_show_wip_page' ) );

		// Admin-only hooks.
		if ( is_admin() ) {
			$this->admin->register_hooks();
		}

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

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

	/**
	 * Runs when the module is first enabled.
	 * Migrates legacy options and sets safe defaults.
	 *
	 * @return void
	 */
	public function on_enable(): void {
		// Migrate active status from legacy option if new option doesn't exist yet.
		if ( null === $this->options->module_get( 'wip_mode', 'active', null ) ) {
			$legacy_active = get_option( 'stryke_wip_active', null );
			$this->options->module_set(
				'wip_mode',
				'active',
				null !== $legacy_active ? ( 'active' === $legacy_active ) : false
			);
		}

		// Migrate allowed pages from legacy option.
		if ( null === $this->options->module_get( 'wip_mode', 'allowed_pages', null ) ) {
			$legacy_pages = get_option( 'stryke_wip_allowed_pages_ids', null );
			$this->options->module_set(
				'wip_mode',
				'allowed_pages',
				( null !== $legacy_pages && is_array( $legacy_pages ) )
					? array_values( array_map( 'absint', $legacy_pages ) )
					: array()
			);
		}
	}

	/**
	 * Runs when the module is disabled.
	 * No cleanup required — template_redirect won't fire when disabled.
	 *
	 * @return void
	 */
	public function on_disable(): void {}

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

	/**
	 * Renders WIP Mode settings inside the STRYKE WP dashboard.
	 *
	 * @return void
	 */
	public function render_settings(): void {
		if ( isset( $this->admin ) ) {
			$this->admin->render_settings();
		}
	}
}
