<?php
/**
 * MuchWOW Shabnam Commerce functions and definitions.
 *
 * @package Shabnam_MuchWOW
 * @author SHABNAM.DEV
 * @link https://shcd.ir/
 */

defined( 'ABSPATH' ) || exit;

if ( ! defined( 'SHABNAM_MUCHWOW_VERSION' ) ) {
	define( 'SHABNAM_MUCHWOW_VERSION', '1.0.0' );
}

if ( ! defined( 'SHABNAM_MUCHWOW_EDITION' ) ) {
	define( 'SHABNAM_MUCHWOW_EDITION', 'trial' );
}

/**
 * Return a cache-busting version for a theme asset without changing the public theme version.
 *
 * @param string $relative_path Path relative to the theme directory.
 * @return string
 */
function shabnam_asset_version( $relative_path ) {
	$path = get_template_directory() . '/' . ltrim( (string) $relative_path, '/' );
	return is_file( $path ) ? (string) filemtime( $path ) : SHABNAM_MUCHWOW_VERSION;
}

require_once get_template_directory() . '/inc/template-tags.php';
require_once get_template_directory() . '/inc/site-pages.php';
require_once get_template_directory() . '/inc/shabnam-framework.php';
require_once get_template_directory() . '/inc/customizer.php';
require_once get_template_directory() . '/inc/woocommerce.php';
require_once get_template_directory() . '/inc/account-spa.php';
require_once get_template_directory() . '/inc/site-spa.php';
require_once get_template_directory() . '/inc/elementor.php';

// The multi-demo installer creates and manages the front pages; disable the legacy single-home bootstrap.
remove_action( 'init', 'shabnam_elementor_bootstrap_home_page', 30 );

/**
 * Configure theme features.
 */
function shabnam_theme_setup() {
	load_theme_textdomain( 'shabnam-muchwow', get_template_directory() . '/languages' );

	add_theme_support( 'title-tag' );
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'responsive-embeds' );
	add_theme_support( 'align-wide' );
	add_theme_support( 'wp-block-styles' );
	add_theme_support( 'editor-styles' );
	add_theme_support(
		'html5',
		array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script' )
	);
	add_theme_support(
		'custom-logo',
		array(
			'height'      => 120,
			'width'       => 360,
			'flex-height' => true,
			'flex-width'  => true,
		)
	);

	register_nav_menus(
		array(
			'primary' => __( 'منوی اصلی', 'shabnam-muchwow' ),
			'footer'  => __( 'منوی فوتر', 'shabnam-muchwow' ),
		)
	);
}
add_action( 'after_setup_theme', 'shabnam_theme_setup' );

/**
 * Register widget areas.
 */
function shabnam_widgets_init() {
	register_sidebar(
		array(
			'name'          => __( 'سایدبار وبلاگ', 'shabnam-muchwow' ),
			'id'            => 'blog-sidebar',
			'description'   => __( 'ابزارک‌های صفحات وبلاگ.', 'shabnam-muchwow' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s panel p-3 sm:p-4 border border-1 border-gray-100 border-opacity-40 rounded-3 bg-white bg-opacity-5">',
			'after_widget'  => '</section>',
			'before_title'  => '<h3 class="h5 text-secondary mb-2">',
			'after_title'   => '</h3>',
		)
	);
}
add_action( 'widgets_init', 'shabnam_widgets_init' );

/**
 * Enqueue front-end assets from the original MuchWOW package.
 */
function shabnam_enqueue_assets() {
	$uri = get_template_directory_uri();
	$ver = SHABNAM_MUCHWOW_VERSION;

	wp_enqueue_style( 'shabnam-uni-core', $uri . '/assets/js/uni-core/css/uni-core.min.css', array(), $ver );
	wp_enqueue_style( 'shabnam-unicons', $uri . '/assets/css/unicons.min.css', array(), $ver );
	wp_enqueue_style( 'shabnam-swiper', $uri . '/assets/css/swiper-bundle.min.css', array(), $ver );
	wp_enqueue_style( 'shabnam-prettify', $uri . '/assets/css/prettify.min.css', array(), $ver );
	wp_enqueue_style( 'shabnam-magic-cursor', $uri . '/assets/css/magic-cursor.min.css', array(), $ver );
	$variant = function_exists( 'shabnam_muchwow_style_variant' ) ? shabnam_muchwow_style_variant() : 'one';
	$core_css = 'two' === $variant ? 'theme-two.min.purge.css' : 'main.min.purge.css';
	wp_enqueue_style( 'shabnam-muchwow-core', $uri . '/assets/css/theme/' . $core_css, array(), shabnam_asset_version( 'assets/css/theme/' . $core_css ) );
	wp_enqueue_style( 'shabnam-muchwow-rtl', $uri . '/assets/css/rtl-fa.css', array( 'shabnam-muchwow-core' ), $ver );
	wp_enqueue_style( 'shabnam-wordpress', $uri . '/assets/css/shabnam-wp.css', array( 'shabnam-muchwow-rtl' ), shabnam_asset_version( 'assets/css/shabnam-wp.css' ) );
	wp_enqueue_style( 'shabnam-elementor', $uri . '/assets/css/shabnam-elementor.css', array( 'shabnam-wordpress' ), shabnam_asset_version( 'assets/css/shabnam-elementor.css' ) );
	wp_enqueue_style( 'shabnam-style', get_stylesheet_uri(), array( 'shabnam-elementor' ), $ver );

	wp_enqueue_script( 'shabnam-app-head', $uri . '/assets/js/app-head-bs.js', array(), $ver, false );
	wp_enqueue_script( 'shabnam-uni-core-js', $uri . '/assets/js/uni-core/js/uni-core-bundle.min.js', array( 'shabnam-app-head' ), $ver, false );

	wp_enqueue_script( 'jquery' );
	wp_enqueue_script( 'shabnam-bootstrap', $uri . '/assets/js/libs/bootstrap.min.js', array( 'jquery' ), $ver, true );
	wp_enqueue_script( 'shabnam-anime', $uri . '/assets/js/libs/anime.min.js', array(), $ver, true );
	wp_enqueue_script( 'shabnam-swiper-js', $uri . '/assets/js/libs/swiper-bundle.min.js', array(), $ver, true );
	wp_enqueue_script( 'shabnam-scrollmagic', $uri . '/assets/js/libs/scrollmagic.min.js', array(), $ver, true );
	wp_enqueue_script( 'shabnam-typed', $uri . '/assets/js/libs/typed.min.js', array(), $ver, true );
	wp_enqueue_script( 'shabnam-tilt', $uri . '/assets/js/libs/tilt.min.js', array(), $ver, true );
	wp_enqueue_script( 'shabnam-split-type', $uri . '/assets/js/libs/split-type.min.js', array(), $ver, true );
	wp_enqueue_script( 'shabnam-prettify-js', $uri . '/assets/js/libs/prettify.min.js', array(), $ver, true );
	wp_enqueue_script( 'shabnam-gsap', $uri . '/assets/js/libs/gsap.min.js', array(), $ver, true );
	wp_enqueue_script( 'shabnam-magic-cursor-js', $uri . '/assets/js/core/magic-cursor.js', array(), $ver, true );
	wp_enqueue_script( 'shabnam-data-attr', $uri . '/assets/js/helpers/data-attr-helper.js', array(), $ver, true );
	wp_enqueue_script( 'shabnam-swiper-helper', $uri . '/assets/js/helpers/swiper-helper.js', array( 'shabnam-swiper-js' ), $ver, true );
	wp_enqueue_script( 'shabnam-splitype-helper', $uri . '/assets/js/helpers/splitype-helper.js', array( 'shabnam-split-type' ), $ver, true );
	wp_enqueue_script( 'shabnam-anime-helper', $uri . '/assets/js/helpers/anime-helper.js', array( 'shabnam-anime' ), $ver, true );
	wp_enqueue_script( 'shabnam-typed-helper', $uri . '/assets/js/helpers/typed-helper.js', array( 'shabnam-typed' ), $ver, true );
	wp_enqueue_script( 'shabnam-tilt-helper', $uri . '/assets/js/helpers/tilt-helper.js', array( 'shabnam-tilt' ), $ver, true );
	wp_enqueue_script( 'shabnam-marquee', $uri . '/assets/js/core/marquee.js', array(), $ver, true );
	wp_enqueue_script( 'shabnam-uikit-components', $uri . '/assets/js/uikit-components-bs.js', array(), $ver, true );
	wp_enqueue_script( 'shabnam-app', $uri . '/assets/js/app.js', array(), $ver, true );
	wp_enqueue_script( 'shabnam-wordpress-js', $uri . '/assets/js/shabnam-wp.js', array( 'jquery', 'shabnam-app' ), shabnam_asset_version( 'assets/js/shabnam-wp.js' ), true );
	if ( class_exists( 'WooCommerce' ) && ( ! function_exists( 'shabnam_get_setting' ) || shabnam_get_setting( 'show_cart', true ) ) ) {
		wp_enqueue_script( 'wc-cart-fragments' );
	}
	wp_localize_script(
		'shabnam-wordpress-js',
		'SHCDMuchWowTheme',
		array(
			'cartAnimation' => function_exists( 'shabnam_get_setting' ) ? (bool) shabnam_get_setting( 'cart_animation', true ) : true,
			'accountSpa'    => class_exists( 'WooCommerce' ) && is_user_logged_in() && function_exists( 'is_account_page' ) && is_account_page(),
			'accountAjax'   => admin_url( 'admin-ajax.php' ),
			'accountNonce'  => wp_create_nonce( 'shabnam_muchwow_account_spa' ),
			'accountUrls'   => function_exists( 'shabnam_muchwow_account_spa_endpoint_urls' ) ? shabnam_muchwow_account_spa_endpoint_urls() : array(),
		)
	);

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'shabnam_enqueue_assets' );

/**
 * Add MuchWOW body utility classes without overwriting WordPress classes.
 */
function shabnam_body_classes( $classes ) {
	$classes[] = 'uni-body';
	$classes[] = 'panel';
	$variant = function_exists( 'shabnam_muchwow_style_variant' ) ? shabnam_muchwow_style_variant() : 'one';
	if ( 'two' === $variant ) {
		$classes[] = 'bg-white';
		$classes[] = 'text-black';
	} elseif ( 'three' === $variant ) {
		$classes[] = 'bg-secondary';
		$classes[] = 'text-white';
	} else {
		$classes[] = 'bg-gray-700';
		$classes[] = 'text-white';
	}
	$classes[] = 'overflow-x-hidden';
	$classes[] = 'disable-cursor';
	$classes[] = 'shabnam-theme';
	if ( function_exists( 'shabnam_muchwow_selected_demo' ) ) {
		$classes[] = 'shabnam-active-demo-' . sanitize_html_class( shabnam_muchwow_selected_demo() );
	}
	return $classes;
}
add_filter( 'body_class', 'shabnam_body_classes' );

/**
 * Add custom image sizes for archive cards.
 */
function shabnam_image_sizes() {
	add_image_size( 'shabnam-card', 800, 520, true );
	add_image_size( 'shabnam-hero', 1440, 900, true );
}
add_action( 'after_setup_theme', 'shabnam_image_sizes', 20 );

/**
 * Apply blog pagination preferences to the main front-end query only.
 *
 * @param WP_Query $query Query object.
 */
function shabnam_blog_query_preferences( $query ) {
	if ( is_admin() || ! $query->is_main_query() ) {
		return;
	}

	if ( $query->is_home() || $query->is_archive() || $query->is_search() ) {
		$per_page = function_exists( 'shabnam_get_setting' ) ? absint( shabnam_get_setting( 'blog_posts_per_page', 9 ) ) : 9;
		$query->set( 'posts_per_page', max( 3, min( 30, $per_page ) ) );
	}
}
add_action( 'pre_get_posts', 'shabnam_blog_query_preferences' );

