<?php
/**
 * Quiz Statistics Questions OpenAPI Documentation.
 *
 * Provides OpenAPI specification for quiz statistics questions endpoints.
 * Currently based on V2 REST API: https://developers.learndash.com/rest-api/v2/v2-quiz-statistics/.
 *
 * @since TBD
 *
 * @package LearnDash\Core
 */

namespace LearnDash\Core\Modules\REST\Documentation_Migration\OpenAPI\Endpoints\Quizzes;

use LDLMS_Post_Types;
use LearnDash_Settings_Section;
use LearnDash\Core\Modules\REST\Documentation_Migration\OpenAPI\Contracts\LDLMS_V2_Endpoint;

/**
 * Quiz Statistics Questions OpenAPI Documentation Endpoint.
 *
 * @since TBD
 */
class Quiz_Statistics_Questions extends LDLMS_V2_Endpoint {
	/**
	 * Returns the response schema for this endpoint.
	 *
	 * @since TBD
	 *
	 * @param string $path   The path of the route. Defaults to empty string.
	 * @param string $method The HTTP method. Defaults to empty string.
	 *
	 * @return array<string,array<string,mixed>|string>
	 */
	public function get_response_schema( string $path = '', string $method = '' ): array {
		$route_path = '/' . trim( $this->get_namespace(), '/' ) . '/' . ltrim( $path, '/' );

		if ( $this->determine_route_type( $route_path, 'sfwd-quiz/{quiz}/statistics/{statistic}/questions' ) === 'singular' ) {
			return [
				'$ref' => '#/components/schemas/LDLMS_v2_Quiz_Statistic_Question',
			];
		}

		return [
			'type'  => 'array',
			'items' => [
				'$ref' => '#/components/schemas/LDLMS_v2_Quiz_Statistic_Question',
			],
		];
	}

	/**
	 * Returns the routes configuration for this endpoint.
	 *
	 * @since TBD
	 *
	 * @return array<string,array<string,string|callable>>
	 */
	protected function get_routes(): array {
		$quizzes_endpoint              = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_REST_API', 'quizzes_v2' );
		$statistics_endpoint           = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_REST_API', 'quizzes-statistics_v2' );
		$statistics_questions_endpoint = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_General_REST_API', 'quizzes-statistics-questions_v2' );

		return $this->discover_routes(
			trailingslashit( $quizzes_endpoint ) . '(?P<quiz>[\d]+)/' . $statistics_endpoint . '/(?P<statistic>[\d]+)/' . $statistics_questions_endpoint,
			[ 'collection', 'singular' ]
		);
	}

	/**
	 * Returns the summary for a specific HTTP method.
	 *
	 * @since TBD
	 *
	 * @param string $method The HTTP method.
	 * @param string $route_type The route type ('collection', 'singular', or 'nested').
	 *
	 * @return string
	 */
	protected function get_method_summary( string $method, string $route_type = 'collection' ): string {
		$summaries = [
			'collection' => [
				'GET' => sprintf(
					// translators: %1$s: question label. %2$s: quiz label.
					__( 'Get %1$s statistics for a specific %2$s', 'learndash' ),
					learndash_get_custom_label_lower( LDLMS_Post_Types::QUESTION ),
					learndash_get_custom_label_lower( LDLMS_Post_Types::QUIZ ),
				),
			],
			'singular'   => [
				'GET' => sprintf(
					// translators: %1$s: question label. %2$s: quiz label.
					__( 'Get %1$s statistics for a specific %2$s and %1$s', 'learndash' ),
					learndash_get_custom_label_lower( LDLMS_Post_Types::QUESTION ),
					learndash_get_custom_label_lower( LDLMS_Post_Types::QUIZ ),
				),
			],
		];

		return $summaries[ $route_type ][ $method ]
			?? sprintf(
				// translators: %1$s: question label. %2$s: quiz label.
				__( '%1$s statistics %2$s operation', 'learndash' ),
				learndash_get_custom_label_lower( LDLMS_Post_Types::QUESTION ),
				learndash_get_custom_label_lower( LDLMS_Post_Types::QUIZ ),
			);
	}

	/**
	 * Returns the description for a specific HTTP method.
	 *
	 * @since TBD
	 *
	 * @param string $method The HTTP method.
	 * @param string $route_type The route type ('collection', 'singular', or 'nested').
	 *
	 * @return string
	 */
	protected function get_method_description( string $method, string $route_type = 'collection' ): string {
		$descriptions = [
			'collection' => [
				'GET' => sprintf(
					// translators: %1$s: question label. %2$s: quiz label.
					__( 'Retrieves the %1$s statistics for a specific %2$s statistic.', 'learndash' ),
					learndash_get_custom_label_lower( LDLMS_Post_Types::QUESTION ),
					learndash_get_custom_label_lower( LDLMS_Post_Types::QUIZ ),
				),
			],
			'singular'   => [
				'GET' => sprintf(
					// translators: %1$s: question label. %2$s: quiz label.
					__( 'Retrieves a specific %1$s statistics for a %2$s statistic and %1$s.', 'learndash' ),
					learndash_get_custom_label_lower( LDLMS_Post_Types::QUESTION ),
					learndash_get_custom_label_lower( LDLMS_Post_Types::QUIZ ),
				),
			],
		];

		return $descriptions[ $route_type ][ $method ] ?? sprintf(
			// translators: %1$s: question label. %2$s: quiz label.
			__( 'Performs %1$s operations on %2$s statistics.', 'learndash' ),
			learndash_get_custom_label_lower( LDLMS_Post_Types::QUESTION ),
			learndash_get_custom_label_lower( LDLMS_Post_Types::QUIZ ),
		);
	}

	/**
	 * Returns the tags for this endpoint.
	 *
	 * @since TBD
	 *
	 * @return string[]
	 */
	protected function get_tags(): array {
		return [
			sprintf(
				'%s-statistics-%s',
				learndash_get_custom_label_lower( 'quiz' ),
				learndash_get_custom_label_lower( 'questions' )
			),
		];
	}
}
