<?php
/**
 * Asset Enqueue Class
 *
 * Handles enqueueing of all theme CSS and JS assets for frontend,
 * block editor, and Gutenberg blocks.
 *
 * @package    Wira
 * @since      1.0.0
 */

namespace Wira;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

if ( ! class_exists( 'Assets_Enqueue' ) ) {

	/**
	 * Class Assets_Enqueue
	 *
	 * Loads all required assets (CSS and JS) for frontend and block editor.
	 *
	 * @since 1.0.0
	 */
	class Assets_Enqueue {

		/**
		 * Constructor.
		 *
		 * Register hooks on instantiation.
		 *
		 * @since 1.0.0
		 */
		public function __construct() {
			$this->load_hooks();
		}

		/**
		 * Register hooks for asset enqueueing.
		 *
		 * @since 1.0.0
		 * @return void
		 */
		private function load_hooks() {
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
			add_action( 'init', array( $this, 'block_style_assets' ) );
		}

		/**
		 * Enqueue frontend assets.
		 *
		 * Loads Bootstrap, Font Awesome, Google Fonts,
		 * custom scripts, styles, and threaded comments.
		 *
		 * @since 1.0.0
		 * @return void
		 */
		public function enqueue_assets() {

			// Bootstrap CSS (RTL support).
			if ( is_rtl() ) {
				wp_enqueue_style(
					'bootstrap-rtl',
					get_template_directory_uri() . '/assets/css/bootstrap.rtl.min.css',
					array(),
					'5.3.0'
				);
			} else {
				wp_enqueue_style(
					'bootstrap',
					get_template_directory_uri() . '/assets/css/bootstrap.min.css',
					array(),
					'5.3.0'
				);
			}

			// Bootstrap JS.
			wp_enqueue_script(
				'bootstrap-js',
				get_template_directory_uri() . '/assets/js/bootstrap.min.js',
				array(),
				'5.3.0',
				true
			);

			$font = get_theme_mod( 'wira_body_font_family', 'Outfit' );

			wp_enqueue_style(
				'google-fonts',
				'https://fonts.googleapis.com/css2?family=' . urlencode( $font ) . ':wght@300;400;500;600;700&display=swap',
				[],
				null
			);

			// Outfit Google Fonts.
			wp_enqueue_style(
				'outfit-fonts',
				'https://fonts.googleapis.com/css2?family=Outfit:wght@100..900&display=swap',
				array(),
				null
			);

			// Font Awesome v6.
			wp_enqueue_style(
				'wira-font-awesome',
				get_template_directory_uri() . '/assets/fonts/fontawesome/css/all.min.css',
				array(),
				'6.6.0'
			);

			// Main stylesheet (style.css).
			wp_enqueue_style(
				'wira-style',
				get_stylesheet_uri(),
				array(),
				wp_get_theme()->get( 'Version' )
			);

			// Custom theme CSS.
			wp_enqueue_style(
				'wira-custom-style',
				get_template_directory_uri() . '/assets/css/style.css',
				array(),
				wp_get_theme()->get( 'Version' )
			);


			// Ensure RTL stylesheet replaces LTR.
			wp_style_add_data( 'wira-custom-style', 'rtl', 'replace' );

			// Custom cursor/animations JS.
			wp_enqueue_script(
				'wira-custom-cursor',
				get_template_directory_uri() . '/assets/js/custom-cursor.js',
				array(),
				wp_get_theme()->get( 'Version' ),
				true
			);

			// Main theme JS.
			wp_enqueue_script(
				'wira-custom-js',
				get_template_directory_uri() . '/assets/js/main.js',
				array( 'jquery' ),
				wp_get_theme()->get( 'Version' ),
				true
			);


			// Threaded comments (only if needed).
			if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
				wp_enqueue_script( 'comment-reply' );
			}
		}

		/**
		 * Enqueue block style for frontend.
		 *
		 * Registers and enqueues block editor style on init.
		 *
		 * @since 1.0.0
		 * @return void
		 */
		public function block_style_assets() {
			$theme_version  = wp_get_theme()->get( 'Version' );
			$version_string = is_string( $theme_version ) ? $theme_version : false;

			wp_register_style(
				'wira-block-style',
				get_template_directory_uri() . '/assets/css/block-style.css',
				array(),
				$version_string
			);

			wp_enqueue_style( 'wira-block-style' );
		}

		/**
		 * Enqueue block style for editor.
		 *
		 * Ensures block editor has consistent styling with frontend.
		 *
		 * @since 1.0.0
		 * @return void
		 */
		public function editor_styles() {
			wp_enqueue_style(
				'wira-block-style',
				get_template_directory_uri() . '/assets/css/block-style.css',
				array(),
				wp_get_theme()->get( 'Version' )
			);
		}
	}
}
