<?php
/**
 * Query Generator Class
 *
 * Generates realistic search queries based on user behavior patterns.
 *
 * @package MaplePress_SearchSpeedTest
 */

class MPSS_Query_Generator {

	/**
	 * Pre-defined realistic search query templates by category.
	 *
	 * @var array
	 */
	private $query_templates = array(
		// Navigation queries (30%)
		'navigation'      => array(
			'contact',
			'about',
			'home',
			'services',
			'pricing',
			'support',
			'login',
			'register',
			'cart',
			'checkout',
			'account',
			'profile',
			'settings',
			'help',
			'faq',
			'terms',
			'privacy',
			'policy',
			'sitemap',
			'search',
		),

		// Informational queries (25%)
		'informational'   => array(
			'how to',
			'what is',
			'why does',
			'when should',
			'where can',
			'getting started',
			'tutorial',
			'guide',
			'documentation',
			'manual',
			'learn',
			'course',
			'training',
			'video',
			'webinar',
			'ebook',
			'whitepaper',
			'case study',
			'best practices',
			'tips',
		),

		// Product/Service queries (20%)
		'product'         => array(
			'wordpress plugin',
			'theme',
			'template',
			'widget',
			'extension',
			'free shipping',
			'discount',
			'coupon',
			'promo',
			'deal',
			'best seller',
			'popular',
			'recommended',
			'featured',
			'top rated',
			'new arrival',
			'latest',
			'coming soon',
			'pre order',
			'limited',
			'on sale',
			'clearance',
			'bundle',
			'package',
			'subscription',
		),

		// Content discovery queries (15%)
		'content'         => array(
			'blog',
			'post',
			'article',
			'news',
			'announcement',
			'latest',
			'recent',
			'new',
			'updated',
			'archive',
			'category',
			'tag',
			'topic',
			'subject',
			'theme',
			'author',
			'contributor',
			'guest',
			'interview',
			'podcast',
		),

		// Troubleshooting queries (10%)
		'troubleshooting' => array(
			'error',
			'not working',
			'fix',
			'repair',
			'restore',
			'problem',
			'issue',
			'bug',
			'glitch',
			'crash',
			'troubleshoot',
			'diagnose',
			'solve',
			'resolve',
			'debug',
			'broken',
			'missing',
			'invalid',
			'failed',
			'rejected',
		),
	);

	/**
	 * Category weights for distribution.
	 *
	 * @var array
	 */
	private $category_weights = array(
		'navigation'      => 0.30,
		'informational'   => 0.25,
		'product'         => 0.20,
		'content'         => 0.15,
		'troubleshooting' => 0.10,
	);

	/**
	 * Generate query set based on site size profile.
	 *
	 * @param string $profile_key Profile key (tiny, small, medium, big, gigantic).
	 * @param int    $count       Number of queries to generate.
	 * @return array Array of search query strings.
	 */
	public function generate_queries( $profile_key, $count ) {
		$queries = array();

		// Generate queries from each category based on weight
		foreach ( $this->category_weights as $category => $weight ) {
			$category_count   = (int) ( $count * $weight );
			$category_queries = $this->get_random_from_category( $category, $category_count );
			$queries          = array_merge( $queries, $category_queries );
		}

		// Shuffle to mix categories
		shuffle( $queries );

		// Add some variations (typos, mixed case, etc) - 20% of queries
		$queries = $this->add_query_variations( $queries, 0.20 );

		// Ensure we have exactly the requested count
		return array_slice( $queries, 0, $count );
	}

	/**
	 * Get random queries from a specific category.
	 *
	 * @param string $category Category name.
	 * @param int    $count    Number of queries to get.
	 * @return array Array of queries.
	 */
	private function get_random_from_category( $category, $count ) {
		if ( ! isset( $this->query_templates[ $category ] ) ) {
			return array();
		}

		$available = $this->query_templates[ $category ];

		// If we need more queries than available, allow duplicates
		if ( $count > count( $available ) ) {
			$queries = array();
			for ( $i = 0; $i < $count; $i++ ) {
				$queries[] = $available[ array_rand( $available ) ];
			}
			return $queries;
		}

		// Otherwise, shuffle and slice
		shuffle( $available );
		return array_slice( $available, 0, $count );
	}

	/**
	 * Add realistic query variations to the set.
	 *
	 * @param array $queries          Base queries.
	 * @param float $variation_ratio  Ratio of queries to vary (0.0-1.0).
	 * @return array Queries with variations added.
	 */
	private function add_query_variations( $queries, $variation_ratio = 0.20 ) {
		$with_variations = array();

		foreach ( $queries as $query ) {
			// Always add original query
			$with_variations[] = $query;

			// Randomly add variations based on ratio
			if ( mt_rand( 1, 100 ) <= ( $variation_ratio * 100 ) ) {
				$variant = $this->create_variation( $query );
				if ( $variant !== $query ) {
					$with_variations[] = $variant;
				}
			}
		}

		return $with_variations;
	}

	/**
	 * Create a variation of a query (case changes, partial queries, suffixes).
	 *
	 * @param string $query Original query.
	 * @return string Variation of the query.
	 */
	private function create_variation( $query ) {
		$rand = mt_rand( 1, 5 );

		switch ( $rand ) {
			case 1:
				// All uppercase
				return strtoupper( $query );

			case 2:
				// Title case
				return ucwords( $query );

			case 3:
				// Partial query (first word only)
				$words = explode( ' ', $query );
				return $words[0];

			case 4:
				// Add common suffix
				$suffixes = array( ' guide', ' help', ' info', ' page', ' wordpress' );
				return $query . $suffixes[ array_rand( $suffixes ) ];

			case 5:
				// Mixed case (simulate typo)
				$chars  = str_split( $query );
				$result = '';
				foreach ( $chars as $char ) {
					$result .= ( mt_rand( 0, 1 ) === 1 ) ? strtoupper( $char ) : strtolower( $char );
				}
				return $result;
		}

		return $query;
	}
}
