<?php
/**
 * Direct module identity transition guard.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Extension;

/**
 * Rejects ambiguous local development state instead of providing aliases.
 */
final class ModuleIdentityGuard {
	/**
	 * Returns errors for legacy profile hooks, constants, or package manifests.
	 *
	 * @param ModuleDefinition[] $definitions Registered manifests.
	 * @return string[]
	 */
	public function errors( array $definitions ): array {
		$legacy_hooks = array(
			'scheduled_changes_module_distribution_profile',
			'scheduled_changes_module_profile_selection',
		);
		$found        = array();
		$wp_filter    = $GLOBALS['wp_filter'] ?? array();

		foreach ( $legacy_hooks as $hook ) {
			if ( is_array( $wp_filter ) && array_key_exists( $hook, $wp_filter ) ) {
				$found[] = $hook;
			}
		}

		if ( defined( 'SCHEDULED_CHANGES_MODULE_PROFILE' ) ) {
			$found[] = 'SCHEDULED_CHANGES_MODULE_PROFILE';
		}

		foreach ( $definitions as $definition ) {
			if ( 'scheduled-changes-for-woocommerce' === $definition->slug() || 'scheduled-changes-for-woocommerce' === $definition->sourcePackage() ) {
				$found[] = $definition->slug();
			}
		}

		if ( array() === $found ) {
			return array();
		}

		return array(
			'Mixed module identity state detected (' . implode( ', ', array_unique( $found ) ) . '). Remove legacy local MU-profile/bootstrap files, reset local development data if needed, and rebuild artifacts. Compatibility aliases are not supported.',
		);
	}
}
