<?php
/**
 * Filterable Quick Answer block limits with hard caps.
 *
 * @package SEOvaultSEO\Blocks
 */

namespace SEOvaultSEO\Blocks;

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

/**
 * Soft defaults + hard maximums for Quick Answer block attributes.
 */
class QuickAnswerBlockLimits {

	const DEFAULT_MAX_LABEL_LENGTH  = 80;
	const HARD_MAX_LABEL_LENGTH     = 160;
	const DEFAULT_MAX_ANSWER_LENGTH = 600;
	const HARD_MAX_ANSWER_LENGTH    = 2000;
	const DEFAULT_MAX_SOURCE_LENGTH = 40;
	const HARD_MAX_SOURCE_LENGTH    = 80;
	const DEFAULT_MAX_PAYLOAD_BYTES = 16384;
	const HARD_MAX_PAYLOAD_BYTES    = 65536;

	/**
	 * Allowed style presets (aligned with web Quick Answer block).
	 *
	 * @return string[]
	 */
	public static function style_presets() {
		return array( 'clean', 'callout', 'soft-card', 'bordered', 'minimal', 'editorial' );
	}

	/**
	 * Allowed color presets.
	 *
	 * @return string[]
	 */
	public static function color_presets() {
		return array( 'blue', 'green', 'purple', 'orange', 'dark', 'neutral' );
	}

	/**
	 * Allowed density values.
	 *
	 * @return string[]
	 */
	public static function densities() {
		return array( 'compact', 'comfortable', 'spacious' );
	}

	/**
	 * Allowed diagnostic source bases (never secrets/prompts).
	 *
	 * @return string[]
	 */
	public static function source_bases() {
		return array( 'manual', 'ultra', 'tldr', 'aeo', 'ai' );
	}

	/**
	 * Resolve one filterable limit clamped to hard max.
	 *
	 * @param string $key      Filter suffix.
	 * @param int    $default  Soft default.
	 * @param int    $hard_max Hard cap.
	 * @param array  $context  Optional overrides for tests.
	 * @return int
	 */
	public static function get( $key, $default, $hard_max, array $context = array() ) {
		if ( isset( $context[ $key ] ) ) {
			$value = (int) $context[ $key ];
		} else {
			/**
			 * Filter a Quick Answer block limit.
			 *
			 * @param int    $default Soft default.
			 * @param string $key     Limit key.
			 * @param array  $context Context.
			 */
			$value = (int) apply_filters( 'seovaultseo_quick_answer_block_limits', $default, $key, $context );
			if ( is_array( $value ) ) {
				$value = isset( $value[ $key ] ) ? (int) $value[ $key ] : $default;
			}
		}

		$value = max( 0, $value );
		return min( $value, (int) $hard_max );
	}

	/**
	 * Snapshot of active limits.
	 *
	 * @param array $context Overrides.
	 * @return array<string,int>
	 */
	public static function all( array $context = array() ) {
		return array(
			'max_label_length'  => self::get( 'max_label_length', self::DEFAULT_MAX_LABEL_LENGTH, self::HARD_MAX_LABEL_LENGTH, $context ),
			'max_answer_length' => self::get( 'max_answer_length', self::DEFAULT_MAX_ANSWER_LENGTH, self::HARD_MAX_ANSWER_LENGTH, $context ),
			'max_source_length' => self::get( 'max_source_length', self::DEFAULT_MAX_SOURCE_LENGTH, self::HARD_MAX_SOURCE_LENGTH, $context ),
			'max_payload_bytes' => self::get( 'max_payload_bytes', self::DEFAULT_MAX_PAYLOAD_BYTES, self::HARD_MAX_PAYLOAD_BYTES, $context ),
		);
	}
}
