<?php
/**
 * Reusable front-end sections and section library (bundle).
 *
 * @package EasySite_Pro
 */

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

/**
 * Check if a bundle section is enabled.
 *
 * @param string $key Section key.
 * @return bool
 */
function easysite_section_enabled( $key ) {
	if ( ! easysite_is_bundle() ) {
		return false;
	}
	return (bool) easysite_get_mod( 'easysite_section_' . $key, true );
}

/**
 * Basic pricing table (all tiers).
 */
function easysite_section_pricing_table_basic() {
	?>
	<section class="easysite-section easysite-pricing easysite-pricing-basic">
		<div class="easysite-container">
			<h2 class="easysite-section-title"><?php esc_html_e( 'Simple Pricing', 'easysite-pro' ); ?></h2>
			<div class="easysite-pricing-grid">
				<div class="easysite-pricing-card">
					<h3><?php esc_html_e( 'Basic', 'easysite-pro' ); ?></h3>
					<p class="easysite-price"><?php esc_html_e( 'From £39', 'easysite-pro' ); ?></p>
					<p><?php esc_html_e( 'Great for getting started.', 'easysite-pro' ); ?></p>
				</div>
				<div class="easysite-pricing-card is-featured">
					<h3><?php esc_html_e( 'Standard', 'easysite-pro' ); ?></h3>
					<p class="easysite-price"><?php esc_html_e( 'From £79', 'easysite-pro' ); ?></p>
					<p><?php esc_html_e( 'Our most popular option.', 'easysite-pro' ); ?></p>
				</div>
				<div class="easysite-pricing-card">
					<h3><?php esc_html_e( 'Premium', 'easysite-pro' ); ?></h3>
					<p class="easysite-price"><?php esc_html_e( 'From £119', 'easysite-pro' ); ?></p>
					<p><?php esc_html_e( 'Full service with extra support.', 'easysite-pro' ); ?></p>
				</div>
			</div>
		</div>
	</section>
	<?php
}

/**
 * Render pricing table section.
 */
function easysite_section_pricing_table() {
	if ( easysite_is_bundle() && easysite_section_enabled( 'pricing_tables' ) ) {
		$plans = array(
			array(
				'name'     => __( 'Starter', 'easysite-pro' ),
				'price'    => '£25',
				'features' => array( __( '1 page', 'easysite-pro' ), __( 'Email support', 'easysite-pro' ) ),
			),
			array(
				'name'     => __( 'Professional', 'easysite-pro' ),
				'price'    => '£49',
				'features' => array( __( '5 pages', 'easysite-pro' ), __( 'Priority support', 'easysite-pro' ) ),
				'featured' => true,
			),
			array(
				'name'     => __( 'Business', 'easysite-pro' ),
				'price'    => '£79',
				'features' => array( __( 'Unlimited pages', 'easysite-pro' ), __( 'Phone support', 'easysite-pro' ) ),
			),
		);
		?>
		<section class="easysite-section easysite-pricing">
			<div class="easysite-container">
				<h2 class="easysite-section-title"><?php esc_html_e( 'Simple Pricing', 'easysite-pro' ); ?></h2>
				<div class="easysite-pricing-grid">
					<?php foreach ( $plans as $plan ) : ?>
						<div class="easysite-pricing-card <?php echo ! empty( $plan['featured'] ) ? 'is-featured' : ''; ?>">
							<h3><?php echo esc_html( $plan['name'] ); ?></h3>
							<p class="easysite-price"><?php echo esc_html( $plan['price'] ); ?></p>
							<ul>
								<?php foreach ( $plan['features'] as $feature ) : ?>
									<li><?php echo esc_html( $feature ); ?></li>
								<?php endforeach; ?>
							</ul>
							<a href="<?php echo esc_url( home_url( '/contact' ) ); ?>" class="easysite-btn"><?php esc_html_e( 'Get Started', 'easysite-pro' ); ?></a>
						</div>
					<?php endforeach; ?>
				</div>
			</div>
		</section>
		<?php
		return;
	}

	easysite_section_pricing_table_basic();
}

/**
 * Render FAQ section.
 *
 * @param array $items FAQ items.
 */
function easysite_section_faq( $items = array() ) {
	if ( easysite_is_bundle() && ! easysite_section_enabled( 'faq_blocks' ) ) {
		return;
	}

	if ( empty( $items ) ) {
		$items = array();
		for ( $i = 1; $i <= 6; $i++ ) {
			$q = easysite_get_mod( "easysite_faq_{$i}_q" );
			$a = easysite_get_mod( "easysite_faq_{$i}_a" );
			if ( $q && $a ) {
				$items[] = array( 'q' => $q, 'a' => $a );
			}
		}
		if ( empty( $items ) ) {
			$items = array(
				array( 'q' => __( 'How do I get started?', 'easysite-pro' ), 'a' => __( 'Contact us and we will guide you through every step.', 'easysite-pro' ) ),
				array( 'q' => __( 'What areas do you serve?', 'easysite-pro' ), 'a' => __( 'We work with customers locally and online.', 'easysite-pro' ) ),
			);
		}
	}
	?>
	<section class="easysite-section easysite-faq">
		<div class="easysite-container">
			<h2 class="easysite-section-title"><?php esc_html_e( 'Frequently Asked Questions', 'easysite-pro' ); ?></h2>
			<div class="easysite-faq-list">
				<?php foreach ( $items as $item ) : ?>
					<details class="easysite-faq-item">
						<summary><?php echo esc_html( $item['q'] ); ?></summary>
						<p><?php echo esc_html( $item['a'] ); ?></p>
					</details>
				<?php endforeach; ?>
			</div>
		</div>
	</section>
	<?php
}

/**
 * Display basic testimonials from customizer (all tiers).
 */
function easysite_display_basic_testimonials() {
	if ( ! easysite_get_mod( 'easysite_testimonials_show', true ) ) {
		return;
	}
	?>
	<section class="easysite-section easysite-testimonials">
		<div class="easysite-container">
			<h2 class="easysite-section-title"><?php echo esc_html( easysite_get_mod( 'easysite_testimonials_title', __( 'What Our Customers Say', 'easysite-pro' ) ) ); ?></h2>
			<div class="easysite-testimonials-grid">
				<?php for ( $i = 1; $i <= 3; $i++ ) :
					$name  = easysite_get_mod( "easysite_testimonial_{$i}_name" );
					$quote = easysite_get_mod( "easysite_testimonial_{$i}_quote" );
					if ( ! $name || ! $quote ) {
						continue;
					}
					?>
					<blockquote class="easysite-testimonial-card">
						<p class="easysite-quote"><?php echo esc_html( $quote ); ?></p>
						<cite><?php echo esc_html( $name ); ?> — <?php echo esc_html( easysite_get_mod( "easysite_testimonial_{$i}_role" ) ); ?></cite>
					</blockquote>
				<?php endfor; ?>
			</div>
		</div>
	</section>
	<?php
}

/**
 * Social sharing buttons (bundle).
 */
function easysite_social_share_buttons() {
	if ( ! easysite_is_bundle() ) {
		return;
	}
	$url   = rawurlencode( get_permalink() );
	$title = rawurlencode( get_the_title() );
	?>
	<div class="easysite-share">
		<span><?php esc_html_e( 'Share:', 'easysite-pro' ); ?></span>
		<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo esc_attr( $url ); ?>" target="_blank" rel="noopener">Facebook</a>
		<a href="https://twitter.com/intent/tweet?url=<?php echo esc_attr( $url ); ?>&text=<?php echo esc_attr( $title ); ?>" target="_blank" rel="noopener">X</a>
		<a href="https://www.linkedin.com/sharing/share-offsite/?url=<?php echo esc_attr( $url ); ?>" target="_blank" rel="noopener">LinkedIn</a>
	</div>
	<?php
}

/**
 * Get SVG icon from library.
 *
 * @param string $name Icon name.
 * @return string
 */
function easysite_icon( $name ) {
	$path = EASYSITE_PRO_DIR . '/assets/images/icons/' . sanitize_file_name( $name ) . '.svg';
	if ( ! file_exists( $path ) ) {
		return '';
	}
	return file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
}
