<?php
/**
 * WeCodeArt Framework.
 *
 * WARNING: This file is part of the core WeCodeArt Framework. DO NOT edit this file under any circumstances.
 * Please do all modifications in the form of a child theme.
 *
 * @package 	WeCodeArt Framework
 * @subpackage 	Support\WPSeo\Generators\FAQ
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since 		6.5.7
 * @version		6.5.7
 */

namespace WeCodeArt\Support\Plugins\WPSeo\Generators;

defined( 'ABSPATH' ) || exit;

use function WeCodeArt\Functions\get_prop;

/**
 * Class Book
 */
class FAQ {

	/**
	 * A value object with context variables.
	 *
	 * @var WPSEO_Schema_Context
	 */
	public $context;

	/**
	 * Book constructor.
	 *
	 * @param WPSEO_Schema_Context $context Value object with context variables.
	 */
	public function __construct( \WPSEO_Schema_Context $context ) {
		$this->context = $context;
	}

	/**
	 * Determines whether or not a piece should be added to the graph.
	 *
	 * @return bool Whether or not a piece should be added.
	 */
	public function is_needed() {
		if( ! wecodeart( 'blocks' )->get( 'core/details' )::has_seo_support() ) {
			return false;
		}

		if ( empty( $this->context->blocks['core/details'] ) ) {
			return false;
		}

		if ( ! \is_array( $this->context->schema_page_type ) ) {
			$this->context->schema_page_type = [ $this->context->schema_page_type ];
		}

		$this->context->schema_page_type[]  = 'FAQPage';
		$this->context->main_entity_of_page = $this->generate_ids();

		return true;
	}

	/**
	 * Generate the IDs so we can link to them in the main entity.
	 *
	 * @return array
	 */
	private function generate_ids(): array {
		$ids = [];

		foreach ( $this->context->blocks['core/details'] as $block ) {
			if( empty( $this->get_question( $block ) ) ) {
				continue;
			}

			$ids[] = [
				'@id' => $this->get_question_id( $block )
			];
		}

		if( isset( $this->context->main_entity_of_page ) ) {
			$ids = wp_parse_args( $ids, $this->context->main_entity_of_page );
		}

		return $ids;
	}

	/**
	 * Render a list of questions, referencing them by ID.
	 *
	 * @return array Our Schema graph.
	 */
	public function generate(): array {
		$graph = [];
	
		foreach ( $this->context->blocks['core/details'] as $block ) {
			if( empty( $this->get_question( $block ) ) ) {
				continue;
			}

			$graph[] = $this->generate_question_block( $block );
		}

		return $graph;
	}

	/**
	 * Generate a Question piece.
	 *
	 * @param 	array 	$question The question to generate schema for.
	 *
	 * @return 	array 	Schema.org Question piece.
	 */
	protected function generate_question_block( array $block ): array {
		$language		= \apply_filters( 'wpseo_schema_piece_language', \get_bloginfo( 'language' ) );
		$question		= $this->get_question( $block );
		$question_id 	= $this->get_question_id( $block );
		$question_ans	= $this->get_question_answer( $block );
		$question_pos 	= array_search( $question_id, array_column( $this->context->main_entity_of_page, '@id' ) );

		return [
			'@type'          => 'Question',
			'@id'            => $question_id,
			'position'       => $question_pos + 1,
			'url'            => $question_id,
			'name'           => $question,
			'inLanguage'	 => $language,
			'answerCount'    => 1,
			'acceptedAnswer' => [
				'@type' 		=> 'Answer',
				'text'  		=> $question_ans,
				'inLanguage'	=> $language
			],
		];
	}

	/**
	 * Get Question
	 *
	 * @param	array	$block
	 *
	 * @return 	string
	 */
	protected function get_question( array $block ): string {
		return wecodeart( 'blocks' )->get( 'core/details' )::get_question( $block );
	}

	/**
	 * Get Question id
	 *
	 * @param	array	$block
	 *
	 * @return 	string
	 */
	protected function get_question_id( array $block ): string {
		$dom 		= wecodeart( 'dom' )::create( get_prop( $block, [ 'innerHTML' ], '' ) );
		$details	= wecodeart( 'dom' )::get_element( 'details', $dom, 0 );
		$has_id 	= is_object( $details ) ? $details->getAttribute( 'id' ) : '';
		
		$the_id 	= empty( $has_id ) ? wecodeart( 'blocks' )->get( 'core/details' )::generate_id( $block ) : $has_id;

		return $this->context->canonical . '#' . $the_id;
	}

	/**
	 * Get Question content
	 *
	 * @param	array	$block
	 *
	 * @return 	string
	 */
	protected function get_question_answer( array $block = [] ): string {
		return wecodeart( 'blocks' )->get( 'core/details' )::get_answer( $block );
	}
}