<?php
/**
 * Public-facing functionality.
 *
 * @package MasterQuiz
 * @since   1.0.0
 */

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

/**
 * Class MasterQuiz_Public
 *
 * Registers and manages public-facing assets.
 *
 * @since 1.0.0
 */
class MasterQuiz_Public {

	/**
	 * Register public assets (scripts and styles).
	 *
	 * Assets are registered but NOT enqueued here — they are enqueued
	 * only when the shortcode is rendered (see MasterQuiz_Shortcode).
	 *
	 * @since 1.0.0
	 */
	public function register_assets() {
		wp_register_style(
			'masterquiz-public',
			MASTERQUIZ_PLUGIN_URL . 'public/css/masterquiz-public.css',
			array(),
			$this->asset_version( 'public/css/masterquiz-public.css' )
		);

		wp_register_script(
			'masterquiz-public',
			MASTERQUIZ_PLUGIN_URL . 'public/js/masterquiz-public.js',
			array( 'jquery' ),
			$this->asset_version( 'public/js/masterquiz-public.js' ),
			true
		);
	}

	/**
	 * Build an asset version string that changes whenever the file changes.
	 *
	 * Without this, browsers and caching layers keep serving a stale copy after
	 * an update because the plugin version alone stays the same between releases.
	 *
	 * @since 1.0.4
	 *
	 * @param string $relative_path Path relative to the plugin directory.
	 * @return string Version string for wp_register_* calls.
	 */
	private function asset_version( $relative_path ) {
		$full_path = MASTERQUIZ_PLUGIN_DIR . $relative_path;

		if ( file_exists( $full_path ) ) {
			$mtime = filemtime( $full_path );
			if ( $mtime ) {
				return MASTERQUIZ_VERSION . '.' . $mtime;
			}
		}

		return MASTERQUIZ_VERSION;
	}
}
