<?php
/**
 * Content Length Preset System
 * 
 * Converts user-friendly length presets into LLM-understandable
 * structural instructions (sections, paragraphs, word counts).
 *
 * @package AIPT\Utils
 * @since 2.6.0
 */

namespace AIPT\Utils;

class ContentLengthPreset {

	/**
	 * Preset definitions for full descriptions
	 * Simplified in v2.6.0 - removed over-prescriptive section structures
	 */
	private const FULL_PRESETS = array(
		'short'     => array(
			'label'           => 'Short',
			'description'     => '2-3 paragraphs, quick overview',
			'min_words'       => 100,
			'paragraphs'      => '2-3',
			'structure_hint'  => 'Opening benefit → Key features (2-3) → Brief close. Get to the point fast.',
		),
		'medium'    => array(
			'label'           => 'Medium',
			'description'     => '4-5 paragraphs with feature highlights',
			'min_words'       => 250,
			'paragraphs'      => '4-5',
			'structure_hint'  => 'Opening benefit → Feature highlights with benefits → Practical specs → Confident close.',
		),
		'long'      => array(
			'label'           => 'Long',
			'description'     => '6-8 paragraphs with detailed sections',
			'min_words'       => 500,
			'paragraphs'      => '6-8',
			'structure_hint'  => 'Opening hook → Problem/solution → Detailed features → Specs/dimensions → Use cases → Strong close.',
		),
		'very_long' => array(
			'label'           => 'Very Long',
			'description'     => '10+ paragraphs, comprehensive content',
			'min_words'       => 800,
			'paragraphs'      => '10-12',
			'structure_hint'  => 'Complete guide: Opening → Problem → Solution → Feature deep-dive → Specs → Who it\'s for → Use cases → FAQ-style points → Close.',
		),
	);

	/**
	 * Preset definitions for short descriptions
	 * Simplified in v2.6.0 - removed over-prescriptive structures
	 */
	private const SHORT_PRESETS = array(
		'brief'    => array(
			'label'           => 'Brief',
			'description'     => '1-2 sentences, key highlight only',
			'min_words'       => 20,
			'paragraphs'      => '1',
			'structure_hint'  => 'One punchy sentence with the main benefit. Optional second sentence with proof point.',
		),
		'standard' => array(
			'label'           => 'Standard',
			'description'     => '2-3 sentences with features',
			'min_words'       => 40,
			'paragraphs'      => '1',
			'structure_hint'  => 'Lead with benefit → Support with 1-2 key features → Hook to read more.',
		),
		'detailed' => array(
			'label'           => 'Detailed',
			'description'     => '4-5 sentences, mini-description',
			'min_words'       => 70,
			'paragraphs'      => '1-2',
			'structure_hint'  => 'Mini description: benefit hook → 2-3 feature highlights → what makes it different.',
		),
	);

	/**
	 * Get all available presets for full descriptions
	 *
	 * @return array
	 */
	public static function getFullPresets(): array {
		return self::FULL_PRESETS;
	}

	/**
	 * Get all available presets for short descriptions
	 *
	 * @return array
	 */
	public static function getShortPresets(): array {
		return self::SHORT_PRESETS;
	}

	/**
	 * Get a specific full description preset
	 *
	 * @param string $preset_key Preset key (short, medium, long, very_long)
	 * @return array
	 */
	public static function getFullPreset( string $preset_key ): array {
		return self::FULL_PRESETS[ $preset_key ] ?? self::FULL_PRESETS['medium'];
	}

	/**
	 * Get a specific short description preset
	 *
	 * @param string $preset_key Preset key (brief, standard, detailed)
	 * @return array
	 */
	public static function getShortPreset( string $preset_key ): array {
		return self::SHORT_PRESETS[ $preset_key ] ?? self::SHORT_PRESETS['standard'];
	}

	/**
	 * Build structural instructions for AI prompt (full description)
	 *
	 * @param string $preset_key Preset key
	 * @param bool   $for_plain_text Whether output should be plain text (no HTML)
	 * @return string Instructions for the AI
	 */
	public static function buildFullInstructions( string $preset_key, bool $for_plain_text = false ): string {
		$preset = self::getFullPreset( $preset_key );

		$format_line = $for_plain_text
			? 'Separate paragraphs with blank lines. No HTML or markdown.'
			: 'HTML: Use <p>, <ul><li>, <strong> as needed.';

		return "LENGTH: {$preset['min_words']}+ words, {$preset['paragraphs']} paragraphs
{$preset['structure_hint']}
{$format_line}";
	}

	/**
	 * Build structural instructions for AI prompt (short description)
	 *
	 * @param string $preset_key Preset key
	 * @param bool   $for_plain_text Whether output should be plain text (no HTML)
	 * @return string Instructions for the AI
	 */
	public static function buildShortInstructions( string $preset_key, bool $for_plain_text = false ): string {
		$preset = self::getShortPreset( $preset_key );

		$format_line = $for_plain_text
			? 'Plain text only. No HTML or markdown.'
			: '';

		$result = "LENGTH: {$preset['min_words']}+ words, {$preset['paragraphs']} paragraph(s)
{$preset['structure_hint']}";

		if ( ! empty( $format_line ) ) {
			$result .= "\n" . $format_line;
		}

		return $result;
	}

	/**
	 * Get preset options for frontend dropdown (full description)
	 *
	 * @return array Array of options for select dropdown
	 */
	public static function getFullPresetOptions(): array {
		$options = array();
		foreach ( self::FULL_PRESETS as $key => $preset ) {
			$options[] = array(
				'value'       => $key,
				'label'       => $preset['label'],
				'description' => $preset['description'],
			);
		}
		return $options;
	}

	/**
	 * Get preset options for frontend dropdown (short description)
	 *
	 * @return array Array of options for select dropdown
	 */
	public static function getShortPresetOptions(): array {
		$options = array();
		foreach ( self::SHORT_PRESETS as $key => $preset ) {
			$options[] = array(
				'value'       => $key,
				'label'       => $preset['label'],
				'description' => $preset['description'],
			);
		}
		return $options;
	}

	/**
	 * Get default preset key for full descriptions
	 *
	 * @return string
	 */
	public static function getDefaultFullPreset(): string {
		return 'medium';
	}

	/**
	 * Get default preset key for short descriptions
	 *
	 * @return string
	 */
	public static function getDefaultShortPreset(): string {
		return 'standard';
	}

	/**
	 * Validate preset key for full descriptions
	 *
	 * @param string $preset_key
	 * @return bool
	 */
	public static function isValidFullPreset( string $preset_key ): bool {
		return isset( self::FULL_PRESETS[ $preset_key ] );
	}

	/**
	 * Validate preset key for short descriptions
	 *
	 * @param string $preset_key
	 * @return bool
	 */
	public static function isValidShortPreset( string $preset_key ): bool {
		return isset( self::SHORT_PRESETS[ $preset_key ] );
	}
}
