<?php
/**
 * Generated title validation helpers.
 *
 * @package SmartyPress_AI_Engine
 */

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

/**
 * Detects invalid generated titles without trying to identify a language.
 */
class SmartyPress_AI_Content_Generator_Generated_Title_Validator {

	/**
	 * Check if a title is a placeholder or copied from the source topic.
	 *
	 * @param string $title Generated title.
	 * @param string $topic Source topic.
	 * @return bool
	 */
	public function is_placeholder_or_copied_title( $title, $topic ) {
		if ( '' === trim( (string) $title ) ) {
			return false;
		}

		$normalized_title = $this->normalize_for_compare( $title );
		$normalized_topic = $this->normalize_for_compare( $topic );

		if ( false !== strpos( $normalized_title, 'generated post' ) ) {
			return true;
		}

		if ( $normalized_title === $normalized_topic ) {
			return true;
		}

		return $this->starts_with_source_topic_fragment( $normalized_title, $normalized_topic );
	}

	/**
	 * Detect copied source-topic prefixes without relying on a specific alphabet.
	 *
	 * @param string $normalized_title Normalized title.
	 * @param string $normalized_topic Normalized source topic.
	 * @return bool
	 */
	private function starts_with_source_topic_fragment( $normalized_title, $normalized_topic ) {
		$title_tokens = $this->get_compare_tokens( $normalized_title );
		$topic_tokens = $this->get_compare_tokens( $normalized_topic );

		if ( count( $title_tokens ) >= 2 && count( $topic_tokens ) >= 2 ) {
			$prefix_length = min( 3, count( $title_tokens ), count( $topic_tokens ) );
			$title_prefix  = array_slice( $title_tokens, 0, $prefix_length );
			$topic_prefix  = array_slice( $topic_tokens, 0, $prefix_length );

			return $title_prefix === $topic_prefix && count( $title_tokens ) <= ( $prefix_length + 1 );
		}

		if ( '' === $normalized_topic || '' === $normalized_title ) {
			return false;
		}

		$fragment     = function_exists( 'mb_substr' ) ? mb_substr( $normalized_topic, 0, 8 ) : substr( $normalized_topic, 0, 8 );
		$title_length = function_exists( 'mb_strlen' ) ? mb_strlen( $normalized_title ) : strlen( $normalized_title );
		$topic_length = function_exists( 'mb_strlen' ) ? mb_strlen( $normalized_topic ) : strlen( $normalized_topic );

		return '' !== trim( $fragment ) && 0 === strpos( $normalized_title, $fragment ) && $title_length <= $topic_length;
	}

	/**
	 * Split normalized text into comparable words.
	 *
	 * @param string $value Normalized text.
	 * @return array
	 */
	private function get_compare_tokens( $value ) {
		$tokens = preg_split( '/[^\p{L}\p{N}]+/u', (string) $value, -1, PREG_SPLIT_NO_EMPTY );

		return is_array( $tokens ) ? array_values( $tokens ) : array();
	}

	/**
	 * Normalize text for conservative equality checks.
	 *
	 * @param string $value Value.
	 * @return string
	 */
	private function normalize_for_compare( $value ) {
		$value = trim( preg_replace( '/\s+/u', ' ', wp_strip_all_tags( (string) $value ) ) );

		return function_exists( 'mb_strtolower' ) ? mb_strtolower( $value ) : strtolower( $value );
	}
}
