<?php
/**
 * AI writing style presets for alt text generation.
 *
 * @package AIAltTextGenerator
 * @since 2.0.1
 */

namespace AIAltTextGenerator;

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

/**
 * Central prompt style registry.
 *
 * @since 2.0.1
 */
class PromptStyles {

	/**
	 * @return list<string>
	 */
	public static function keys(): array {
		return array_keys( self::definitions() );
	}

	/**
	 * @return array<string, string>
	 */
	public static function labels(): array {
		$labels = [];
		foreach ( self::definitions() as $key => $definition ) {
			$labels[ $key ] = (string) $definition['label'];
		}

		return $labels;
	}

	/**
	 * @return array<string, array{label: string, group: string, instruction: string, quality: bool}>
	 */
	private static function definitions(): array {
		return [
			'concise'      => [
				'label'       => __( 'Concise — essential content only (recommended)', 'wp-powersuite' ),
				'group'       => __( 'General', 'wp-powersuite' ),
				'instruction' => __( 'Be very concise. Use the fewest words needed to convey the essential content and purpose of the image. Skip decorative filler.', 'wp-powersuite' ),
				'quality'     => false,
			],
			'detailed'     => [
				'label'       => __( 'Detailed — richer context and visual detail', 'wp-powersuite' ),
				'group'       => __( 'General', 'wp-powersuite' ),
				'instruction' => __( 'Provide a fuller description including context, setting, colors, composition, and the most important visual elements while staying within the character limit.', 'wp-powersuite' ),
				'quality'     => true,
			],
			'seo'          => [
				'label'       => __( 'SEO — keyword-friendly for search', 'wp-powersuite' ),
				'group'       => __( 'General', 'wp-powersuite' ),
				'instruction' => __( 'Write descriptive alt text that includes the provided SEO keywords or focus keyphrase naturally when they are supplied. Clarity for humans first, then search relevance. Avoid keyword stuffing or keyword lists.', 'wp-powersuite' ),
				'quality'     => true,
			],
			'descriptive'  => [
				'label'       => __( 'Accessibility — objective screen-reader focus', 'wp-powersuite' ),
				'group'       => __( 'Accessibility', 'wp-powersuite' ),
				'instruction' => __( 'Write objective, accessibility-first alt text. Describe what the image communicates to someone who cannot see it. Focus on meaning, text in the image, and functional content.', 'wp-powersuite' ),
				'quality'     => true,
			],
			'minimalist'   => [
				'label'       => __( 'Minimalist — ultra-short (3–8 words)', 'wp-powersuite' ),
				'group'       => __( 'Accessibility', 'wp-powersuite' ),
				'instruction' => __( 'Use the shortest possible phrase, ideally 3 to 8 words. Include only the single most important subject or action.', 'wp-powersuite' ),
				'quality'     => false,
			],
			'ecommerce'    => [
				'label'       => __( 'E-commerce — product and merchandising focus', 'wp-powersuite' ),
				'group'       => __( 'Marketing', 'wp-powersuite' ),
				'instruction' => __( 'Write product-oriented alt text suitable for online stores. Highlight the product, variant, material, color, or use case when visible. Sound helpful to shoppers.', 'wp-powersuite' ),
				'quality'     => true,
			],
			'brand'        => [
				'label'       => __( 'Brand voice — polished marketing tone', 'wp-powersuite' ),
				'group'       => __( 'Marketing', 'wp-powersuite' ),
				'instruction' => __( 'Use a polished, brand-friendly tone. Keep the description inviting and professional without hype, exclamation marks, or sales clichés.', 'wp-powersuite' ),
				'quality'     => true,
			],
			'journalistic' => [
				'label'       => __( 'Journalistic — neutral factual caption', 'wp-powersuite' ),
				'group'       => __( 'Editorial', 'wp-powersuite' ),
				'instruction' => __( 'Write in a neutral, factual news-caption style. State who, what, where, and when when relevant. Avoid opinion or marketing language.', 'wp-powersuite' ),
				'quality'     => true,
			],
			'narrative'    => [
				'label'       => __( 'Narrative — brief scene or story', 'wp-powersuite' ),
				'group'       => __( 'Editorial', 'wp-powersuite' ),
				'instruction' => __( 'Describe the image as a short scene. Convey atmosphere and action in a natural sentence while remaining concise and useful.', 'wp-powersuite' ),
				'quality'     => true,
			],
			'technical'    => [
				'label'       => __( 'Technical — precise functional documentation', 'wp-powersuite' ),
				'group'       => __( 'Editorial', 'wp-powersuite' ),
				'instruction' => __( 'Use precise, functional language suitable for documentation or UI screenshots. Name interface elements, states, and relationships clearly.', 'wp-powersuite' ),
				'quality'     => true,
			],
		];
	}

	public static function sanitize( string $style ): string {
		$style = sanitize_key( $style );

		return in_array( $style, self::keys(), true ) ? $style : 'concise';
	}

	public static function instruction( string $style ): string {
		$definitions = self::definitions();
		$style       = self::sanitize( $style );

		return (string) ( $definitions[ $style ]['instruction'] ?? $definitions['concise']['instruction'] );
	}

	public static function needsQualityModel( string $style ): bool {
		$definitions = self::definitions();
		$style       = self::sanitize( $style );

		return ! empty( $definitions[ $style ]['quality'] );
	}

	/**
	 * Grouped labels for optgroup selects.
	 *
	 * @return array<string, array<string, string>>
	 */
	public static function groupedLabels(): array {
		$groups = [];

		foreach ( self::definitions() as $key => $definition ) {
			$group = (string) $definition['group'];
			if ( ! isset( $groups[ $group ] ) ) {
				$groups[ $group ] = [];
			}
			$groups[ $group ][ $key ] = (string) $definition['label'];
		}

		return $groups;
	}

	public static function description( string $style ): string {
		return self::instruction( $style );
	}
}
