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

namespace SEOvaultSEO\Blocks;

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

/**
 * Soft defaults + hard maximums for FAQ block attributes.
 */
class FaqBlockLimits {

	const DEFAULT_MAX_ITEMS            = 10;
	const HARD_MAX_ITEMS               = 20;
	const DEFAULT_MAX_HEADING_LENGTH   = 80;
	const HARD_MAX_HEADING_LENGTH      = 160;
	const DEFAULT_MAX_INTRO_LENGTH     = 240;
	const HARD_MAX_INTRO_LENGTH        = 480;
	const DEFAULT_MAX_QUESTION_LENGTH  = 160;
	const HARD_MAX_QUESTION_LENGTH     = 320;
	const DEFAULT_MAX_ANSWER_LENGTH    = 600;
	const HARD_MAX_ANSWER_LENGTH       = 2000;
	const DEFAULT_MAX_PAYLOAD_BYTES    = 65536;
	const HARD_MAX_PAYLOAD_BYTES       = 131072;

	/**
	 * Allowed display modes.
	 *
	 * @return string[]
	 */
	public static function display_modes() {
		return array( 'accordion', 'static' );
	}

	/**
	 * Allowed style presets.
	 *
	 * @return string[]
	 */
	public static function style_presets() {
		return array(
			'clean-faq',
			'accordion-cards',
			'minimal-seo',
			'soft-help-center',
			'bordered-editorial',
			'compact-faq',
			'dark-faq',
		);
	}

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

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

	/**
	 * Resolve one filterable limit clamped to hard max.
	 *
	 * @param string $key          Filter suffix (e.g. max_items).
	 * @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 FAQ block limit.
			 *
			 * @param int    $default Soft default.
			 * @param string $key     Limit key.
			 * @param array  $context Context.
			 */
			$value = (int) apply_filters( 'seovaultseo_faq_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_items'           => self::get( 'max_items', self::DEFAULT_MAX_ITEMS, self::HARD_MAX_ITEMS, $context ),
			'max_heading_length'  => self::get( 'max_heading_length', self::DEFAULT_MAX_HEADING_LENGTH, self::HARD_MAX_HEADING_LENGTH, $context ),
			'max_intro_length'    => self::get( 'max_intro_length', self::DEFAULT_MAX_INTRO_LENGTH, self::HARD_MAX_INTRO_LENGTH, $context ),
			'max_question_length' => self::get( 'max_question_length', self::DEFAULT_MAX_QUESTION_LENGTH, self::HARD_MAX_QUESTION_LENGTH, $context ),
			'max_answer_length'   => self::get( 'max_answer_length', self::DEFAULT_MAX_ANSWER_LENGTH, self::HARD_MAX_ANSWER_LENGTH, $context ),
			'max_payload_bytes'   => self::get( 'max_payload_bytes', self::DEFAULT_MAX_PAYLOAD_BYTES, self::HARD_MAX_PAYLOAD_BYTES, $context ),
		);
	}
}
