<?php
/**
 * SEO Analysis prompt builders.
 *
 * @package SmartyPress_AI_Engine
 */

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

/**
 * Builds Sonar prompts for competitor research.
 */
class SmartyPress_AI_SEO_Analysis_Prompts {

	/**
	 * Plugin settings.
	 *
	 * @var array
	 */
	private $settings;

	/**
	 * Constructor.
	 *
	 * @param array $settings Plugin settings.
	 */
	public function __construct( array $settings = array() ) {
		$this->settings = $settings;
	}

	/**
	 * Build system prompt for competitor search.
	 *
	 * @param string $language Target language.
	 * @return string
	 */
	public function build_competitor_search_system_prompt( $language ) {
		return sprintf(
			/* translators: %s: language name. */
			__( 'You are a competitor research assistant. Search the web and return concise structured SEO findings in %s. Return valid JSON only, without Markdown or prose.', 'smartypress-ai-engine' ),
			$language ?: $this->get_default_language()
		);
	}

	/**
	 * Build compact URL search prompt for Sonar.
	 *
	 * @param string $keyword  Focus keyword.
	 * @param string $language Target language.
	 * @param int    $limit    Competitor limit.
	 * @return string
	 */
	public function build_competitor_url_search_prompt( $keyword, $language, $limit ) {
		$language = $language ?: $this->get_default_language();

		return sprintf(
			/* translators: 1: result limit, 2: keyword, 3: language. */
			__( 'Find up to the top %1$d Google organic result URLs for the search query "%2$s". Treat the query as the user search intent: use the exact phrase when natural, but also accept close word forms, reordered words, and common language-specific variants that clearly satisfy the same intent. Search only in %3$s and prefer pages written in %3$s. Prioritize real competing pages with useful long-form content: articles, reviews, listicles, guides, comparisons, tutorials, or long-form landing pages. Exclude app stores, social profiles, forums/Q&A, marketplaces, tag/archive pages, profile pages, PDFs, ads, internal search pages, and thin pages. Do not extract headings in this step. Return compact JSON only with this schema: {"competitors":[{"searchPosition":1,"url":"","title":"","snippet":"","pageType":"","relevanceScore":0,"matchReason":""}]} . Do not invent URLs. Do not include Markdown, comments, trailing commas, or text outside JSON.', 'smartypress-ai-engine' ),
			$limit,
			$keyword,
			$language
		);
	}

	/**
	 * Build prompt for analysis of a single competitor URL.
	 *
	 * @param string $keyword    Focus keyword.
	 * @param string $language   Target language.
	 * @param array  $competitor Competitor URL metadata.
	 * @return string
	 */
	public function build_competitor_page_analysis_prompt( $keyword, $language, array $competitor ) {
		$language = $language ?: $this->get_default_language();

		return sprintf(
			/* translators: 1: URL, 2: title, 3: keyword, 4: language. */
			__( 'Analyze this exact competitor page: URL "%1$s"; title "%2$s"; target query "%3$s"; language "%4$s". Open and inspect only this page. Extract the real visible H1, H2, and H3 headings in page order. Also extract SEO keywords or phrases used on the page, matchedTerms, intent, contentAngle, questions answered by the page, and contentGaps that our article can cover better. If the page cannot be opened or headings cannot be found, return analysisStatus "failed" or "partial" with analysisError. Return JSON only with this schema: {"analysisStatus":"completed","analysisError":"","intent":"","contentAngle":"","matchedTerms":[],"headings":[{"level":"h1","text":"","purpose":""}],"keywords":[],"questions":[],"contentGaps":[]} . Do not analyze other pages. Do not include Markdown, comments, trailing commas, or text outside JSON.', 'smartypress-ai-engine' ),
			esc_url_raw( $competitor['url'] ?? '' ),
			sanitize_text_field( $competitor['title'] ?? '' ),
			$keyword,
			$language
		);
	}

	/**
	 * Get default language name.
	 *
	 * @return string
	 */
	private function get_default_language() {
		if ( ! empty( $this->settings['default_language'] ) ) {
			return sanitize_text_field( $this->settings['default_language'] );
		}

		return function_exists( 'smartypress_ai_get_language_by_locale' )
			? smartypress_ai_get_language_by_locale()
			: 'English';
	}
}
