<?php
/**
 * Theme Asset Enqueuing
 *
 * Handles enqueuing of all CSS and JavaScript assets.
 *
 * @package pawwell
 */

defined( 'ABSPATH' ) || exit;

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

	// Product Single CSS
	if ( pawwell_is_active_woocommerce()) {
		wp_enqueue_style( 'pawwell-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() ) {
		wp_enqueue_style( 'pawwell-blog-style', get_template_directory_uri() . '/assets/css/blog.css', array(), wp_get_theme()->get('Version') );
	}

	// Responsive CSS
	wp_enqueue_style( 'pawwell-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
	wp_enqueue_script( 'pawwell-script', get_template_directory_uri() . '/assets/js/main.js', array('jquery'), wp_get_theme()->get('Version'), true );

	// Shop JavaScript
	if ( pawwell_is_active_woocommerce() ) {
		wp_enqueue_script( 'pawwell-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( 'pawwell-shop-script', "var pawwell_shop_base_url = '{$shop_base_url}';", 'before' );
		}

		// Product Single JavaScript
		if ( is_product() ) {
			wp_enqueue_script( 'pawwell-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( 'pawwell-product-single-script', 'pawwell_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!', 'pawwell' ),
					'review_pending' => esc_html__( 'Your review is pending approval.', 'pawwell' ),
					'select_options' => esc_html__( 'Please select all options before adding to cart.', 'pawwell' ),
				),
			) );
		}
		
		// 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', 'pawwell' ),
			'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( 'pawwell_cart_data_nonce' ),
		);
		
		// Localize script with WooCommerce AJAX parameters (available to both scripts)
		wp_localize_script( 'pawwell-script', 'pawwell_wc_params', $wc_ajax_params );
		wp_localize_script( 'pawwell-shop-script', 'pawwell_wc_params', $wc_ajax_params );
		
		// Localize shop script for AJAX
		wp_localize_script( 'pawwell-shop-script', 'pawwell_ajax', array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce' => wp_create_nonce( 'pawwell_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( 'pawwell-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( 'pawwell-blog-script', 'pawwell_ajax', array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'pawwell_ajax_nonce' ),
		));
	}

	// Cart Page Assets
	if ( pawwell_is_active_woocommerce() && is_cart() ) {
		$_cart_css_ver = (string) filemtime( get_template_directory() . '/assets/css/cart.css' );
		$_cart_js_ver  = (string) filemtime( get_template_directory() . '/assets/js/cart.js' );
		wp_enqueue_style( 'pawwell-cart-style', get_template_directory_uri() . '/assets/css/cart.css', array(), $_cart_css_ver );
		wp_enqueue_script( 'pawwell-cart-script', get_template_directory_uri() . '/assets/js/cart.js', array('jquery'), $_cart_js_ver, true );
		wp_localize_script( 'pawwell-cart-script', 'pawwell_cart_ajax', array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'pawwell_cart_nonce' ),
		) );
		wp_localize_script( 'pawwell-cart-script', 'pawwell_cart_texts', array(
			'remove_confirm' => esc_html__( 'Remove %s from cart?', 'pawwell' ),
		) );

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

	// Checkout Page Assets
	if ( pawwell_is_active_woocommerce() && is_checkout() ) {
		$_checkout_css_ver = (string) filemtime( get_template_directory() . '/assets/css/checkout.css' );
		$_checkout_js_ver  = (string) filemtime( get_template_directory() . '/assets/js/checkout.js' );
		wp_enqueue_style( 'pawwell-checkout-style', get_template_directory_uri() . '/assets/css/checkout.css', array(), $_checkout_css_ver );
		wp_enqueue_script( 'pawwell-checkout-script', get_template_directory_uri() . '/assets/js/checkout.js', array('jquery'), $_checkout_js_ver, true );
	}

	// My Account Page Assets
	if ( pawwell_is_active_woocommerce() && is_account_page() ) {
		$_account_css_ver = (string) filemtime( get_template_directory() . '/assets/css/account.css' );
		wp_enqueue_style( 'pawwell-account-style', get_template_directory_uri() . '/assets/css/account.css', array(), $_account_css_ver );
	}
}
add_action( 'wp_enqueue_scripts', 'pawwell_scripts' );

/**
 * Inject inline CSS for the custom logo width.
 *
 * @return void
 */
function pawwell_logo_width_inline_css() {
	$logo_width = absint( get_theme_mod( 'pawwell_logo_width', 180 ) );

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

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

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

	wp_register_style( 'pawwell-cart-ajax', false );
	wp_enqueue_style( 'pawwell-cart-ajax' );
	wp_add_inline_style( 'pawwell-cart-ajax', $css );
}
add_action( 'wp_enqueue_scripts', 'pawwell_cart_ajax_inline_css', 20 );
