<?php
/**
 * 404 recovery page template.
 *
 * Loaded via template_include or RecoveryRenderer::render().
 * Variables stay inside seovaultai_render_notfound_recovery() so they are not
 * unprefixed file-scope globals.
 *
 * @package SEOvaultSEO
 */

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

if ( ! function_exists( 'seovaultai_render_notfound_recovery' ) ) {
	/**
	 * Render the 404 recovery page.
	 *
	 * @param array|null $seovaultai_content Recovery content array, or null to build.
	 * @param array|null $seovaultai_colors  Theme colors array, or null to resolve.
	 */
	function seovaultai_render_notfound_recovery( $seovaultai_content = null, $seovaultai_colors = null ) {
		$content = ( is_array( $seovaultai_content ) ) ? $seovaultai_content : \SEOvaultSEO\NotFound\RecoveryContent::get();

		$colors = ( is_array( $seovaultai_colors ) ) ? $seovaultai_colors : \SEOvaultSEO\NotFound\ThemeColors::resolve();
		if ( empty( $colors['accent'] ) && ! empty( $content['accent'] ) && is_string( $content['accent'] ) ) {
			$colors['accent'] = $content['accent'];
		}

		$accent     = isset( $colors['accent'] ) && is_string( $colors['accent'] ) && '' !== $colors['accent']
			? $colors['accent']
			: ( isset( $content['accent'] ) && is_string( $content['accent'] ) ? $content['accent'] : '#1a1a1a' );
		$background = isset( $colors['background'] ) && is_string( $colors['background'] ) ? $colors['background'] : '';
		$text       = isset( $colors['text'] ) && is_string( $colors['text'] ) ? $colors['text'] : '';

		$style_parts = array( '--seovault-nf-accent: ' . $accent );
		if ( '' !== $background ) {
			$style_parts[] = '--seovault-nf-background: ' . $background;
		}
		if ( '' !== $text ) {
			$style_parts[] = '--seovault-nf-text: ' . $text;
		}
		$wrapper_style = implode( '; ', $style_parts );

		$show_search    = ! empty( $content['show_search'] );
		$show_home_cta  = ! empty( $content['show_home_cta'] );
		$show_recent    = ! empty( $content['show_recent'] ) && ! empty( $content['recent_posts'] ) && is_array( $content['recent_posts'] );
		$headline       = isset( $content['headline'] ) && is_string( $content['headline'] ) ? $content['headline'] : '';
		$message        = isset( $content['message'] ) && is_string( $content['message'] ) ? $content['message'] : '';
		$cta_label      = isset( $content['cta_label'] ) && is_string( $content['cta_label'] ) ? $content['cta_label'] : '';
		$cta_url        = isset( $content['cta_url'] ) && is_string( $content['cta_url'] ) ? $content['cta_url'] : '';
		$home_url       = isset( $content['home_url'] ) && is_string( $content['home_url'] ) ? $content['home_url'] : home_url( '/' );
		$page_body_html = isset( $content['page_body_html'] ) && is_string( $content['page_body_html'] ) ? $content['page_body_html'] : '';
		$use_page_body  = '' !== trim( $page_body_html );
		if ( '' === $cta_url ) {
			$cta_url = $home_url;
		}

		get_header();
		// get_header also fires Monitor404 capture on classic themes; keep this call
		// so recovery 404s are logged like normal theme 404s.
		?>
<main
	class="seovault-nf-recovery"
	style="<?php echo esc_attr( $wrapper_style ); ?>"
	role="main"
>
	<div class="seovault-nf-recovery__inner">
		<?php if ( $use_page_body ) : ?>
			<div class="seovault-nf-recovery__page-body">
				<?php
				// Preserve normal post markup while stripping unsafe tags and attributes.
				echo wp_kses_post( $page_body_html );
				?>
			</div>
		<?php else : ?>
			<header class="seovault-nf-recovery__header">
				<?php if ( '' !== $headline ) : ?>
					<h1 class="seovault-nf-recovery__headline"><?php echo esc_html( $headline ); ?></h1>
				<?php endif; ?>

				<?php if ( '' !== $message ) : ?>
					<p class="seovault-nf-recovery__message"><?php echo esc_html( $message ); ?></p>
				<?php endif; ?>
			</header>
		<?php endif; ?>

		<?php if ( $show_search ) : ?>
			<div class="seovault-nf-recovery__search">
				<?php get_search_form(); ?>
			</div>
		<?php endif; ?>

		<?php if ( $show_home_cta && '' !== $cta_label ) : ?>
			<p class="seovault-nf-recovery__cta-wrap">
				<a class="seovault-nf-recovery__cta" href="<?php echo esc_url( $cta_url ); ?>">
					<?php echo esc_html( $cta_label ); ?>
				</a>
			</p>
		<?php endif; ?>

		<?php if ( $show_recent ) : ?>
			<section class="seovault-nf-recovery__recent" aria-label="<?php echo esc_attr__( 'Recent posts', 'seovault-ai' ); ?>">
				<h2 class="seovault-nf-recovery__recent-title"><?php echo esc_html__( 'Recent posts', 'seovault-ai' ); ?></h2>
				<ul class="seovault-nf-recovery__recent-list">
					<?php foreach ( $content['recent_posts'] as $recent_post ) : ?>
						<?php
						if ( ! is_array( $recent_post ) ) {
							continue;
						}
						$post_title = isset( $recent_post['title'] ) && is_string( $recent_post['title'] ) ? $recent_post['title'] : '';
						$post_url   = isset( $recent_post['url'] ) && is_string( $recent_post['url'] ) ? $recent_post['url'] : '';
						if ( '' === $post_title || '' === $post_url ) {
							continue;
						}
						?>
						<li class="seovault-nf-recovery__recent-item">
							<a href="<?php echo esc_url( $post_url ); ?>"><?php echo esc_html( $post_title ); ?></a>
						</li>
					<?php endforeach; ?>
				</ul>
			</section>
		<?php endif; ?>
	</div>
</main>
		<?php
		get_footer();
	}
}

// template_include path: WP includes this file directly.
// RecoveryRenderer::render() sets SEOVAULTAI_RECOVERY_MANUAL_RENDER and calls the function itself.
if ( ! defined( 'SEOVAULTAI_RECOVERY_MANUAL_RENDER' ) || ! SEOVAULTAI_RECOVERY_MANUAL_RENDER ) {
	seovaultai_render_notfound_recovery();
}
