<?php
/**
 * Shop Zen Theme Asset Enqueuing
 *
 * Handles enqueuing of all CSS and JavaScript assets.
 *
 * @package shop-zen
 */

defined( 'ABSPATH' ) || exit;

/**
 * Enqueue Styles and Scripts
 *
 * Enqueues all front-end styles and scripts.
 *
 * @return void
 */
function shop_zen_scripts() {
	// Google Fonts
	wp_enqueue_style( 'shop-zen-google-fonts', get_template_directory_uri() . '/assets/fonts/google-fonts.css', array(), wp_get_theme()->get( 'Version' ) );
	
	// Style CSS
	$shop_zen_style_ver = filemtime( get_template_directory() . '/style.css' );
	wp_enqueue_style( 'shop-zen-style', get_stylesheet_uri(), array(), $shop_zen_style_ver );
	
	// Main theme CSS
	wp_enqueue_style( 'shop-zen-main-style', get_template_directory_uri() . '/assets/css/main.css', array(), wp_get_theme()->get('Version') );
	
	// Shop CSS
	if ( shop_zen_is_active_woocommerce() ) {
		wp_enqueue_style( 'shop-zen-shop-style', get_template_directory_uri() . '/assets/css/shop.css', array(), wp_get_theme()->get('Version') );
	}

	// Product Single CSS
	if ( shop_zen_is_active_woocommerce()) {
		wp_enqueue_style( 'shop-zen-product-single-style', get_template_directory_uri() . '/assets/css/product-single.css', array(), wp_get_theme()->get('Version') );
	}

	// Blog CSS
	if ( is_home() || is_archive() || is_category() || is_tag() || is_author() || is_date() || is_search() || is_singular( 'post' ) ) {
		wp_enqueue_style( 'shop-zen-blog-style', get_template_directory_uri() . '/assets/css/blog.css', array(), wp_get_theme()->get('Version') );
	}

	// Responsive CSS
	wp_enqueue_style( 'shop-zen-responsive-style', get_template_directory_uri() . '/assets/css/responsive.css', array(), wp_get_theme()->get('Version') );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) :
		wp_enqueue_script( 'comment-reply' );
	endif;

	// Main theme JavaScript
	$shop_zen_script_ver = filemtime( get_template_directory() . '/assets/js/main.js' );
	wp_enqueue_script( 'shop-zen-script', get_template_directory_uri() . '/assets/js/main.js', array('jquery'), $shop_zen_script_ver, true );

	// Shop JavaScript
	if ( shop_zen_is_active_woocommerce() ) {
		wp_enqueue_script( 'shop-zen-shop-script', get_template_directory_uri() . '/assets/js/shop.js', array('jquery'), wp_get_theme()->get('Version'), true );

		// Inline script: shop base URL for archive product pages.
		if ( function_exists( 'wc_get_page_permalink' ) ) {
			$shop_base_url = esc_js( wp_parse_url( wc_get_page_permalink( 'shop' ), PHP_URL_PATH ) );
			wp_add_inline_script( 'shop-zen-shop-script', "var shop_zen_shop_base_url = '{$shop_base_url}';", 'before' );
		}

		// Product Single JavaScript
		if ( is_product() ) {
			wp_enqueue_script( 'shop-zen-product-single-script', get_template_directory_uri() . '/assets/js/product-single.js', array('jquery'), wp_get_theme()->get('Version'), true );
			
			// Localize product single script with checkout URL and currency format data
			wp_localize_script( 'shop-zen-product-single-script', 'shop_zen_product_single', array(
				'checkout_url' => function_exists( 'wc_get_checkout_url' ) ? wc_get_checkout_url() : '',
				'ajax_url' => admin_url( 'admin-ajax.php' ),
				'currency_symbol' => function_exists( 'get_woocommerce_currency_symbol' ) ? get_woocommerce_currency_symbol() : '$',
				'currency_pos' => get_option( 'woocommerce_currency_pos', 'left' ),
				'decimal_sep' => function_exists( 'wc_get_price_decimal_separator' ) ? wc_get_price_decimal_separator() : '.',
				'thousand_sep' => function_exists( 'wc_get_price_thousand_separator' ) ? wc_get_price_thousand_separator() : ',',
				'decimals' => function_exists( 'wc_get_price_decimals' ) ? wc_get_price_decimals() : 2,
				'texts' => array(
					'review_submitted' => esc_html__( 'Thank you for your review!', 'shop-zen' ),
					'review_pending' => esc_html__( 'Your review is pending approval.', 'shop-zen' ),
					'select_options' => esc_html__( 'Please select all options before adding to cart.', 'shop-zen' ),
				),
			) );
		}
		
		// Ensure WooCommerce variation scripts are loaded on product pages
		if ( is_product() ) {
			wp_enqueue_script( 'wc-add-to-cart-variation' );
		}
		
		// Build WooCommerce AJAX parameters
		$wc_ajax_params = array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'wc_ajax_url' => class_exists( 'WC_AJAX' ) ? WC_AJAX::get_endpoint( '%%endpoint%%' ) : '/?wc-ajax=%%endpoint%%',
			'i18n_view_cart' => esc_attr__( 'View cart', 'shop-zen' ),
			'cart_url' => function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : '',
			'is_cart' => function_exists( 'is_cart' ) ? is_cart() : false,
			'cart_redirect_after_add' => get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ? 1 : 0,
			'cart_data_nonce' => wp_create_nonce( 'shop_zen_cart_data_nonce' ),
		);
		
		// Localize script with WooCommerce AJAX parameters
		wp_localize_script( 'shop-zen-script', 'shop_zen_wc_params', $wc_ajax_params );
		wp_localize_script( 'shop-zen-shop-script', 'shop_zen_wc_params', $wc_ajax_params );
		
		// Localize shop script for AJAX
		wp_localize_script( 'shop-zen-shop-script', 'shop_zen_ajax', array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce' => wp_create_nonce( 'shop_zen_ajax_nonce' ),
		));
	}

	// Blog JavaScript
	if ( is_home() || is_archive() || is_category() || is_tag() || is_author() || is_date() || is_search() || is_singular( 'post' ) ) {
		wp_enqueue_script( 'shop-zen-blog-script', get_template_directory_uri() . '/assets/js/blog.js', array('jquery'), wp_get_theme()->get('Version'), true );

		// Localize blog script for AJAX
		wp_localize_script( 'shop-zen-blog-script', 'shop_zen_ajax', array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'shop_zen_ajax_nonce' ),
		));
	}

	// Cart Page Assets
	if ( shop_zen_is_active_woocommerce() && is_cart() ) {
		wp_enqueue_style( 'shop-zen-cart-style', get_template_directory_uri() . '/assets/css/cart.css', array(), wp_get_theme()->get('Version') );
		wp_enqueue_script( 'shop-zen-cart-script', get_template_directory_uri() . '/assets/js/cart.js', array('jquery'), wp_get_theme()->get('Version'), true );
		wp_localize_script( 'shop-zen-cart-script', 'shop_zen_cart_ajax', array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'shop_zen_cart_nonce' ),
		) );
		wp_localize_script( 'shop-zen-cart-script', 'shop_zen_cart_texts', array(
			'remove_confirm' => esc_html__( 'Remove %s from cart?', 'shop-zen' ),
		) );

		// Cart Page AJAX Quantity Update — updates pricing without page reload.
		wp_enqueue_script(
			'shop-zen-cart-qty-ajax-js',
			get_template_directory_uri() . '/assets/js/cart-qty-ajax.js',
			array( 'jquery', 'shop-zen-cart-script' ),
			wp_get_theme()->get( 'Version' ),
			true
		);
		wp_localize_script( 'shop-zen-cart-qty-ajax-js', 'shopzenCartQty', array(
			'ajaxUrl'   => admin_url( 'admin-ajax.php' ),
			'nonce'     => wp_create_nonce( 'shop_zen_cart_qty_nonce' ),
			'i18nItem'  => __( '%s Item', 'shop-zen' ),
			'i18nItems' => __( '%s Items', 'shop-zen' ),
		) );
	}

	// Checkout Page Assets
	if ( shop_zen_is_active_woocommerce() && is_checkout() ) {
		$checkout_css_ver = filemtime( get_template_directory() . '/assets/css/checkout.css' );
		wp_enqueue_style( 'shop-zen-checkout-style', get_template_directory_uri() . '/assets/css/checkout.css', array(), $checkout_css_ver );
		wp_enqueue_script( 'shop-zen-checkout-script', get_template_directory_uri() . '/assets/js/checkout.js', array('jquery'), wp_get_theme()->get('Version'), true );
	}

	// My Account Page Assets
	if ( shop_zen_is_active_woocommerce() && is_account_page() ) {
		wp_enqueue_style( 'shop-zen-account-style', get_template_directory_uri() . '/assets/css/account.css', array(), wp_get_theme()->get('Version') );
	}
}
add_action( 'wp_enqueue_scripts', 'shop_zen_scripts' );

/**
 * Inject cart AJAX update CSS.
 * Adds a subtle loading indicator when cart quantities are being updated via AJAX.
 */
function shop_zen_cart_ajax_inline_css() {
	if ( ! class_exists( 'WooCommerce' ) || ! function_exists( 'is_cart' ) || ! is_cart() ) {
		return;
	}

	$css = '
		.shop-zen-cart-updating .ci-total {
			opacity: 0.4;
			transition: opacity 0.2s ease;
		}
		.shop-zen-cart-updating .ci-qty {
			opacity: 0.6;
			pointer-events: none;
		}
	';

	wp_register_style( 'shop-zen-cart-ajax', false );
	wp_enqueue_style( 'shop-zen-cart-ajax' );
	wp_add_inline_style( 'shop-zen-cart-ajax', $css );
}
add_action( 'wp_enqueue_scripts', 'shop_zen_cart_ajax_inline_css', 20 );

/**
 * Inject inline CSS for the custom logo max-width.
 * Driven by the Customizer "Logo Width (px)" setting.
 */
function shop_zen_logo_width_inline_css() {
	$logo_width = absint( get_theme_mod( 'shop_zen_logo_width', 180 ) );

	if ( $logo_width > 0 ) {
		$css = '.site-branding .custom-logo{max-width:' . $logo_width . 'px !important;}';
		wp_add_inline_style( 'shop-zen-style', $css );
	}
}
add_action( 'wp_enqueue_scripts', 'shop_zen_logo_width_inline_css', 20 );
