<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Avision_Components {
	private static bool $footer_rendered = false;
	private static bool $header_rendered = false;

	public static function init(): void {
		// Hook réservé aux évolutions futures.
		add_action( 'shutdown', array( __CLASS__, 'check_layout_integrity' ), 20 );
	}

	public static function get_layout( string $name, array $context = array() ): string {
		if ( $name === 'header' ) {
			self::$header_rendered = true;
		} elseif ( $name === 'footer' ) {
			self::$footer_rendered = true;
		}
		$safe_name = sanitize_file_name( $name );
		$file = defined( 'AVISION_PATH' ) ? trailingslashit( AVISION_PATH ) . 'templates/layout/' . $safe_name . '.php' : '';
		if ( $file === '' || ! file_exists( $file ) ) {
			$file = trailingslashit( plugin_dir_path( dirname( __FILE__ ) ) ) . 'templates/layout/' . $safe_name . '.php';
		}
		error_log( '[AVI] get_layout: ' . $file . ' exists=' . ( file_exists( $file ) ? 'yes' : 'NO' ) );
		if ( ! file_exists( $file ) ) {
			error_log( '[AVI] Layout not found: ' . $safe_name );
			return '';
		}
		ob_start();
		$avi_context = $context;
		if ( is_array( $avi_context ) ) {
			foreach ( $avi_context as $avi_key => $avi_value ) {
				if ( is_string( $avi_key ) && preg_match( '/^[A-Za-z_][A-Za-z0-9_]*$/', $avi_key ) ) {
					${$avi_key} = $avi_value;
				}
			}
		}
		include $file;
		return (string) ob_get_clean();
	}

	public static function check_layout_integrity(): void {
		if ( self::$header_rendered && ! self::$footer_rendered ) {
			$uri = isset( $_SERVER['REQUEST_URI'] ) ? (string) $_SERVER['REQUEST_URI'] : 'unknown';
			error_log( '[AVI WARNING] Footer missing on: ' . $uri );
		}
	}

	public static function get_component( string $name, array $context = array() ): string {
		$safe_name = sanitize_file_name( $name );
		$file = defined( 'AVISION_PATH' ) ? trailingslashit( AVISION_PATH ) . 'templates/components/' . $safe_name . '.php' : '';
		if ( $file === '' || ! file_exists( $file ) ) {
			$file = trailingslashit( plugin_dir_path( dirname( __FILE__ ) ) ) . 'templates/components/' . $safe_name . '.php';
		}
		if ( ! file_exists( $file ) ) {
			return '';
		}
		ob_start();
		$avi_context = $context;
		if ( is_array( $avi_context ) ) {
			foreach ( $avi_context as $avi_key => $avi_value ) {
				if ( is_string( $avi_key ) && preg_match( '/^[A-Za-z_][A-Za-z0-9_]*$/', $avi_key ) ) {
					${$avi_key} = $avi_value;
				}
			}
		}
		include $file;
		return (string) ob_get_clean();
	}
}

if ( ! function_exists( 'avi_get_layout' ) ) {
	function avi_get_layout( string $name, array $context = array() ): string {
		echo Avision_Components::get_layout( $name, $context );
		return '';
	}
}

if ( ! function_exists( 'avi_get_component' ) ) {
	function avi_get_component( string $name, array $context = array() ): string {
		return Avision_Components::get_component( $name, $context );
	}
}

if ( ! function_exists( 'avi_component' ) ) {
	function avi_component( string $name, array $context = array() ): string {
		$html = Avision_Components::get_component( $name, $context );
		echo $html;
		return $html;
	}
}
