<?php
/**
 * Homepage section renderers (composition only — not an Engine).
 *
 * @package Kutby
 * @since   2.9.1
 */

declare(strict_types=1);

namespace Kutby\Homepage;

use Kutby\Cards\Card;
use Kutby\Helpers\Utils;
use Kutby\TemplateTags\Tags;

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

/**
 * Class Sections
 */
final class Sections {

	/**
	 * Semester navigation chips.
	 *
	 * @return void
	 */
	public static function semester_nav(): void {
		if ( ! Tags::show_section( 'semester_nav' ) ) {
			return;
		}
		$items = Catalog::semesters();
		?>
		<section class="kutby-home-section kutby-home-section--semesters" aria-labelledby="kutby-semesters-title">
			<div class="kutby-container">
				<?php SectionHeader::render( 'اختر الفصل الدراسي', 'تصفّح المحتوى حسب الفصل الحالي بكل سهولة.', 'kutby-semesters-title' ); ?>
				<nav class="kutby-semester-nav" aria-label="الفصول الدراسية">
					<ul class="kutby-semester-nav__list">
						<?php foreach ( $items as $i => $item ) : ?>
							<li>
								<a
									class="kutby-semester-nav__link<?php echo 0 === $i ? ' is-active' : ''; ?>"
									href="<?php echo esc_url( home_url( '/?s=' . rawurlencode( $item['label'] ) ) ); ?>"
									<?php echo 0 === $i ? ' aria-current="page"' : ''; ?>
								>
									<span class="kutby-semester-nav__icon" aria-hidden="true"><?php self::icon( 'calendar' ); ?></span>
									<?php echo esc_html( $item['label'] ); ?>
								</a>
							</li>
						<?php endforeach; ?>
					</ul>
				</nav>
			</div>
		</section>
		<?php
	}

	/**
	 * Grades grid.
	 *
	 * @return void
	 */
	public static function grades(): void {
		if ( ! Tags::show_section( 'grades' ) ) {
			return;
		}
		?>
		<section class="kutby-home-section kutby-home-section--grades" aria-labelledby="kutby-grades-title">
			<div class="kutby-container">
				<?php SectionHeader::render( 'المراحل والصفوف', 'اختر صفّك للوصول إلى الحلول وأوراق العمل المناسبة.', 'kutby-grades-title' ); ?>
				<div class="kutby-grades-grid">
					<?php foreach ( Catalog::grades() as $grade ) : ?>
						<a
							class="kutby-grade-card"
							href="<?php echo esc_url( home_url( '/?s=' . rawurlencode( $grade['label'] ) ) ); ?>"
						>
							<span class="kutby-grade-card__icon" aria-hidden="true"><?php self::icon( 'book' ); ?></span>
							<span class="kutby-grade-card__stage"><?php echo esc_html( $grade['stage'] ); ?></span>
							<span class="kutby-grade-card__label"><?php echo esc_html( $grade['label'] ); ?></span>
						</a>
					<?php endforeach; ?>
				</div>
			</div>
		</section>
		<?php
	}

	/**
	 * Subjects strip.
	 *
	 * @return void
	 */
	public static function subjects(): void {
		if ( ! Tags::show_section( 'subjects' ) && ! Tags::show_section( 'popular_subjects' ) ) {
			return;
		}
		?>
		<section class="kutby-home-section kutby-home-section--subjects" aria-labelledby="kutby-subjects-title">
			<div class="kutby-container">
				<?php SectionHeader::render( 'المواد الدراسية', 'مسارات سريعة لأشهر المواد التي يبحث عنها الطلاب.', 'kutby-subjects-title' ); ?>
				<ul class="kutby-subjects">
					<?php foreach ( Catalog::subjects() as $subject ) : ?>
						<li>
							<a class="kutby-subject-chip" href="<?php echo esc_url( home_url( '/?s=' . rawurlencode( $subject['label'] ) ) ); ?>">
								<span class="kutby-subject-chip__icon" aria-hidden="true"><?php self::icon( $subject['icon'] ); ?></span>
								<?php echo esc_html( $subject['label'] ); ?>
							</a>
						</li>
					<?php endforeach; ?>
				</ul>
			</div>
		</section>
		<?php
	}

	/**
	 * Latest educational files.
	 *
	 * @return void
	 */
	public static function latest_files(): void {
		if ( ! Tags::show_section( 'latest_files' ) ) {
			return;
		}

		$q = new \WP_Query(
			array(
				'posts_per_page'      => 4,
				'no_found_rows'       => true,
				'ignore_sticky_posts' => true,
				'category_name'       => 'kutby-files',
			)
		);

		if ( ! $q->have_posts() ) {
			$q = new \WP_Query(
				array(
					'posts_per_page'      => 4,
					'no_found_rows'       => true,
					'ignore_sticky_posts' => true,
				)
			);
		}
		?>
		<section class="kutby-home-section kutby-home-section--files" aria-labelledby="kutby-files-title">
			<div class="kutby-container">
				<?php
				$files_cat = get_category_by_slug( 'kutby-files' );
				SectionHeader::render(
					'أحدث الملفات التعليمية',
					'حلول كتب، اختبارات، وأوراق عمل محدثة للطلاب.',
					'kutby-files-title',
					$files_cat ? array(
						'url'   => get_category_link( (int) $files_cat->term_id ),
						'label' => 'عرض كل الملفات',
					) : array()
				);
				?>
				<?php if ( $q->have_posts() ) : ?>
					<div class="kutby-home-files">
						<?php
						while ( $q->have_posts() ) :
							$q->the_post();
							Card::render(
								'horizontal',
								get_post(),
								array(
									'ratio'          => '4x3',
									'show_excerpt'   => true,
									'show_read_more' => false,
									'heading'        => 'h3',
								)
							);
						endwhile;
						wp_reset_postdata();
						?>
					</div>
				<?php endif; ?>
			</div>
		</section>
		<?php
	}

	/**
	 * Latest articles.
	 *
	 * @return void
	 */
	public static function latest_articles(): void {
		if ( ! Tags::show_section( 'latest_articles' ) ) {
			return;
		}

		$q = new \WP_Query(
			array(
				'posts_per_page'      => 4,
				'no_found_rows'       => true,
				'ignore_sticky_posts' => true,
				'category_name'       => 'kutby-articles',
			)
		);

		if ( ! $q->have_posts() ) {
			$q = new \WP_Query(
				array(
					'posts_per_page' => 4,
					'no_found_rows'  => true,
				)
			);
		}

		$posts = $q->posts;
		?>
		<section class="kutby-home-section kutby-home-section--articles" aria-labelledby="kutby-articles-title">
			<div class="kutby-container">
				<?php
				$articles_cat = get_category_by_slug( 'kutby-articles' );
				SectionHeader::render(
					'أحدث المقالات',
					'نصائح ومقالات عملية لتنظيم المذاكرة والاستعداد للاختبارات.',
					'kutby-articles-title',
					$articles_cat ? array(
						'url'   => get_category_link( (int) $articles_cat->term_id ),
						'label' => 'عرض كل المقالات',
					) : array()
				);
				?>
				<?php if ( ! empty( $posts ) ) : ?>
					<div class="kutby-home-articles">
						<div class="kutby-home-articles__featured">
							<?php
							Card::render(
								'featured',
								$posts[0],
								array(
									'ratio'             => '16x9',
									'show_excerpt'      => false,
									'show_reading_time' => true,
									'show_bookmark'     => false,
									'lazy'              => false,
									'heading'           => 'h3',
								)
							);
							?>
						</div>
						<div class="kutby-home-articles__grid">
							<?php
							foreach ( array_slice( $posts, 1 ) as $post ) {
								Card::render(
									'grid',
									$post,
									array(
										'ratio'        => '16x9',
										'show_excerpt' => false,
										'heading'      => 'h3',
									)
								);
							}
							?>
						</div>
					</div>
				<?php endif; ?>
			</div>
		</section>
		<?php
		wp_reset_postdata();
	}

	/**
	 * Educational calendar.
	 *
	 * @return void
	 */
	public static function calendar(): void {
		if ( ! Tags::show_section( 'calendar' ) ) {
			return;
		}
		?>
		<section class="kutby-home-section kutby-home-section--calendar" aria-labelledby="kutby-calendar-title">
			<div class="kutby-container">
				<?php SectionHeader::render( 'التقويم التعليمي', 'مواعيد مهمة تساعدك على التخطيط للمذاكرة والاختبارات.', 'kutby-calendar-title' ); ?>
				<ol class="kutby-calendar">
					<?php foreach ( Catalog::calendar() as $event ) : ?>
						<li class="kutby-calendar__item kutby-calendar__item--<?php echo esc_attr( $event['type'] ); ?>">
							<span class="kutby-calendar__date"><?php echo esc_html( $event['date'] ); ?></span>
							<span class="kutby-calendar__title"><?php echo esc_html( $event['title'] ); ?></span>
						</li>
					<?php endforeach; ?>
				</ol>
			</div>
		</section>
		<?php
	}

	/**
	 * App banner.
	 *
	 * @return void
	 */
	public static function app_banner(): void {
		if ( ! Tags::show_section( 'app_banner' ) ) {
			return;
		}
		$app = Catalog::app();
		?>
		<section class="kutby-home-section kutby-home-section--app" aria-labelledby="kutby-app-title">
			<div class="kutby-container">
				<div class="kutby-app-banner">
					<div class="kutby-app-banner__copy">
						<p class="kutby-app-banner__eyebrow">متوفر قريباً</p>
						<h2 class="kutby-app-banner__title" id="kutby-app-title"><?php echo esc_html( $app['title'] ); ?></h2>
						<p class="kutby-app-banner__text"><?php echo esc_html( $app['text'] ); ?></p>
						<a class="kutby-btn kutby-btn--primary" href="<?php echo esc_url( home_url( '/' ) ); ?>">
							<?php echo esc_html( $app['cta'] ); ?>
						</a>
					</div>
					<div class="kutby-app-banner__visual" aria-hidden="true">
						<img
							src="<?php echo esc_url( Utils::asset( 'demo/placeholders/study.png' ) ); ?>"
							alt=""
							width="600"
							height="338"
							loading="lazy"
							decoding="async"
						/>
					</div>
				</div>
			</div>
		</section>
		<?php
	}

	/**
	 * About website.
	 *
	 * @return void
	 */
	public static function about(): void {
		if ( ! Tags::show_section( 'about' ) ) {
			return;
		}
		$about = Catalog::about();
		?>
		<section class="kutby-home-section kutby-home-section--about" aria-labelledby="kutby-about-title">
			<div class="kutby-container">
				<div class="kutby-about">
					<div class="kutby-about__content">
						<h2 class="kutby-about__title" id="kutby-about-title"><?php echo esc_html( $about['title'] ); ?></h2>
						<p class="kutby-about__text"><?php echo esc_html( $about['text'] ); ?></p>
					</div>
					<div class="kutby-about__panel" aria-hidden="true">
						<?php self::icon( 'book' ); ?>
						<span>محتوى عربي منظم للطلاب</span>
					</div>
				</div>
			</div>
		</section>
		<?php
	}

	/**
	 * Inline SVG icon (local, no CDN).
	 *
	 * @param string $name Icon key.
	 * @return void
	 */
	private static function icon( string $name ): void {
		$icons = array(
			'calendar' => '<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="1.75" aria-hidden="true"><rect x="3" y="5" width="18" height="16" rx="2"/><path d="M3 9h18M8 3v4M16 3v4"/></svg>',
			'book'     => '<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="1.75" aria-hidden="true"><path d="M4 5a2 2 0 0 1 2-2h12v18H6a2 2 0 0 1-2-2V5z"/><path d="M8 7h8M8 11h6"/></svg>',
			'math'     => '<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="1.75" aria-hidden="true"><path d="M5 8h6M8 5v6M14 7l5 5M19 7l-5 5M14 16h6"/></svg>',
			'science'  => '<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="1.75" aria-hidden="true"><path d="M9 3h6M10 3v6l-5 9a2 2 0 0 0 1.7 3h10.6a2 2 0 0 0 1.7-3l-5-9V3"/></svg>',
			'arabic'   => '<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="1.75" aria-hidden="true"><path d="M4 18c3-6 6-9 8-9s3 2 4 4 2 5 4 5"/><path d="M14 8c2 0 4-1 5-3"/></svg>',
			'english'  => '<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="1.75" aria-hidden="true"><circle cx="12" cy="12" r="9"/><path d="M3 12h18M12 3a14 14 0 0 1 0 18M12 3a14 14 0 0 0 0 18"/></svg>',
			'islamic'  => '<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="1.75" aria-hidden="true"><path d="M12 3v3M8 8l2 2M16 8l-2 2"/><path d="M7 20h10l-1-8H8l-1 8z"/></svg>',
			'computer' => '<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="1.75" aria-hidden="true"><rect x="3" y="5" width="18" height="12" rx="2"/><path d="M8 21h8M12 17v4"/></svg>',
		);

		echo $icons[ $name ] ?? $icons['book']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static SVG.
	}
}
