<?php
/**
 * Legacy bulk prompt service.
 *
 * @package SmartyPress_AI_Engine
 */

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

/**
 * Builds and parses legacy text prompts used by bulk generation.
 */
class SmartyPress_AI_Content_Generator_Bulk_Prompt_Service {

	/**
	 * Get default legacy bulk prompts.
	 *
	 * @return array
	 */
	public function get_default_prompts() {
		return array(
			'title'   => 'Write a title for an article in {LANGUAGE}. Must be between 40 and 60 characters. Write naturally as a human would. Include the main topic phrase only after translating/adapting it into {LANGUAGE}. Output only the title, no formatting, no Markdown, no special characters. If TOPIC or CONTEXT is written in another language, translate/adapt the meaning and output the title only in {LANGUAGE}. Do not copy the source-language topic verbatim.
IMPORTANT: Wrap your response with ===CONTENT_START=== and ===CONTENT_END=== markers.

### TOPIC:

{TOPIC}

### CONTEXT:

{CONTEXT}

Generate a title based on the topic above, taking into account the provided context.',

			'sections' => 'Write {SECTIONS_COUNT} consecutive headings for an article about "{TITLE}", in {LANGUAGE}. Each heading is between 40 and 60 characters. Format each heading with Markdown (## ). Write naturally as a human would. Include the main topic phrase only after translating/adapting it into {LANGUAGE}. Output only the headings, nothing else. If TOPIC, TITLE, or CONTEXT is written in another language, translate/adapt the meaning and output headings only in {LANGUAGE}.
IMPORTANT: Wrap your response with ===CONTENT_START=== and ===CONTENT_END=== markers.

### TOPIC:

{TOPIC}

### CONTEXT:

{CONTEXT}

Create headings that align with both the topic and context provided above.',

			'content' => 'Write an article about "{TITLE}" in {LANGUAGE}. Use clean Markdown only: ## or ### headings, short paragraphs, bullet lists for enumerations and tips, **bold** for key terms, and *italic* only when useful. Do not use blockquotes, raw HTML, CSS, or unsupported markup. If HTML appears in this prompt, return the result in pure Markdown. Include an introduction at the beginning and a conclusion at the end. Write naturally as a human would. SEO requirements: write at least 600 words, use the translated/adapted main topic phrase in the introduction, in one ## or ### heading, and naturally in several paragraphs without keyword stuffing. Do not add a sales CTA or CTA block at the end. If TITLE, TOPIC, SECTIONS, or CONTEXT is written in another language, translate/adapt the meaning and output the whole article only in {LANGUAGE}.
IMPORTANT: Wrap your response with ===CONTENT_START=== and ===CONTENT_END=== markers. Return content in Markdown only.

### ARTICLE STRUCTURE:

{SECTIONS}

### TOPIC DETAILS:

{TOPIC}

### WRITING CONTEXT:

{CONTEXT}

Write the article following the structure above, incorporating the topic details while adhering to the context guidelines.',

			'excerpt' => 'Write an excerpt for an article in {LANGUAGE}. Must be between 40 and 60 characters. Write naturally as a human would. Output only the excerpt, no formatting. If ARTICLE TITLE, TOPIC, CONTENT, or CONTEXT is written in another language, translate/adapt the meaning and output the excerpt only in {LANGUAGE}.
IMPORTANT: Wrap your response with ===CONTENT_START=== and ===CONTENT_END=== markers.

### ARTICLE TITLE:

"{TITLE}"

### TOPIC:

{TOPIC}

### CONTEXT:

{CONTEXT}

Create a compelling excerpt that captures the essence of the article while considering the context.',
		);
	}

	/**
	 * Build fallback full-post prompt for bulk generation.
	 *
	 * @param array $args Prompt arguments.
	 * @return string
	 */
	public function build_full_post_prompt( $args ) {
		$topic      = sanitize_text_field( $args['topic'] ?? '' );
		$language   = sanitize_text_field( $args['language'] ?? 'Russian' );
		$tone       = sanitize_text_field( $args['writing_tone'] ?? 'neutral' );
		$style      = sanitize_text_field( $args['writing_style'] ?? 'informative' );
		$sections   = absint( $args['sections'] ?? 5 );
		$paragraphs = absint( $args['paragraphs_per_section'] ?? 2 );

		return sprintf(
			"Create a complete WordPress article in %s.\n\nTopic: %s\nTone: %s\nStyle: %s\nSections: %d\nParagraphs per section: %d\n\nReturn the response strictly in this format:\n===TITLE_START===\nPost title\n===TITLE_END===\n===CONTENT_START===\nMarkdown content with H2 headings\n===CONTENT_END===\n===EXCERPT_START===\nShort excerpt\n===EXCERPT_END===\n===TAGS_START===\ntag one, tag two\n===TAGS_END===",
			$language,
			$topic,
			$tone,
			$style,
			max( 1, $sections ),
			max( 1, $paragraphs )
		);
	}

	/**
	 * Build a safe title fallback.
	 *
	 * @param string $topic Topic.
	 * @return string
	 */
	public function get_fallback_title( $topic ) {
		$title = sanitize_text_field( $topic );

		return '' !== $title ? $title : __( 'Generated Post', 'smartypress-ai-engine' );
	}
}
