<?php
/**
 * Vitalisite FSE – Theme functions.
 *
 * @package Vitalisite_FSE
 */

namespace Vitalisite_FSE;

/**
 * Theme version constant.
 */
define( 'VITALISITE_FSE_VERSION', wp_get_theme()->get( 'Version' ) );

// Include CPT registrations (global namespace – class-based).
require_once __DIR__ . '/inc/cpt-testimonials.php';
require_once __DIR__ . '/inc/cpt-specialities.php';

// Include block render callbacks (global namespace – referenced by string).
require_once __DIR__ . '/inc/block-testimonials.php';
require_once __DIR__ . '/inc/block-video.php';
require_once __DIR__ . '/inc/block-before-after.php';
require_once __DIR__ . '/inc/block-opening-hours.php';
require_once __DIR__ . '/inc/block-pricing.php';

// Patterns & blocks registration.
require_once __DIR__ . '/inc/patterns.php';

// Admin settings page.
require_once __DIR__ . '/inc/admin-settings.php';

// Dynamic footer content injection.
require_once __DIR__ . '/inc/footer-dynamic.php';

// Features: announcement banner + sticky CTA + links page + social links shortcode.
require_once __DIR__ . '/inc/feature-banner.php';
require_once __DIR__ . '/inc/feature-sticky-cta.php';
require_once __DIR__ . '/inc/feature-links-page.php';
require_once __DIR__ . '/inc/feature-social-links.php';

// AJAX contact form handler.
require_once __DIR__ . '/inc/ajax-contact-form.php';

// Block render callback: contact form.
require_once __DIR__ . '/inc/block-contact-form.php';

// Google Reviews integration.
require_once __DIR__ . '/inc/google-reviews-functions.php';

// Debug visuel couleurs (WP_DEBUG uniquement).
// require_once __DIR__ . '/inc/debug-colors.php';

// License system & setup wizard.
require_once __DIR__ . '/inc/license.php';
require_once __DIR__ . '/inc/demo-pages.php';
require_once __DIR__ . '/inc/setup-wizard.php';
require_once __DIR__ . '/inc/theme-updater.php';

/**
 * Check if the site is running in development mode.
 */
function is_dev_mode() {
	if ( defined( 'VITALISITE_DEV_MODE' ) && VITALISITE_DEV_MODE ) {
		return true;
	}
	if ( function_exists( 'wp_get_environment_type' ) ) {
		return wp_get_environment_type() === 'local';
	}
	return false;
}

/**
 * Return a theme asset URL with automatic cache busting based on file mtime.
 *
 * @param string $relative_path Relative path from the theme root.
 * @return string
 */
function theme_asset_uri( $relative_path ) {
	$relative_path = ltrim( (string) $relative_path, '/' );
	$file_path     = get_theme_file_path( $relative_path );
	$file_uri      = get_theme_file_uri( $relative_path );

	if ( ! file_exists( $file_path ) ) {
		return $file_uri;
	}

	return add_query_arg( 'ver', (string) filemtime( $file_path ), $file_uri );
}

/**
 * Refresh placeholder image URLs inside generated pages when theme images change.
 *
 * This rewrites stored image URLs in page content so browsers receive a new URL
 * whenever a placeholder asset is replaced.
 */
function maybe_refresh_placeholder_image_urls() {
	if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
		return;
	}

	$placeholder_files = glob( get_template_directory() . '/assets/images/placeholder-slider-*.jpg' );

	if ( empty( $placeholder_files ) ) {
		return;
	}

	sort( $placeholder_files );

	$signature_parts = array();
	$replacements    = array(
		'https://images.unsplash.com/photo-1576091160399-112ba8d25d1d?w=800&q=80' => theme_asset_uri( 'assets/images/placeholder-slider-5.jpg' ),
	);

	foreach ( $placeholder_files as $file_path ) {
		$basename            = basename( $file_path );
		$signature_parts[]   = $basename . ':' . filemtime( $file_path );
		$replacements[ $basename ] = theme_asset_uri( 'assets/images/' . $basename );
	}

	$signature      = md5( implode( '|', $signature_parts ) );
	$stored_version = (string) get_option( 'vitalisite_placeholder_images_signature', '' );

	if ( $signature === $stored_version ) {
		return;
	}

	$page_ids = get_posts(
		array(
			'post_type'              => 'page',
			'post_status'            => array( 'publish', 'draft', 'pending', 'private' ),
			'posts_per_page'         => -1,
			'fields'                 => 'ids',
			'no_found_rows'          => true,
			'update_post_meta_cache' => false,
			'update_post_term_cache' => false,
		)
	);

	$theme_slug = preg_quote( get_stylesheet(), '#' );

	foreach ( $page_ids as $page_id ) {
		$content = (string) get_post_field( 'post_content', $page_id );
		$updated = $content;

		foreach ( $replacements as $needle => $replacement ) {
			if ( 0 === strpos( $needle, 'http' ) ) {
				$updated = str_replace( $needle, $replacement, $updated );
				continue;
			}

			$pattern = '#https?://[^"\'\\s)]+/wp-content/themes/' . $theme_slug . '/assets/images/' . preg_quote( $needle, '#' ) . '(?:\\?ver=[^"\'\\s)]+)?#';
			$updated = preg_replace( $pattern, $replacement, $updated );
		}

		if ( $updated === $content ) {
			continue;
		}

		wp_update_post(
			array(
				'ID'           => $page_id,
				'post_content' => $updated,
			)
		);
	}

	update_option( 'vitalisite_placeholder_images_signature', $signature );
}
add_action( 'admin_init', __NAMESPACE__ . '\maybe_refresh_placeholder_image_urls' );

/**
 * Theme setup: editor styles, remove core patterns.
 */
function setup() {
	// Auto-detect all CSS files in assets/styles/ for the editor.
	$files       = glob( get_template_directory() . '/assets/styles/*.css' );
	$editor_css  = array();
	foreach ( $files as $file ) {
		$editor_css[] = 'assets/styles/' . basename( $file );
	}
	add_editor_style( $editor_css );

	// Hide core block patterns to keep the inserter focused on theme patterns.
	remove_theme_support( 'core-block-patterns' );
}
add_action( 'after_setup_theme', __NAMESPACE__ . '\setup' );

// Disable remote patterns from the WordPress.org pattern directory.
add_filter( 'should_load_remote_block_patterns', '__return_false' );

/**
 * Add an early class on the root element to prevent reveal flashes before GSAP
 * has initialized. A timeout removes it automatically if JS/GSAP fails.
 */
function print_motion_bootstrap_script() {
	?>
	<script>
		(function () {
			if (
				window.matchMedia &&
				window.matchMedia("(prefers-reduced-motion: reduce)").matches
			) {
				return;
			}

			var root = document.documentElement;
			root.classList.add("vitalisite-motion-pending");

			window.setTimeout(function () {
				root.classList.remove("vitalisite-motion-pending");
			}, 3000);
		})();
	</script>
	<?php
}
add_action( 'wp_head', __NAMESPACE__ . '\print_motion_bootstrap_script', 1 );

/**
 * Change the post-content block wrapper from <div> to <main>.
 */
function post_content_use_main_tag( $block_content, $block ) {
	if ( 'core/post-content' !== $block['blockName'] ) {
		return $block_content;
	}
	return preg_replace(
		array( '/^<div\b/', '/<\/div>\s*$/' ),
		array( '<main', '</main>' ),
		$block_content
	);
}
add_filter( 'render_block', __NAMESPACE__ . '\post_content_use_main_tag', 10, 2 );

/**
 * Add a body class matching the active style variation slug.
 *
 * Compares the active primary color against each variation file to detect
 * which one is selected. Outputs: vitalisite-style--{slug} (e.g. vitalisite-style--mineral).
 * Falls back to vitalisite-style--default when no variation matches.
 */
function add_style_variation_body_class( $classes ) {
	$active_primary = wp_get_global_settings( array( 'color', 'palette', 'theme' ) );
	$current_primary = '';

	if ( is_array( $active_primary ) ) {
		foreach ( $active_primary as $color ) {
			if ( isset( $color['slug'] ) && 'primary' === $color['slug'] ) {
				$current_primary = strtolower( trim( $color['color'] ) );
				break;
			}
		}
	}

	$variation_slug = 'default';

	if ( $current_primary ) {
		$styles_dir = get_template_directory() . '/styles';
		foreach ( glob( $styles_dir . '/*.json' ) as $file ) {
			$data = json_decode( file_get_contents( $file ), true );
			if ( ! isset( $data['settings']['color']['palette'] ) ) {
				continue;
			}
			foreach ( $data['settings']['color']['palette'] as $color ) {
				if ( isset( $color['slug'] ) && 'primary' === $color['slug'] ) {
					if ( strtolower( trim( $color['color'] ) ) === $current_primary ) {
						$variation_slug = basename( $file, '.json' );
					}
					break;
				}
			}
		}
	}

	$classes[] = 'vitalisite-style--' . sanitize_html_class( $variation_slug );
	return $classes;
}
add_filter( 'body_class', __NAMESPACE__ . '\add_style_variation_body_class' );

/**
 * Enqueue global styles (theme stylesheet + utilities + layout CSS).
 */
function enqueue_global_styles() {
	$version = VITALISITE_FSE_VERSION;
	$uri     = get_template_directory_uri();

	wp_enqueue_style( 'vitalisite-fse', $uri . '/style.css', array(), $version );
	wp_enqueue_style( 'vitalisite-fse-utilities', $uri . '/assets/styles/utilities.css', array( 'vitalisite-fse' ), $version );
	wp_enqueue_style( 'vitalisite-fse-header', $uri . '/assets/styles/header.css', array( 'vitalisite-fse' ), $version );
	wp_enqueue_style( 'vitalisite-fse-hero', $uri . '/assets/styles/hero.css', array( 'vitalisite-fse' ), $version );
	wp_enqueue_style( 'vitalisite-fse-bento', $uri . '/assets/styles/bento.css', array( 'vitalisite-fse' ), $version );
	wp_enqueue_style( 'vitalisite-fse-footer', $uri . '/assets/styles/footer.css', array( 'vitalisite-fse' ), $version );
	wp_enqueue_style( 'vitalisite-fse-features', $uri . '/assets/styles/features.css', array( 'vitalisite-fse' ), $version );
	wp_enqueue_style( 'vitalisite-fse-pricing', $uri . '/assets/styles/pricing.css', array( 'vitalisite-fse' ), $version );
	wp_enqueue_style( 'vitalisite-fse-doctor', $uri . '/assets/styles/doctor.css', array( 'vitalisite-fse' ), $version );
	wp_enqueue_style( 'vitalisite-fse-text-simple', $uri . '/assets/styles/text-simple.css', array( 'vitalisite-fse' ), $version );
	wp_enqueue_style( 'vitalisite-fse-blog-listing', $uri . '/assets/styles/blog-listing.css', array( 'vitalisite-fse' ), $version );
	wp_enqueue_style( 'vitalisite-fse-blog-single', $uri . '/assets/styles/blog-single.css', array( 'vitalisite-fse' ), $version );
	wp_enqueue_style( 'vitalisite-fse-contact-form', $uri . '/assets/styles/contact-form.css', array( 'vitalisite-fse' ), $version );

	if ( is_page() && 'template-links' === get_page_template_slug() ) {
		wp_enqueue_style( 'vitalisite-fse-links-page', $uri . '/assets/styles/links-page.css', array( 'vitalisite-fse' ), $version );
	}
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_global_styles' );

/**
 * Enqueue block-specific styles only when the block is used.
 */
function enqueue_block_styles() {
	$version = VITALISITE_FSE_VERSION;

	$block_styles = array(
		'vitalisite-fse/accordion'       => 'accordion.css',
		'vitalisite-fse/accordion-item'  => 'accordion.css',
		'vitalisite-fse/cards-container' => 'cards.css',
		'vitalisite-fse/card'            => 'cards.css',
		'vitalisite-fse/text-image'      => 'text-image.css',
		'vitalisite-fse/testimonials'    => 'testimonials.css',
		'vitalisite-fse/video'           => 'video.css',
		'vitalisite-fse/before-after'    => 'before-after.css',
		'vitalisite-fse/slider'          => 'slider.css',
		'vitalisite-fse/opening-hours'   => 'opening-hours.css',
	);

	foreach ( $block_styles as $block_name => $css_file ) {
		$handle = 'vitalisite-fse-' . basename( $css_file, '.css' );
		wp_enqueue_block_style(
			$block_name,
			array(
				'handle' => $handle,
				'src'    => get_theme_file_uri( "assets/styles/{$css_file}" ),
				'path'   => get_theme_file_path( "assets/styles/{$css_file}" ),
				'ver'    => $version,
			)
		);
	}
}
add_action( 'init', __NAMESPACE__ . '\enqueue_block_styles' );

/**
 * Enqueue responsive nav script — render-blocking in <head> to prevent flash.
 * Must run BEFORE first paint so the header is revealed only after the
 * desktop/mobile mode has been determined.
 */
function enqueue_responsive_nav() {
	$version = VITALISITE_FSE_VERSION;
	$uri     = get_template_directory_uri();

	// false = load in <head> (not in footer)
	wp_enqueue_script(
		'vitalisite-fse-responsive-nav',
		$uri . '/assets/js/responsive-nav.js',
		array(),
		$version,
		false
	);
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_responsive_nav' );

/**
 * Remove defer/async from the responsive-nav script so it is truly
 * render-blocking. WordPress 6.3+ adds defer by default.
 */
function responsive_nav_remove_defer( $tag, $handle ) {
	if ( 'vitalisite-fse-responsive-nav' === $handle ) {
		$tag = str_replace( array( ' defer', ' async' ), '', $tag );
	}
	return $tag;
}
add_filter( 'script_loader_tag', __NAMESPACE__ . '\responsive_nav_remove_defer', 10, 2 );

/**
 * Enqueue vendor scripts (Swiper) and block-specific JS.
 */
function enqueue_scripts() {
	$version = VITALISITE_FSE_VERSION;
	$uri     = get_template_directory_uri();

	// Swiper.js (bundled, version 12.1.0)
	wp_enqueue_style( 'vitalisite-swiper-css', $uri . '/assets/styles/swiper.min.css', array(), $version );
	wp_enqueue_script( 'vitalisite-swiper', $uri . '/assets/js/vendor/swiper.min.js', array(), $version, true );

	// Slider JS
	wp_enqueue_script( 'vitalisite-fse-slider-js', $uri . '/assets/js/slider.js', array( 'vitalisite-swiper' ), $version, true );

	// Testimonials JS
	wp_enqueue_script( 'vitalisite-fse-testimonials-js', $uri . '/assets/js/testimonials.js', array( 'vitalisite-swiper' ), $version, true );

	// Contact Form JS
	wp_enqueue_script( 'vitalisite-fse-contact-form-js', $uri . '/assets/js/contact-form.js', array(), $version, true );
	wp_localize_script( 'vitalisite-fse-contact-form-js', 'vitalisiteContact', array(
		'ajaxUrl' => admin_url( 'admin-ajax.php' ),
	) );

	// AJAX Blog Filter (Listing & Archives)
	if ( is_home() || is_archive() || is_search() ) {
		wp_enqueue_script( 'vitalisite-fse-blog-filter-js', $uri . '/assets/js/blog-filter.js', array(), $version, true );
	}

	// GSAP + ScrollTrigger (CDN, en-tête pour être dispo tôt)
	wp_enqueue_script(
		'gsap',
		'https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/gsap.min.js',
		array(),
		'3.12.5',
		true
	);
	wp_enqueue_script(
		'gsap-scrolltrigger',
		'https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/ScrollTrigger.min.js',
		array( 'gsap' ),
		'3.12.5',
		true
	);

	// Animations au scroll
	wp_enqueue_script(
		'vitalisite-animations',
		$uri . '/assets/js/animations.js',
		array( 'gsap', 'gsap-scrolltrigger' ),
		$version,
		true
	);
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_scripts' );

/**
 * Clear caches in dev mode.
 */
function dev_cache_busting() {
	// DÉSACTIVÉ TEMPORAIREMENT POUR FORCER LE VIDAGE DU CACHE : 
	// if ( ! is_dev_mode() ) {
	// 	return;
	// }
	if ( function_exists( 'wp_clean_themes_cache' ) ) {
		wp_clean_themes_cache( false );
	}
	if ( class_exists( 'WP_Theme_JSON_Resolver' ) ) {
		\WP_Theme_JSON_Resolver::clean_cached_data();
	}
}
add_action( 'init', __NAMESPACE__ . '\dev_cache_busting' );

// TAMPON FORCE FLUSH
add_action('init', function() {
    if ( get_option('fse_flushed_v5') !== 'yes' ) {
        $posts = get_posts([
            'post_type' => 'wp_global_styles',
            'post_status' => 'any',
            'numberposts' => -1,
        ]);
        foreach ($posts as $p) {
            wp_delete_post($p->ID, true);
        }
        
        global $wpdb;
        $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_theme_json%'");
        wp_clean_themes_cache(false);
        if ( class_exists( 'WP_Theme_JSON_Resolver' ) ) {
            \WP_Theme_JSON_Resolver::clean_cached_data();
        }
        update_option('fse_flushed_v5', 'yes');
    }
});

/**
 * Shortcode pour le lien de retour dynamique vers le blog
 */
add_shortcode( 'vitalisite_back_to_blog', function( $atts ) {
	$page_for_posts = get_option( 'page_for_posts' );
	$url = $page_for_posts ? get_permalink( $page_for_posts ) : home_url( '/blog/' );
	$text = vitalisite_get_option( OPTION_BLOG, 'back_text', 'Retour aux actualités' );

	return sprintf(
		'<p class="vitalisite-single-back"><a href="%s">← %s</a></p>',
		esc_url( $url ),
		esc_html( $text )
	);
});

/**
 * Filtre personnalisé pour la longueur de l'extrait du blog
 */
add_filter( 'excerpt_length', function( $length ) {
	$custom_length = vitalisite_get_option( OPTION_BLOG, 'excerpt_length' );
	if ( ! empty( $custom_length ) ) {
		return absint( $custom_length );
	}
	return 20; // Default length matching option placeholder
}, 999 );

/**
 * Remplacer dynamiquement le texte "Lire l'article" de core/post-excerpt
 */
add_filter( 'render_block_core/post-excerpt', function( $block_content, $block ) {
	$read_more_text = vitalisite_get_option( OPTION_BLOG, 'read_more_text' );
	if ( ! empty( $read_more_text ) ) {
		// Replace the more text inside the link generated by the block
		$block_content = preg_replace(
			'/(<a[^>]*class="wp-block-post-excerpt__more-link"[^>]*>)(.*?)(<\/a>)/is',
			'$1' . esc_html( $read_more_text ) . '$3',
			$block_content
		);
	}
	return $block_content;
}, 10, 2 );

/**
 * Ajouter le bouton "Tous" (reset) aux raccourcis de catégories de blog
 */
add_filter( 'render_block_core/categories', function( $block_content, $block ) {
	// Only alter if it's our blog categories block
	if ( ! isset( $block['attrs']['className'] ) || strpos( $block['attrs']['className'], 'vitalisite-blog-categories' ) === false ) {
		return $block_content;
	}

	$all_text = vitalisite_get_option( OPTION_BLOG, 'all_categories_text', 'Tous' );
	if ( ! empty( $all_text ) ) {
		$blog_url = get_permalink( get_option( 'page_for_posts' ) );
		if ( ! $blog_url ) {
			$blog_url = home_url( '/blog/' );
		}

		// Si on n'est PAS sur une archive de catégorie, on est sur la page de base donc "Tous" est actif
		$active_class = ( ! is_category() ) ? 'current-cat' : '';
		
		$all_li = sprintf(
			'<li class="cat-item %s"><a href="%s">%s</a></li>',
			esc_attr( $active_class ),
			esc_url( $blog_url ),
			esc_html( $all_text )
		);

		// Insert right after the opening <ul>
		$block_content = preg_replace( '/(<ul[^>]*>)/i', '$1' . $all_li, $block_content, 1 );
	}

	return $block_content;
}, 10, 2 );
