<?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)', 'wppowersuite-lite' ),
				'group'       => __( 'General', 'wppowersuite-lite' ),
				'instruction' => __( 'Be very concise. Use the fewest words needed to convey the essential content and purpose of the image. Skip decorative filler.', 'wppowersuite-lite' ),
				'quality'     => false,
			],
			'detailed'     => [
				'label'       => __( 'Detailed — richer context and visual detail', 'wppowersuite-lite' ),
				'group'       => __( 'General', 'wppowersuite-lite' ),
				'instruction' => __( 'Provide a fuller description including context, setting, colors, composition, and the most important visual elements while staying within the character limit.', 'wppowersuite-lite' ),
				'quality'     => true,
			],
			'seo'          => [
				'label'       => __( 'SEO — keyword-friendly for search', 'wppowersuite-lite' ),
				'group'       => __( 'General', 'wppowersuite-lite' ),
				'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.', 'wppowersuite-lite' ),
				'quality'     => true,
			],
			'descriptive'  => [
				'label'       => __( 'Accessibility — objective screen-reader focus', 'wppowersuite-lite' ),
				'group'       => __( 'Accessibility', 'wppowersuite-lite' ),
				'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.', 'wppowersuite-lite' ),
				'quality'     => true,
			],
			'minimalist'   => [
				'label'       => __( 'Minimalist — ultra-short (3–8 words)', 'wppowersuite-lite' ),
				'group'       => __( 'Accessibility', 'wppowersuite-lite' ),
				'instruction' => __( 'Use the shortest possible phrase, ideally 3 to 8 words. Include only the single most important subject or action.', 'wppowersuite-lite' ),
				'quality'     => false,
			],
			'ecommerce'    => [
				'label'       => __( 'E-commerce — product and merchandising focus', 'wppowersuite-lite' ),
				'group'       => __( 'Marketing', 'wppowersuite-lite' ),
				'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.', 'wppowersuite-lite' ),
				'quality'     => true,
			],
			'brand'        => [
				'label'       => __( 'Brand voice — polished marketing tone', 'wppowersuite-lite' ),
				'group'       => __( 'Marketing', 'wppowersuite-lite' ),
				'instruction' => __( 'Use a polished, brand-friendly tone. Keep the description inviting and professional without hype, exclamation marks, or sales clichés.', 'wppowersuite-lite' ),
				'quality'     => true,
			],
			'journalistic' => [
				'label'       => __( 'Journalistic — neutral factual caption', 'wppowersuite-lite' ),
				'group'       => __( 'Editorial', 'wppowersuite-lite' ),
				'instruction' => __( 'Write in a neutral, factual news-caption style. State who, what, where, and when when relevant. Avoid opinion or marketing language.', 'wppowersuite-lite' ),
				'quality'     => true,
			],
			'narrative'    => [
				'label'       => __( 'Narrative — brief scene or story', 'wppowersuite-lite' ),
				'group'       => __( 'Editorial', 'wppowersuite-lite' ),
				'instruction' => __( 'Describe the image as a short scene. Convey atmosphere and action in a natural sentence while remaining concise and useful.', 'wppowersuite-lite' ),
				'quality'     => true,
			],
			'technical'    => [
				'label'       => __( 'Technical — precise functional documentation', 'wppowersuite-lite' ),
				'group'       => __( 'Editorial', 'wppowersuite-lite' ),
				'instruction' => __( 'Use precise, functional language suitable for documentation or UI screenshots. Name interface elements, states, and relationships clearly.', 'wppowersuite-lite' ),
				'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 );
	}
}
