<?php
/**
 * Single content template.
 *
 * @package BYT01
 */

get_header();
while ( have_posts() ) :
	the_post();
	$byt01_id         = get_the_ID();
	$byt01_type       = get_post_type();
	$byt01_rating     = get_post_meta( $byt01_id, '_byt01_rating', true );
	$byt01_price      = get_post_meta( $byt01_id, '_byt01_price', true );
	$byt01_badge      = get_post_meta( $byt01_id, '_byt01_badge', true );
	$byt01_verified   = get_post_meta( $byt01_id, '_byt01_review_verified_date', true );
	$byt01_sample     = get_post_meta( $byt01_id, '_byt01_review_sample_origin', true );
	$byt01_method     = get_post_meta( $byt01_id, '_byt01_review_methodology', true );
	$byt01_sources    = array_filter( preg_split( '/\r\n|\r|\n/', (string) get_post_meta( $byt01_id, '_byt01_review_sources', true ) ) );
	$byt01_updates    = array_filter( preg_split( '/\r\n|\r|\n/', (string) get_post_meta( $byt01_id, '_byt01_review_update_log', true ) ) );
	$byt01_doc_status = get_post_meta( $byt01_id, '_byt01_review_documentation_status', true );
	$byt01_cost       = get_post_meta( $byt01_id, '_byt01_cost', true );
	$byt01_target     = get_post_meta( $byt01_id, '_byt01_target', true );
	$byt01_components = array_filter( preg_split( '/\r\n|\r|\n/', (string) get_post_meta( $byt01_id, '_byt01_components', true ) ) );
	$byt01_term       = byt01_primary_term_name( $byt01_id );
	$byt01_kind       = 'byt_review' === $byt01_type ? __( 'مراجعة', 'byt01' ) : ( 'byt_build' === $byt01_type ? __( 'تجميعة', 'byt01' ) : __( 'مقال', 'byt01' ) );
	?>
	<article <?php post_class( 'article-page' ); ?>>
		<header class="article-header">
			<div class="container article-header__inner">
				<div class="article-header__tags"><span><?php echo esc_html( $byt01_badge ?: $byt01_kind ); ?></span><?php if ( $byt01_term ) : ?><span><?php echo esc_html( $byt01_term ); ?></span><?php endif; ?></div>
				<h1><?php echo wp_kses( byt01_format_mixed_title( get_the_title() ), array( 'bdi' => array( 'dir' => array() ) ) ); ?></h1>
				<?php if ( has_excerpt() ) : ?><p class="article-deck"><?php echo esc_html( get_the_excerpt() ); ?></p><?php endif; ?>
				<div class="article-byline">
					<div class="author-avatar" aria-hidden="true">B</div>
					<div><strong><?php echo esc_html( get_the_author() ); ?></strong><span><time datetime="<?php echo esc_attr( get_the_date( DATE_W3C ) ); ?>"><?php echo esc_html( get_the_date() ); ?></time><i aria-hidden="true">•</i><?php $byt01_minutes = byt01_reading_time( $byt01_id ); printf( 1 === $byt01_minutes ? esc_html__( '%d دقيقة قراءة', 'byt01' ) : esc_html__( '%d دقائق قراءة', 'byt01' ), $byt01_minutes ); ?></span></div>
					<?php if ( '' !== (string) $byt01_rating ) : ?><span class="article-score"><strong><?php echo esc_html( $byt01_rating ); ?></strong><small>/10</small></span><?php endif; ?>
				</div>
			</div>
		</header>

		<?php if ( has_post_thumbnail() ) : ?>
			<div class="container article-cover"><figure><?php the_post_thumbnail( 'full', array( 'loading' => 'eager', 'fetchpriority' => 'high', 'decoding' => 'async' ) ); ?><?php if ( get_the_post_thumbnail_caption() ) : ?><figcaption><?php echo esc_html( get_the_post_thumbnail_caption() ); ?></figcaption><?php endif; ?></figure></div>
		<?php endif; ?>

		<div class="container article-layout">
			<aside class="article-facts" aria-label="<?php esc_attr_e( 'ملخص المادة', 'byt01' ); ?>">
				<div class="article-facts__title"><span><?php echo byt01_ui_icon( 'lab' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span><div><small>BYT01 LABS</small><strong><?php esc_html_e( 'الخلاصة السريعة', 'byt01' ); ?></strong></div></div>
				<dl>
					<div><dt><?php esc_html_e( 'نوع المحتوى', 'byt01' ); ?></dt><dd><?php echo esc_html( $byt01_kind ); ?></dd></div>
					<?php if ( 'byt_review' === $byt01_type && $byt01_badge ) : ?><div><dt><?php esc_html_e( 'مستوى الأدلة', 'byt01' ); ?></dt><dd><?php echo esc_html( $byt01_badge ); ?></dd></div><?php endif; ?>
					<?php if ( 'byt_review' === $byt01_type && $byt01_verified ) : ?><div><dt><?php esc_html_e( 'آخر تحقق تحريري', 'byt01' ); ?></dt><dd><?php echo esc_html( $byt01_verified ); ?></dd></div><?php endif; ?>
					<?php if ( 'byt_review' === $byt01_type && $byt01_sample ) : ?><div><dt><?php esc_html_e( 'مصدر العينة', 'byt01' ); ?></dt><dd><?php echo esc_html( $byt01_sample ); ?></dd></div><?php endif; ?>
					<?php if ( $byt01_price ) : ?><div><dt><?php esc_html_e( 'السعر المسجل', 'byt01' ); ?></dt><dd><?php echo esc_html( $byt01_price ); ?></dd></div><?php endif; ?>
					<?php if ( $byt01_cost ) : ?><div><dt><?php esc_html_e( 'تكلفة التجميعة', 'byt01' ); ?></dt><dd><?php echo esc_html( $byt01_cost ); ?></dd></div><?php endif; ?>
					<?php if ( $byt01_target ) : ?><div><dt><?php esc_html_e( 'الاستخدام المستهدف', 'byt01' ); ?></dt><dd><?php echo esc_html( $byt01_target ); ?></dd></div><?php endif; ?>
					<div><dt><?php esc_html_e( 'آخر تحديث', 'byt01' ); ?></dt><dd><?php echo esc_html( get_the_modified_date() ); ?></dd></div>
				</dl>
				<?php if ( $byt01_components ) : ?>
					<div class="article-components"><strong><?php esc_html_e( 'مكونات التجميعة', 'byt01' ); ?></strong><ul><?php foreach ( $byt01_components as $byt01_component ) : $byt01_parts = array_map( 'trim', explode( '|', $byt01_component ) ); ?><li><span><?php echo esc_html( $byt01_parts[0] ?? '' ); ?></span><b><?php echo esc_html( $byt01_parts[1] ?? '' ); ?></b><?php if ( ! empty( $byt01_parts[2] ) ) : ?><em><?php echo esc_html( $byt01_parts[2] ); ?></em><?php endif; ?></li><?php endforeach; ?></ul></div>
				<?php endif; ?>
				<p class="article-facts__note"><?php esc_html_e( 'قد تتغير الأسعار والتوفر بمرور الوقت؛ تحقق من المصدر قبل الشراء.', 'byt01' ); ?></p>
			</aside>

			<div class="article-content">
				<?php the_content(); ?>
				<?php if ( 'byt_review' === $byt01_type && ( $byt01_method || $byt01_sources || $byt01_updates ) ) : ?>
					<section class="article-evidence" aria-labelledby="byt01-evidence-title">
						<h2 id="byt01-evidence-title"><?php esc_html_e( 'ملف الأدلة والشفافية', 'byt01' ); ?></h2>
						<p class="article-evidence__status"><strong><?php esc_html_e( 'حالة التوثيق:', 'byt01' ); ?></strong> <?php echo esc_html( 'complete' === $byt01_doc_status ? __( 'مكتمل وفق الحقول التحريرية', 'byt01' ) : __( 'غير مكتمل — لا يعتمد لتقييم رقمي', 'byt01' ) ); ?></p>
						<?php if ( $byt01_method ) : ?><h3><?php esc_html_e( 'المنهجية وحدود المادة', 'byt01' ); ?></h3><p><?php echo nl2br( esc_html( $byt01_method ) ); ?></p><?php endif; ?>
						<?php if ( $byt01_sources ) : ?>
							<h3><?php esc_html_e( 'المصادر', 'byt01' ); ?></h3><ol class="article-sources">
							<?php foreach ( $byt01_sources as $byt01_source_line ) : $byt01_source_parts = array_map( 'trim', explode( '|', $byt01_source_line, 3 ) ); $byt01_source_url = $byt01_source_parts[0] ?? ''; if ( ! wp_http_validate_url( $byt01_source_url ) ) { continue; } ?>
								<li><a href="<?php echo esc_url( $byt01_source_url ); ?>" target="_blank" rel="noopener noreferrer nofollow"><?php echo esc_html( $byt01_source_parts[1] ?? wp_parse_url( $byt01_source_url, PHP_URL_HOST ) ); ?></a><?php if ( ! empty( $byt01_source_parts[2] ) ) : ?><time><?php echo esc_html( $byt01_source_parts[2] ); ?></time><?php endif; ?></li>
							<?php endforeach; ?></ol>
						<?php endif; ?>
						<?php if ( $byt01_updates ) : ?><h3><?php esc_html_e( 'سجل التحديثات', 'byt01' ); ?></h3><ul class="article-update-log"><?php foreach ( $byt01_updates as $byt01_update ) : ?><li><?php echo esc_html( $byt01_update ); ?></li><?php endforeach; ?></ul><?php endif; ?>
					</section>
				<?php endif; ?>
				<?php wp_link_pages( array( 'before' => '<nav class="page-links">' . esc_html__( 'الصفحات:', 'byt01' ), 'after' => '</nav>' ) ); ?>
				<?php $byt01_tags = get_the_tags(); if ( $byt01_tags ) : ?><div class="article-tags"><span><?php esc_html_e( 'وسوم:', 'byt01' ); ?></span><?php foreach ( $byt01_tags as $byt01_tag ) : ?><a href="<?php echo esc_url( get_tag_link( $byt01_tag ) ); ?>"><?php echo esc_html( $byt01_tag->name ); ?></a><?php endforeach; ?></div><?php endif; ?>
			</div>
		</div>

		<div class="container article-navigation">
			<?php the_post_navigation( array( 'prev_text' => '<span>' . esc_html__( 'المادة السابقة', 'byt01' ) . '</span><strong>%title</strong>', 'next_text' => '<span>' . esc_html__( 'المادة التالية', 'byt01' ) . '</span><strong>%title</strong>' ) ); ?>
		</div>
	</article>
	<?php if ( comments_open() || get_comments_number() ) : ?><div class="container comments-shell"><?php comments_template(); ?></div><?php endif; ?>
	<?php
endwhile;
get_footer();
