<?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\RankMath
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since 		6.1.2
 * @version		6.5.7
 */

namespace WeCodeArt\Support\Plugins;

defined( 'ABSPATH' ) || exit;

use WeCodeArt\Support\Plugins;
use WeCodeArt\Admin\Notifications;
use WeCodeArt\Admin\Notifications\Notification;
use WeCodeArt\Config\Traits\Singleton;
use WeCodeArt\Config\Interfaces\Integration;
use function WeCodeArt\Functions\get_prop;

/**
 * RankMath Integration
 */
class RankMath implements Integration {

	use Singleton;

	// Vars
	public static $questions = [];

	/**
	 * Get Conditionals
	 *
	 * @return void
	 */
	public static function get_conditionals(): array {
		wecodeart( 'ifso' )->register( 'is_rankmath_active', RankMath\Condition::class );
		
		return [ 'is_rankmath_active' ];
	}

	/**
	 * Send to Constructor
	 */
	public function register_hooks(): void {
		\add_action( 'after_setup_theme',					[ $this, 'register_blocks'		] );
		\add_filter( 'wecodeart/filter/template/context',	[ $this, 'post_category_view'	], 20, 2 );

		if( wecodeart( 'blocks' )->get( 'core/details' )::has_seo_support() === true ) {
			\add_filter( 'render_block', 		[ $this, 'collect_questions' 	], PHP_INT_MAX, 2 ); 
			\add_filter( 'rank_math/json_ld', 	[ $this, 'generate_schema' 		], PHP_INT_MAX, 1 );
		}
	}

	/**
	 * Register Block Overwrites
	 *
	 * @return 	void
	 */
	public function register_blocks(): void {
		// Avoid overwriting content in admin.
		if( is_admin() ) {
			return;
		}

		// Register RankMath FAQ Overwrite
		wecodeart( 'blocks' )->register( 'rankmath/faq-block', RankMath\Blocks\FAQ::class );
	}

	/**
	 * Collection questions.
	 *
	 * @param	string|null $content	The pre-rendered content. Default null.
	 * @param 	array       $block 		The block being rendered.
	 */
	public function collect_questions( string $content, array $block ): string {
		if ( 'core/details' !== get_prop( $block, [ 'blockName' ] ) ) {
			return $content;
		}

		$details 	= wecodeart( 'blocks' )->get( 'core/details' );
		$processor 	= wecodeart( 'dom' )::processor( $content );
		$processor->next_tag( 'DETAILS' );

		self::$questions[] = [
			'id'		=> $processor->get_attribute( 'id' ),
			'question' 	=> $details::get_question( $block ),
			'answer' 	=> $details::get_answer( $block ),
		];

		return $content;
	}

	/**
	 * Generate schema.
	 *
	 * @param	array 	$data
	 *
	 * @return  array
	 */
	public function generate_schema( array $data ): array { 
		if ( ! isset( $data['WebPage'] ) || empty( self::$questions ) ) {
			return $data;
		}

		$data['WebPage']['@type'] = [ 'WebPage', 'FAQPage' ];
		
		if( ! isset( $data['WebPage']['mainEntity'] ) ) {
			$data['WebPage']['mainEntity'] = [];
		}

		$permalink 	= \get_permalink();
		$language 	= \get_bloginfo( 'language' );

		$data['WebPage']['mainEntity'] = wp_parse_args(
			array_map( function( $data ) use ( $permalink, $language ) {
				return [
					'@type'			 => 'Question',
					'@id'			 => $permalink . '#' . $data['id'],
					'url'            => $permalink . '#' . $data['id'],
					'name'           => $data['question'],
					'inLanguage'	 => $language,
					'answerCount'    => 1,
					'acceptedAnswer' => [
						'@type' 		=> 'Answer',
						'text'  		=> $data['answer'],
						'inLanguage'	=> $language
					],
				];
			}, self::$questions ),
			$data['WebPage']['mainEntity'] 
		);

		return $data; 
	}

	/**
	 * Extend Category with RankMath's Primary Term
	 *
	 * @return 	array
	 */
	public function post_category_view( $args, $name ): array {
		if( $name !== 'blocks/post/terms.php' ) {
			return $args;
		}
		
		if( $meta = get_post_meta( get_prop( $args, 'post_id' ), 'rank_math_primary_category', true ) ) {
			$args['primary'] = (int) $meta;
		}

		return $args;
	}

	/**
	 * Installer
	 *
	 * @since	6.1.2
	 * @version	6.1.2
	 *
	 * @return 	array
	 */
	public function installer(): array {
		return [
			'slug' 			=> null,
			'source'		=> 'custom',
			'title' 		=> 'Rank Math SEO',
			'description' 	=> Plugins::get_default_description( 'Rank Math SEO' ),
			'required' 		=> [ 'seo-by-rank-math' ]
		];
	}
}