<?php
/**
 * Screenshot-faithful Sanctuary front page.
 *
 * @package Sanctuary
 */

defined( 'ABSPATH' ) || exit;

get_header();

$sanctuary_config   = function_exists( 'diy_church_child_config' ) ? diy_church_child_config() : array();
$sanctuary_demo     = isset( $sanctuary_config['demo'] ) ? $sanctuary_config['demo'] : array();
$sanctuary_showcase = isset( $sanctuary_demo['showcase'] ) ? $sanctuary_demo['showcase'] : array();
$sanctuary_sermons  = get_posts(
	array(
		'post_type'      => 'diy_sermon',
		'post_status'    => 'publish',
		'posts_per_page' => 3,
		'meta_key'       => '_diy_sermon_date',
		'orderby'        => 'meta_value',
		'order'          => 'DESC',
	)
);
$sanctuary_card_data = isset( $sanctuary_showcase['sermons'] ) ? $sanctuary_showcase['sermons'] : array();
$sanctuary_campuses  = array(
	array( 'name' => $sanctuary_demo['campus_one'] ?? 'Main Campus', 'address' => $sanctuary_demo['address_one'] ?? '', 'slug' => 'main-campus' ),
	array( 'name' => $sanctuary_demo['campus_two'] ?? 'North Campus', 'address' => $sanctuary_demo['address_two'] ?? '', 'slug' => 'north-campus' ),
);
$sanctuary_events = get_posts(
	array(
		'post_type'      => 'diy_event',
		'post_status'    => 'publish',
		'posts_per_page' => 3,
		'meta_key'       => '_diy_event_next_start',
		'orderby'        => 'meta_value_num',
		'order'          => 'ASC',
		'meta_query'     => array( array( 'key' => '_diy_event_next_start', 'value' => current_time( 'timestamp', true ), 'compare' => '>=', 'type' => 'NUMERIC' ) ),
	)
);
$sanctuary_portfolio = function_exists( 'sanctuary_demo_object_id' ) ? sanctuary_demo_object_id( 'portfolio_ministry', 'diy_portfolio' ) : 0;
if ( ! $sanctuary_portfolio ) {
	$sanctuary_portfolios = get_posts( array( 'post_type' => 'diy_portfolio', 'post_status' => 'publish', 'posts_per_page' => 1, 'fields' => 'ids' ) );
	$sanctuary_portfolio  = $sanctuary_portfolios ? absint( $sanctuary_portfolios[0] ) : 0;
}
$sanctuary_audio = function_exists( 'sanctuary_demo_object_id' ) ? sanctuary_demo_object_id( 'sermon_anchor', 'diy_sermon' ) : 0;
$sanctuary_video = function_exists( 'sanctuary_demo_object_id' ) ? sanctuary_demo_object_id( 'sermon_guest_voddie', 'diy_sermon' ) : 0;
?>

<main id="primary" class="sanctuary-home-main">
	<section class="sanctuary-showcase-hero" aria-labelledby="sanctuary-home-title">
		<div class="cw-container sanctuary-hero-inner">
			<div class="sanctuary-hero-copy">
				<h1 id="sanctuary-home-title"><span><?php esc_html_e( 'Exalting Christ.', 'sanctuary' ); ?></span><span><?php esc_html_e( 'Equipping Believers.', 'sanctuary' ); ?></span><span><?php esc_html_e( 'Extending Grace.', 'sanctuary' ); ?></span></h1>
				<div class="sanctuary-cross-rule" aria-hidden="true"><i></i><span>†</span><i></i></div>
				<p><?php esc_html_e( 'A church committed to the truth of God’s Word, the love of His people, and the glory of His name.', 'sanctuary' ); ?></p>
				<div class="sanctuary-hero-actions">
					<a class="sanctuary-button sanctuary-button-primary" href="<?php echo esc_url( home_url( '/new-here/' ) ); ?>"><?php esc_html_e( 'Plan Your Visit', 'sanctuary' ); ?></a>
					<a class="sanctuary-button sanctuary-button-outline" href="<?php echo esc_url( home_url( '/sermons/' ) ); ?>"><?php esc_html_e( 'Watch Sermons', 'sanctuary' ); ?><span aria-hidden="true">▶</span></a>
				</div>
			</div>
		</div>
	</section>

	<section class="sanctuary-showcase-content" aria-label="<?php esc_attr_e( 'Visit and recent sermons', 'sanctuary' ); ?>">
		<div class="cw-container sanctuary-showcase-grid">
			<section class="sanctuary-weekend" aria-labelledby="sanctuary-weekend-title">
				<div class="sanctuary-section-icon sanctuary-church-icon" aria-hidden="true">⌂</div>
				<h2 id="sanctuary-weekend-title"><?php esc_html_e( 'Join Us This Weekend', 'sanctuary' ); ?></h2>
				<div class="sanctuary-campus-grid">
					<?php foreach ( $sanctuary_campuses as $sanctuary_campus ) : ?>
						<article class="sanctuary-campus-card">
							<div class="sanctuary-card-church" aria-hidden="true"><span>†</span></div>
							<h3><?php echo esc_html( $sanctuary_campus['name'] ); ?></h3>
							<address><?php echo esc_html( $sanctuary_campus['address'] ); ?></address>
							<hr>
							<dl><div><dt><?php esc_html_e( 'Sunday School', 'sanctuary' ); ?></dt><dd><?php esc_html_e( '9:00 AM', 'sanctuary' ); ?></dd></div><div><dt><?php esc_html_e( 'Worship Service', 'sanctuary' ); ?></dt><dd><?php esc_html_e( '11:00 AM', 'sanctuary' ); ?></dd></div></dl>
							<a href="<?php echo esc_url( home_url( '/campuses/' ) ); ?>"><?php esc_html_e( 'Get Directions', 'sanctuary' ); ?><span aria-hidden="true">›</span></a>
						</article>
					<?php endforeach; ?>
				</div>
			</section>

			<section class="sanctuary-latest" aria-labelledby="sanctuary-latest-title">
				<div class="sanctuary-section-icon sanctuary-book-icon" aria-hidden="true">▭</div>
				<h2 id="sanctuary-latest-title"><?php esc_html_e( 'Latest Sermons', 'sanctuary' ); ?></h2>
				<div class="sanctuary-sermon-grid">
					<?php for ( $sanctuary_index = 0; $sanctuary_index < 3; $sanctuary_index++ ) :
						$sanctuary_sermon = $sanctuary_sermons[ $sanctuary_index ] ?? null;
						$sanctuary_card   = $sanctuary_card_data[ $sanctuary_index ] ?? array();
						$sanctuary_link   = $sanctuary_sermon ? get_permalink( $sanctuary_sermon ) : home_url( '/sermons/' );
						$sanctuary_date   = $sanctuary_sermon ? get_post_meta( $sanctuary_sermon->ID, '_diy_sermon_date', true ) : '';
						$sanctuary_image  = get_stylesheet_directory_uri() . '/assets/images/' . ( $sanctuary_card['image'] ?? 'sermon-bible.webp' );
					?>
						<article class="sanctuary-sermon-card">
							<a class="sanctuary-sermon-image" href="<?php echo esc_url( $sanctuary_link ); ?>"><img src="<?php echo esc_url( $sanctuary_image ); ?>" alt=""><span aria-hidden="true">▶</span></a>
							<div class="sanctuary-sermon-copy">
								<h3><a href="<?php echo esc_url( $sanctuary_link ); ?>"><?php echo esc_html( $sanctuary_card['title'] ?? ( $sanctuary_sermon ? get_the_title( $sanctuary_sermon ) : '' ) ); ?></a></h3>
								<p class="sanctuary-reference"><?php echo esc_html( $sanctuary_card['reference'] ?? '' ); ?></p>
								<p class="sanctuary-sermon-meta"><?php echo esc_html( $sanctuary_date ? wp_date( 'M j, Y', strtotime( $sanctuary_date ) ) : '' ); ?> <span>•</span> <?php esc_html_e( 'Pastor Jonathan Reed', 'sanctuary' ); ?></p>
								<a class="sanctuary-watch-link" href="<?php echo esc_url( $sanctuary_link ); ?>"><?php esc_html_e( 'Watch or Listen', 'sanctuary' ); ?><span aria-hidden="true">›</span></a>
							</div>
						</article>
					<?php endfor; ?>
				</div>
			</section>
		</div>
	</section>

	<section class="sanctuary-purpose" aria-labelledby="sanctuary-purpose-title">
		<div class="cw-container sanctuary-purpose-grid">
			<div class="sanctuary-purpose-heading">
				<p class="sanctuary-eyebrow"><?php esc_html_e( 'Why we gather', 'sanctuary' ); ?></p>
				<h2 id="sanctuary-purpose-title"><?php esc_html_e( 'The glory of God. The good of our neighbors.', 'sanctuary' ); ?></h2>
			</div>
			<div class="sanctuary-purpose-copy">
				<p><?php echo esc_html( $sanctuary_demo['mission'] ?? 'We glorify God through Christ-centered worship, expository preaching, earnest prayer, and loving service to our neighbors.' ); ?></p>
				<blockquote><p><?php esc_html_e( '“And they continued stedfastly in the apostles’ doctrine and fellowship, and in breaking of bread, and in prayers.”', 'sanctuary' ); ?></p><cite><?php esc_html_e( 'Acts 2:42, KJV', 'sanctuary' ); ?></cite></blockquote>
			</div>
		</div>
	</section>

	<?php if ( $sanctuary_portfolio && shortcode_exists( 'diy_portfolio' ) ) : ?>
		<section class="sanctuary-portfolio" aria-labelledby="sanctuary-portfolio-title">
			<div class="cw-container">
				<div class="sanctuary-section-heading sanctuary-section-heading-light">
					<p class="sanctuary-eyebrow"><?php esc_html_e( 'Ministry in motion', 'sanctuary' ); ?></p>
					<h2 id="sanctuary-portfolio-title"><?php esc_html_e( 'Faith lived together', 'sanctuary' ); ?></h2>
					<p><?php esc_html_e( 'Worship, discipleship, compassion, and witness are not programs on a calendar. They are the shared life of a church following Christ.', 'sanctuary' ); ?></p>
				</div>
				<?php echo do_shortcode( '[diy_portfolio id="' . absint( $sanctuary_portfolio ) . '"]' ); ?>
			</div>
		</section>
	<?php endif; ?>

	<section class="sanctuary-events" aria-labelledby="sanctuary-events-title">
		<div class="cw-container">
			<div class="sanctuary-section-heading">
				<p class="sanctuary-eyebrow"><?php esc_html_e( 'Life together', 'sanctuary' ); ?></p>
				<h2 id="sanctuary-events-title"><?php esc_html_e( 'Upcoming events', 'sanctuary' ); ?></h2>
				<a href="<?php echo esc_url( home_url( '/events/' ) ); ?>"><?php esc_html_e( 'View the full calendar', 'sanctuary' ); ?><span aria-hidden="true">›</span></a>
			</div>
			<?php if ( $sanctuary_events ) : ?>
				<div class="sanctuary-event-grid">
					<?php foreach ( $sanctuary_events as $sanctuary_event ) :
						$sanctuary_event_date = get_post_meta( $sanctuary_event->ID, '_diy_event_start_date', true );
						$sanctuary_event_terms = get_the_terms( $sanctuary_event->ID, 'diy_event_category' );
					?>
						<article class="sanctuary-event-card">
							<a class="sanctuary-event-image" href="<?php echo esc_url( get_permalink( $sanctuary_event ) ); ?>"><?php echo get_the_post_thumbnail( $sanctuary_event, 'large', array( 'alt' => '' ) ); ?></a>
							<div class="sanctuary-event-copy">
								<p class="sanctuary-event-kicker"><?php echo esc_html( $sanctuary_event_terms && ! is_wp_error( $sanctuary_event_terms ) ? $sanctuary_event_terms[0]->name : __( 'Church Life', 'sanctuary' ) ); ?></p>
								<h3><a href="<?php echo esc_url( get_permalink( $sanctuary_event ) ); ?>"><?php echo esc_html( get_the_title( $sanctuary_event ) ); ?></a></h3>
								<p><?php echo esc_html( $sanctuary_event_date ? wp_date( 'l, F j', strtotime( $sanctuary_event_date ) ) : '' ); ?></p>
							</div>
						</article>
					<?php endforeach; ?>
				</div>
			<?php else : ?>
				<p class="sanctuary-empty-note"><?php esc_html_e( 'New gatherings and opportunities to serve will be posted here.', 'sanctuary' ); ?></p>
			<?php endif; ?>
		</div>
	</section>

	<?php if ( $sanctuary_audio || $sanctuary_video ) : ?>
		<section class="sanctuary-media" aria-labelledby="sanctuary-media-title">
			<div class="cw-container sanctuary-media-grid">
				<div class="sanctuary-media-intro">
					<p class="sanctuary-eyebrow"><?php esc_html_e( 'Watch or listen', 'sanctuary' ); ?></p>
					<h2 id="sanctuary-media-title"><?php esc_html_e( 'Truth for the road ahead', 'sanctuary' ); ?></h2>
					<p><?php esc_html_e( 'Take biblical teaching with you through video, audio, the sermon archive, and the church podcast feed.', 'sanctuary' ); ?></p>
					<a class="sanctuary-button sanctuary-button-primary" href="<?php echo esc_url( home_url( '/sermons/' ) ); ?>"><?php esc_html_e( 'Explore All Sermons', 'sanctuary' ); ?></a>
				</div>
				<div class="sanctuary-media-stack">
					<?php if ( $sanctuary_video && shortcode_exists( 'diy_sermon_player' ) ) : ?>
						<article class="sanctuary-media-card"><p><?php esc_html_e( 'Featured video', 'sanctuary' ); ?></p><h3><?php echo esc_html( get_the_title( $sanctuary_video ) ); ?></h3><?php echo do_shortcode( '[diy_sermon_player id="' . absint( $sanctuary_video ) . '"]' ); ?></article>
					<?php endif; ?>
					<?php if ( $sanctuary_audio && shortcode_exists( 'diy_sermon_player' ) ) : ?>
						<article class="sanctuary-media-card sanctuary-media-card-audio"><p><?php esc_html_e( 'Audio message', 'sanctuary' ); ?></p><h3><?php echo esc_html( get_the_title( $sanctuary_audio ) ); ?></h3><?php echo do_shortcode( '[diy_sermon_player compact="yes" id="' . absint( $sanctuary_audio ) . '"]' ); ?></article>
					<?php endif; ?>
				</div>
			</div>
		</section>
	<?php endif; ?>

	<section class="sanctuary-next" aria-labelledby="sanctuary-next-title">
		<div class="cw-container">
			<div class="sanctuary-section-heading">
				<p class="sanctuary-eyebrow"><?php esc_html_e( 'Take a next step', 'sanctuary' ); ?></p>
				<h2 id="sanctuary-next-title"><?php esc_html_e( 'There is a place for you here', 'sanctuary' ); ?></h2>
			</div>
			<div class="sanctuary-next-grid">
				<a class="sanctuary-next-visit" href="<?php echo esc_url( home_url( '/new-here/' ) ); ?>"><span class="sanctuary-next-number">01</span><span class="sanctuary-next-icon" aria-hidden="true">⌂</span><h3><?php esc_html_e( 'Plan a Visit', 'sanctuary' ); ?></h3><p><?php esc_html_e( 'Service times, directions, children’s check-in, and what to expect.', 'sanctuary' ); ?></p><i aria-hidden="true">›</i></a>
				<a class="sanctuary-next-community" href="<?php echo esc_url( home_url( '/ministries/' ) ); ?>"><span class="sanctuary-next-number">02</span><span class="sanctuary-next-icon" aria-hidden="true">○</span><h3><?php esc_html_e( 'Find Community', 'sanctuary' ); ?></h3><p><?php esc_html_e( 'Grow in Scripture and friendship through groups for every generation.', 'sanctuary' ); ?></p><i aria-hidden="true">›</i></a>
				<a class="sanctuary-next-prayer" href="<?php echo esc_url( home_url( '/prayer/' ) ); ?>"><span class="sanctuary-next-number">03</span><span class="sanctuary-next-icon" aria-hidden="true">†</span><h3><?php esc_html_e( 'Request Prayer', 'sanctuary' ); ?></h3><p><?php esc_html_e( 'Share a confidential need with pastors and trusted prayer volunteers.', 'sanctuary' ); ?></p><i aria-hidden="true">›</i></a>
				<a class="sanctuary-next-serve" href="<?php echo esc_url( home_url( '/ministries/' ) ); ?>"><span class="sanctuary-next-number">04</span><span class="sanctuary-next-icon" aria-hidden="true">♡</span><h3><?php esc_html_e( 'Serve with Us', 'sanctuary' ); ?></h3><p><?php esc_html_e( 'Use your gifts in the church, our neighborhoods, and gospel missions.', 'sanctuary' ); ?></p><i aria-hidden="true">›</i></a>
			</div>
		</div>
	</section>

	<section class="sanctuary-invitation" aria-labelledby="sanctuary-invitation-title">
		<div class="cw-container sanctuary-invitation-inner">
			<div><p class="sanctuary-eyebrow"><?php esc_html_e( 'This Sunday', 'sanctuary' ); ?></p><h2 id="sanctuary-invitation-title"><?php esc_html_e( 'Come worship Christ with us.', 'sanctuary' ); ?></h2></div>
			<p><?php esc_html_e( 'Friendly volunteers will help you find a comfortable seat, check in your children, and get acquainted at your own pace.', 'sanctuary' ); ?></p>
			<a class="sanctuary-button sanctuary-button-outline" href="<?php echo esc_url( home_url( '/new-here/' ) ); ?>"><?php esc_html_e( 'Plan Your Visit', 'sanctuary' ); ?></a>
		</div>
	</section>

</main>

<?php get_footer(); ?>
