<?php
/**
 * Dynamic footer rendering.
 *
 * Replaces the footer template-part output with a fully
 * PHP-rendered footer that includes dynamic contact info,
 * social links, copyright, and proper layout ordering.
 *
 * @package Vitalisite_FSE
 * @since 1.0.0
 */

namespace Vitalisite_FSE;

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

/**
 * Replace the footer template-part with a fully rendered footer.
 */
function render_dynamic_footer( $block_content, $block ) {
	if ( 'core/template-part' !== $block['blockName'] ) {
		return $block_content;
	}
	if ( empty( $block['attrs']['slug'] ) || 'footer' !== $block['attrs']['slug'] ) {
		return $block_content;
	}

	$cabinet = get_option( 'vitalisite_cabinet', array() );
	$social  = get_option( 'vitalisite_social', array() );

	$contact_col = build_footer_contact_column( $cabinet );
	$social_html = build_footer_social_links( $social );
	$site_name   = get_bloginfo( 'name' );
	$year        = gmdate( 'Y' );

	ob_start();
	?>
	<footer class="vitalisite-footer" role="contentinfo">
		<div class="vitalisite-footer__inner">

			<div class="vitalisite-footer__columns">
				<div class="vitalisite-footer__col vitalisite-footer__col--brand">
					<p class="vitalisite-footer__site-name"><?php echo esc_html( $site_name ); ?></p>
					<p class="vitalisite-footer__tagline"><?php echo esc_html__( 'Un thème pensé pour inspirer confiance, avec des sections prêtes à l\'emploi et faciles à personnaliser.', 'vitalisite-fse' ); ?></p>
				</div>

				<div class="vitalisite-footer__col vitalisite-footer__col--nav">
					<p class="vitalisite-footer__heading"><?php echo esc_html__( 'Navigation', 'vitalisite-fse' ); ?></p>
					<ul class="vitalisite-footer__nav-list">
						<li><a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php echo esc_html__( 'Accueil', 'vitalisite-fse' ); ?></a></li>
						<li><a href="<?php echo esc_url( home_url( '/contact' ) ); ?>"><?php echo esc_html__( 'Contact', 'vitalisite-fse' ); ?></a></li>
					</ul>
				</div>

				<?php if ( $contact_col ) : ?>
				<div class="vitalisite-footer__col vitalisite-footer__col--contact">
					<?php echo $contact_col; ?>
				</div>
				<?php endif; ?>
			</div>

			<div class="vitalisite-footer__bottom">
				<p class="vitalisite-footer__copyright">© <?php echo esc_html( $year ); ?> <?php echo esc_html( $site_name ); ?>. <?php echo esc_html__( 'Tous droits réservés.', 'vitalisite-fse' ); ?></p>
				<?php if ( $social_html ) : ?>
					<?php echo $social_html; ?>
				<?php endif; ?>
			</div>

		</div>
	</footer>
	<?php
	return ob_get_clean();
}
add_filter( 'render_block', __NAMESPACE__ . '\render_dynamic_footer', 10, 2 );

/**
 * Build the contact info HTML for the footer.
 */
function build_footer_contact_column( $cabinet ) {
	$show_phone   = ! empty( $cabinet['show_phone_footer'] );
	$show_email   = ! empty( $cabinet['show_email_footer'] );
	$show_address = ! empty( $cabinet['show_address_footer'] );

	$lines = array();

	if ( $show_phone && ! empty( $cabinet['phone'] ) ) {
		$phone      = esc_html( $cabinet['phone'] );
		$phone_href = preg_replace( '/[^0-9+]/', '', $cabinet['phone'] );
		$lines[]    = '<li><a href="tel:' . esc_attr( $phone_href ) . '">' . $phone . '</a></li>';
	}

	if ( $show_email && ! empty( $cabinet['email'] ) ) {
		$email   = esc_html( $cabinet['email'] );
		$lines[] = '<li><a href="mailto:' . esc_attr( $cabinet['email'] ) . '">' . $email . '</a></li>';
	}

	if ( $show_address && ! empty( $cabinet['address'] ) ) {
		$lines[] = '<li>' . esc_html( $cabinet['address'] ) . '</li>';
	}

	if ( empty( $lines ) ) {
		return '';
	}

	$html  = '<p class="vitalisite-footer__heading">' . esc_html__( 'Contact', 'vitalisite-fse' ) . '</p>';
	$html .= '<ul class="vitalisite-footer__contact-list">';
	$html .= implode( '', $lines );
	$html .= '</ul>';

	return $html;
}

/**
 * Build the social links HTML for the footer.
 */
function build_footer_social_links( $social ) {
	$social_config = array(
		'facebook'    => array(
			'label' => 'Facebook',
			'svg'   => '<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M22 12c0-5.523-4.477-10-10-10S2 6.477 2 12c0 4.991 3.657 9.128 8.438 9.878v-6.987h-2.54V12h2.54V9.797c0-2.506 1.492-3.89 3.777-3.89 1.094 0 2.238.195 2.238.195v2.46h-1.26c-1.243 0-1.63.771-1.63 1.562V12h2.773l-.443 2.89h-2.33v6.988C18.343 21.128 22 16.991 22 12z"/></svg>',
		),
		'instagram'   => array(
			'label' => 'Instagram',
			'svg'   => '<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2.163c3.204 0 3.584.012 4.85.07 3.252.148 4.771 1.691 4.919 4.919.058 1.265.069 1.645.069 4.849 0 3.205-.012 3.584-.069 4.849-.149 3.225-1.664 4.771-4.919 4.919-1.266.058-1.644.07-4.85.07-3.204 0-3.584-.012-4.849-.07-3.26-.149-4.771-1.699-4.919-4.92-.058-1.265-.07-1.644-.07-4.849 0-3.204.013-3.583.07-4.849.149-3.227 1.664-4.771 4.919-4.919 1.266-.057 1.645-.069 4.849-.069zM12 0C8.741 0 8.333.014 7.053.072 2.695.272.273 2.69.073 7.052.014 8.333 0 8.741 0 12c0 3.259.014 3.668.072 4.948.2 4.358 2.618 6.78 6.98 6.98C8.333 23.986 8.741 24 12 24c3.259 0 3.668-.014 4.948-.072 4.354-.2 6.782-2.618 6.979-6.98.059-1.28.073-1.689.073-4.948 0-3.259-.014-3.667-.072-4.947-.196-4.354-2.617-6.78-6.979-6.98C15.668.014 15.259 0 12 0zm0 5.838a6.162 6.162 0 100 12.324 6.162 6.162 0 000-12.324zM12 16a4 4 0 110-8 4 4 0 010 8zm6.406-11.845a1.44 1.44 0 100 2.881 1.44 1.44 0 000-2.881z"/></svg>',
		),
		'linkedin'    => array(
			'label' => 'LinkedIn',
			'svg'   => '<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 01-2.063-2.065 2.064 2.064 0 112.063 2.065zm1.782 13.019H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z"/></svg>',
		),
		'doctolib'    => array(
			'label' => 'Doctolib',
			'svg'   => '<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-1 17.93c-3.95-.49-7-3.85-7-7.93 0-.62.08-1.21.21-1.79L9 15v1c0 1.1.9 2 2 2v1.93zm6.9-2.54c-.26-.81-1-1.39-1.9-1.39h-1v-3c0-.55-.45-1-1-1H8v-2h2c.55 0 1-.45 1-1V7h2c1.1 0 2-.9 2-2v-.41c2.93 1.19 5 4.06 5 7.41 0 2.08-.8 3.97-2.1 5.39z"/></svg>',
		),
		'google_maps' => array(
			'label' => 'Google Maps',
			'svg'   => '<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5a2.5 2.5 0 010-5 2.5 2.5 0 010 5z"/></svg>',
		),
	);

	$items = array();
	foreach ( $social_config as $key => $config ) {
		if ( ! empty( $social[ $key ] ) ) {
			$url     = esc_url( $social[ $key ] );
			$label   = esc_attr( $config['label'] );
			$items[] = '<a href="' . $url . '" target="_blank" rel="noopener noreferrer" aria-label="' . $label . '" class="vitalisite-footer__social-link">' . $config['svg'] . '</a>';
		}
	}

	if ( empty( $items ) ) {
		return '';
	}

	$html  = '<div class="vitalisite-footer__social">';
	$html .= implode( '', $items );
	$html .= '</div>';

	return $html;
}
