<?php

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

/** Resolves collision actions from the strongest available decision signal. */
class Content_Miner_Collision_Recommendation {
	/** Apply a verdict when present, otherwise use a non-destructive evidence default. @return array<string,mixed> */
	public static function resolve( array $pair ) {
		$reason_suffix = isset( $pair['reason_suffix'] ) ? (string) $pair['reason_suffix'] : '';
		unset( $pair['reason_suffix'] );
		$verdict = self::verdict( $pair );
		if ( '' !== $verdict ) {
			$recommendation = self::from_verdict( $verdict );
		} else {
			$recommendation = self::from_evidence( $pair );
		}
		$recommendation['reason'] .= $reason_suffix;

		return array_merge( $pair, $recommendation );
	}

	/** Return the collision action represented by a supported verdict. @return string */
	public static function verdict_action( array $pair ) {
		$verdict = self::verdict( $pair );
		if ( '' === $verdict ) {
			return '';
		}

		$recommendation = self::from_verdict( $verdict );
		return $recommendation['action'];
	}

	/** @return string */
	private static function verdict( array $pair ) {
		if ( empty( $pair['llm_verdict']['verdict'] ) ) {
			return '';
		}

		$verdict = strtolower( trim( (string) $pair['llm_verdict']['verdict'] ) );
		$verdict = preg_replace( '/[\s_]+/', '-', $verdict );
		return in_array( $verdict, array( 'consolidate', 'differentiate', 'keep-both' ), true ) ? $verdict : '';
	}

	/** @return array<string,string> */
	private static function from_verdict( $verdict ) {
		if ( 'consolidate' === $verdict ) {
			$reason = function_exists( '__' )
				? __( 'The AI review found that these pages serve the same intent and recommends consolidating them.', 'content-miner' )
				: 'The AI review found that these pages serve the same intent and recommends consolidating them.';
			return array(
				'action'     => 'Consolidate',
				'confidence' => 'High',
				'reason'     => $reason,
			);
		}

		if ( 'differentiate' === $verdict ) {
			$reason = function_exists( '__' )
				? __( 'The AI review found distinct intents or services and recommends keeping these pages clearly differentiated.', 'content-miner' )
				: 'The AI review found distinct intents or services and recommends keeping these pages clearly differentiated.';
			return array(
				'action'     => 'Differentiate',
				'confidence' => 'High',
				'reason'     => $reason,
			);
		}

		$reason = function_exists( '__' )
			? __( 'The AI review found that both pages should remain separate.', 'content-miner' )
			: 'The AI review found that both pages should remain separate.';
		return array(
			'action'     => 'Keep',
			'confidence' => 'High',
			'reason'     => $reason,
		);
	}

	/** @return array<string,string> */
	private static function from_evidence( array $pair ) {
		$evidence = strtolower( trim( isset( $pair['evidence'] ) ? (string) $pair['evidence'] : '' ) );
		$reason = trim( isset( $pair['reason'] ) ? (string) $pair['reason'] : '' );
		$exact = ! empty( $pair['exact'] ) || 'exact copy' === $evidence || ( '' === $evidence && 'The normalized body content is identical.' === $reason );
		if ( $exact ) {
			$reason = function_exists( '__' )
				? __( 'The normalized body content is identical.', 'content-miner' )
				: 'The normalized body content is identical.';
			return array(
				'action'     => 'Consolidate',
				'confidence' => 'High',
				'reason'     => $reason,
			);
		}

		if ( 0 === strpos( $evidence, 'semantic similarity ' ) ) {
			$reason = function_exists( '__' )
				? __( 'These pages express similar meaning, but similarity alone does not establish duplicate intent. Keep them separate and clarify their distinct service, audience, or outcome.', 'content-miner' )
				: 'These pages express similar meaning, but similarity alone does not establish duplicate intent. Keep them separate and clarify their distinct service, audience, or outcome.';
		} else {
			$reason = function_exists( '__' )
				? __( 'These pages share meaningful language, but overlap alone does not establish duplicate intent. Keep them separate and clarify their distinct search intents.', 'content-miner' )
				: 'These pages share meaningful language, but overlap alone does not establish duplicate intent. Keep them separate and clarify their distinct search intents.';
		}

		return array(
			'action'     => 'Differentiate',
			'confidence' => 'Medium',
			'reason'     => $reason,
		);
	}
}
