<?php
/**
 * WooCommerce integration.
 *
 * @package Shabnam_MuchWOW
 */

defined( 'ABSPATH' ) || exit;

/**
 * Declare WooCommerce compatibility.
 */
function shabnam_woocommerce_support() {
	add_theme_support( 'woocommerce' );
	add_theme_support( 'wc-product-gallery-zoom' );
	add_theme_support( 'wc-product-gallery-lightbox' );
	add_theme_support( 'wc-product-gallery-slider' );
}
add_action( 'after_setup_theme', 'shabnam_woocommerce_support', 30 );

/**
 * Configure WooCommerce hooks without bypassing WooCommerce public APIs.
 */
function shabnam_woocommerce_hooks() {
	if ( ! class_exists( 'WooCommerce' ) ) {
		return;
	}

	remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
	remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
	remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
	remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );

	add_action( 'woocommerce_before_main_content', 'shabnam_woocommerce_wrapper_start', 10 );
	add_action( 'woocommerce_before_main_content', 'shabnam_woocommerce_breadcrumb', 15 );
	add_action( 'woocommerce_after_main_content', 'shabnam_woocommerce_wrapper_end', 10 );

	if ( function_exists( 'shabnam_get_setting' ) && ! shabnam_get_setting( 'shop_show_result_count', true ) ) {
		remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
	}
	if ( function_exists( 'shabnam_get_setting' ) && ! shabnam_get_setting( 'shop_show_ordering', true ) ) {
		remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
	}
	if ( function_exists( 'shabnam_get_setting' ) && ! shabnam_get_setting( 'shop_show_rating', true ) ) {
		remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
	}
	if ( function_exists( 'shabnam_get_setting' ) && ! shabnam_get_setting( 'shop_show_sale_badge', true ) ) {
		remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
	}
}
add_action( 'wp', 'shabnam_woocommerce_hooks' );

/**
 * Open shop content wrappers.
 */
function shabnam_woocommerce_wrapper_start() {
	echo '<main id="primary" class="shabnam-shop-main shabnam-inner-page section panel overflow-hidden">';
	echo '<div class="section-outer panel py-6 sm:py-8 lg:py-9 xl:py-10">';
	echo '<div class="container sm:max-w-md xl:max-w-lg">';
	echo '<div class="section-inner panel">';
}

/**
 * Render breadcrumbs inside the shop content area so they never overlap header.
 */
function shabnam_woocommerce_breadcrumb() {
	if ( function_exists( 'shabnam_get_setting' ) && ! shabnam_get_setting( 'show_breadcrumbs', true ) ) {
		return;
	}
	if ( ! function_exists( 'woocommerce_breadcrumb' ) ) {
		return;
	}

	echo '<div class="shabnam-woocommerce-breadcrumb" aria-label="' . esc_attr__( 'مسیر صفحه', 'shabnam-muchwow' ) . '">';
	woocommerce_breadcrumb();
	echo '</div>';
}

/**
 * Close shop content wrappers.
 */
function shabnam_woocommerce_wrapper_end() {
	echo '</div></div></div></main>';
}

/**
 * Use MuchWOW classes for product card titles.
 */
function shabnam_woocommerce_loop_title() {
	echo '<h2 class="woocommerce-loop-product__title h5 xl:h4 mb-1 text-white">' . esc_html( get_the_title() ) . '</h2>';
}

/**
 * Replace only the default loop title callback.
 */
function shabnam_woocommerce_loop_title_hook() {
	if ( ! class_exists( 'WooCommerce' ) ) {
		return;
	}
	remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
	add_action( 'woocommerce_shop_loop_item_title', 'shabnam_woocommerce_loop_title', 10 );
}
add_action( 'wp', 'shabnam_woocommerce_loop_title_hook', 20 );

/**
 * Add MuchWOW button classes to loop add-to-cart buttons.
 *
 * @param array<string,mixed> $args Button args.
 * @param WC_Product          $product Product.
 * @return array<string,mixed>
 */
function shabnam_woocommerce_loop_button_args( $args, $product ) {
	$existing      = isset( $args['class'] ) ? $args['class'] : '';
	$args['class'] = trim( $existing . ' btn btn-md border border-2 border-black bg-gradient-45n from-secondary to-tertiary contrast-shadow hover:rotate-0 text-black' );
	return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'shabnam_woocommerce_loop_button_args', 10, 2 );

/**
 * Product archive columns.
 *
 * @return int
 */
function shabnam_woocommerce_loop_columns() {
	$columns = function_exists( 'shabnam_get_setting' ) ? absint( shabnam_get_setting( 'shop_columns', 3 ) ) : 3;
	return max( 2, min( 4, $columns ) );
}
add_filter( 'loop_shop_columns', 'shabnam_woocommerce_loop_columns' );

/**
 * Product count per archive page.
 *
 * @param int $per_page Existing count.
 * @return int
 */
function shabnam_woocommerce_products_per_page( $per_page ) {
	$count = function_exists( 'shabnam_get_setting' ) ? absint( shabnam_get_setting( 'shop_products_per_page', 12 ) ) : 12;
	return max( 4, min( 48, $count ) );
}
add_filter( 'loop_shop_per_page', 'shabnam_woocommerce_products_per_page', 20 );

/**
 * Related products layout.
 *
 * @param array<string,mixed> $args Related args.
 * @return array<string,mixed>
 */
function shabnam_woocommerce_related_args( $args ) {
	$count = function_exists( 'shabnam_get_setting' ) ? absint( shabnam_get_setting( 'shop_related_count', 3 ) ) : 3;
	$count = max( 2, min( 8, $count ) );
	$args['posts_per_page'] = $count;
	$args['columns']        = min( 4, $count );
	return $args;
}
add_filter( 'woocommerce_output_related_products_args', 'shabnam_woocommerce_related_args' );

/**
 * Refresh header cart count after AJAX add-to-cart.
 *
 * @param array<string,string> $fragments Fragments.
 * @return array<string,string>
 */
function shabnam_woocommerce_cart_fragments( $fragments ) {
	ob_start();
	?>
	<span class="shabnam-cart-count"><?php echo esc_html( WC()->cart ? WC()->cart->get_cart_contents_count() : 0 ); ?></span>
	<?php
	$fragments['span.shabnam-cart-count'] = ob_get_clean();

	ob_start();
	?>
	<div class="shabnam-cart-dropdown-inner"><?php woocommerce_mini_cart(); ?></div>
	<?php
	$fragments['div.shabnam-cart-dropdown-inner'] = ob_get_clean();

	return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'shabnam_woocommerce_cart_fragments' );
