<?php
/**
 * Active consent-platform (CMP) detection.
 *
 * @package Carticy\Conversions\Consent
 */

declare(strict_types=1);

namespace Carticy\Conversions\Consent;

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

/**
 * Detects which consent platform is ACTIVE on the site from signals the server
 * can verify honestly: the WP Consent API surface and known plugin runtime
 * markers (constants/classes). Script-injected CMPs without a WordPress plugin
 * (e.g. a hardcoded OneTrust snippet) are intentionally not guessed here - the
 * browser-side intake still captures their signals at consent time.
 */
final class ConsentPlatformDetector {

	/**
	 * Detect active consent platforms.
	 *
	 * @return array<int, array<string, string>>
	 */
	public function detect(): array {
		$detected = array();

		foreach ( $this->known_platforms() as $platform ) {
			$marker = $platform['marker'];
			$found  = ( 'constant' === $marker['kind'] && defined( $marker['name'] ) )
				|| ( 'class' === $marker['kind'] && class_exists( $marker['name'] ) )
				|| ( 'function' === $marker['kind'] && function_exists( $marker['name'] ) );

			if ( $found ) {
				$detected[] = array(
					'source'  => $platform['source'],
					'label'   => $platform['label'],
					'channel' => $platform['channel'],
				);
			}
		}

		// The WP Consent API layer itself (covers CMPs integrating through it
		// that we do not individually recognize).
		if ( array() === $detected && function_exists( 'wp_has_consent' ) ) {
			$detected[] = array(
				'source'  => 'wp-consent-api',
				'label'   => __( 'WP Consent API platform', 'carticy-conversions-for-woocommerce' ),
				'channel' => 'wp-consent-api',
			);
		}

		return $detected;
	}

	/**
	 * Known CMP plugins and their verifiable runtime markers.
	 *
	 * @return array<int, array{source: string, label: string, channel: string, marker: array{kind: string, name: string}}>
	 */
	private function known_platforms(): array {
		return array(
			array(
				'source'  => 'complianz',
				'label'   => 'Complianz',
				'channel' => 'wp-consent-api',
				'marker'  => array(
					'kind' => 'constant',
					'name' => 'cmplz_version',
				),
			),
			array(
				'source'  => 'cookieyes',
				'label'   => 'CookieYes',
				'channel' => 'wp-consent-api',
				'marker'  => array(
					'kind' => 'constant',
					'name' => 'CLI_SETTINGS_FIELD',
				),
			),
			array(
				'source'  => 'cookiebot',
				'label'   => 'Cookiebot',
				'channel' => 'cookie',
				'marker'  => array(
					'kind' => 'class',
					'name' => 'Cookiebot_WP',
				),
			),
			array(
				'source'  => 'real-cookie-banner',
				'label'   => 'Real Cookie Banner',
				'channel' => 'consent-mode',
				'marker'  => array(
					'kind' => 'constant',
					'name' => 'RCB_FILE',
				),
			),
			array(
				'source'  => 'borlabs-cookie',
				'label'   => 'Borlabs Cookie',
				'channel' => 'consent-mode',
				'marker'  => array(
					'kind' => 'constant',
					'name' => 'BORLABS_COOKIE_VERSION',
				),
			),
			array(
				'source'  => 'wpconsent',
				'label'   => 'WPConsent',
				'channel' => 'wp-consent-api',
				'marker'  => array(
					'kind' => 'function',
					'name' => 'wpconsent',
				),
			),
		);
	}
}
