<?php
/**
 * WooCommerce Compatibility File
 *
 * @package Paws_And_Care
 */

/**
 * WooCommerce Specific Filters & Hooks
 */

// Remove default WooCommerce wrappers
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );

// Disable WooCommerce breadcrumbs in favor of theme's
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );

/**
 * Header Cart Fragment — AJAX refresh for the mini-cart dropdown
 */
function paws_and_care_woocommerce_header_cart_fragment( $fragments ) {
    ob_start();
    paws_and_care_woocommerce_header_cart();
    $fragments['.header-cart-content'] = ob_get_clean();
    return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'paws_and_care_woocommerce_header_cart_fragment' );

/**
 * Helper to display the header cart
 */
function paws_and_care_woocommerce_header_cart() {
    if ( ! class_exists( 'WooCommerce' ) ) {
        return;
    }
    ?>
    <div class="header-cart-content dropdown">
        <a class="cart-toggle" href="<?php echo esc_url( wc_get_cart_url() ); ?>" id="miniCartDropdown" data-bs-toggle="dropdown" aria-expanded="false">
            <i class="fa-solid fa-bag-shopping"></i>
            <?php if ( WC()->cart->get_cart_contents_count() > 0 ) : ?>
                <span class="cart-count"><?php echo esc_html( WC()->cart->get_cart_contents_count() ); ?></span>
            <?php endif; ?>
        </a>
        <div class="dropdown-menu dropdown-menu-end mini-cart-dropdown shadow-sm border-0 p-3" aria-labelledby="miniCartDropdown" style="min-width: 320px;">
            <?php woocommerce_mini_cart(); ?>
        </div>
    </div>
    <?php
}

/**
 * Change number of products per row.
 */
add_filter( 'loop_shop_columns', 'paws_and_care_loop_columns', 999 );
if ( ! function_exists( 'paws_and_care_loop_columns' ) ) {
	function paws_and_care_loop_columns() {
		return get_theme_mod( 'paws_and_care_woocommerce_columns', 4 ); 
	}
}

/**
 * Add WooCommerce specific body classes
 */
add_filter( 'body_class', 'paws_and_care_woocommerce_body_classes' );
function paws_and_care_woocommerce_body_classes( $classes ) {
    if ( is_woocommerce() ) {
        $classes[] = 'paws-and-care-woocommerce';
    }
    if ( is_product() ) {
        $classes[] = 'paws-single-product';
    }
    if ( function_exists( 'is_cart' ) && is_cart() ) {
        $classes[] = 'paws-cart-page';
    }
    if ( function_exists( 'is_checkout' ) && is_checkout() ) {
        $classes[] = 'paws-checkout-page';
    }
    return $classes;
}

/**
 * Ensure Bootstrap consistency in WooCommerce
 */
add_filter( 'woocommerce_post_class', 'paws_and_care_product_post_class', 10, 2 );
function paws_and_care_product_post_class( $classes, $product ) {
    if ( is_shop() || is_product_taxonomy() || is_product_category() || is_product_tag() ) {
        $classes[] = 'product-card-item';
    }
    return $classes;
}

/* ================================================
   SINGLE PRODUCT PAGE — Hooks & Structure
   ================================================ */

/**
 * Wrap the gallery + summary in a card container
 */
add_action( 'woocommerce_before_single_product_summary', 'paws_and_care_single_product_open_wrapper', 1 );
function paws_and_care_single_product_open_wrapper() {
    echo '<div class="paws-single-product-top">';
    echo '<div class="row g-4 g-lg-5">';
    echo '<div class="col-lg-6">';
    echo '<div class="paws-gallery-wrapper">';
}

/**
 * Close gallery wrapper, open summary column
 */
add_action( 'woocommerce_before_single_product_summary', 'paws_and_care_single_product_gallery_close', 30 );
function paws_and_care_single_product_gallery_close() {
    echo '</div>'; // .paws-gallery-wrapper
    echo '</div>'; // .col-lg-6
    echo '<div class="col-lg-6">';
    echo '<div class="paws-summary-wrapper">';
}

/**
 * Close summary wrapper and the top row
 */
add_action( 'woocommerce_after_single_product_summary', 'paws_and_care_single_product_close_wrapper', 1 );
function paws_and_care_single_product_close_wrapper() {
    echo '</div>'; // .paws-summary-wrapper
    echo '</div>'; // .col-lg-6
    echo '</div>'; // .row
    echo '</div>'; // .paws-single-product-top
}

/**
 * Add category/SKU badge above the title
 */
add_action( 'woocommerce_single_product_summary', 'paws_and_care_single_product_category_badge', 3 );
function paws_and_care_single_product_category_badge() {
    global $product;
    $cats = wc_get_product_category_list( $product->get_id(), ', ' );
    if ( $cats ) {
        echo '<div class="paws-sp-category">' . wp_kses_post( $cats ) . '</div>';
    }
}

/**
 * Add stock status badge next to price
 */
add_action( 'woocommerce_single_product_summary', 'paws_and_care_single_product_stock_badge', 12 );
function paws_and_care_single_product_stock_badge() {
    global $product;
    if ( $product->is_in_stock() ) {
        echo '<span class="paws-stock-badge paws-stock-in"><i class="fa-solid fa-circle-check me-1"></i>' . esc_html__( 'In Stock', 'paws-and-care' ) . '</span>';
    } else {
        echo '<span class="paws-stock-badge paws-stock-out"><i class="fa-solid fa-circle-xmark me-1"></i>' . esc_html__( 'Out of Stock', 'paws-and-care' ) . '</span>';
    }
}

/**
 * Wrap product meta in a cleaner container
 */
add_action( 'woocommerce_single_product_summary', 'paws_and_care_single_product_meta_open', 39 );
function paws_and_care_single_product_meta_open() {
    echo '<div class="paws-product-meta-wrap">';
}

add_action( 'woocommerce_single_product_summary', 'paws_and_care_single_product_meta_close', 41 );
function paws_and_care_single_product_meta_close() {
    echo '</div>';
}

/**
 * Wrap tabs section in a card
 */
add_action( 'woocommerce_after_single_product_summary', 'paws_and_care_tabs_wrapper_open', 5 );
function paws_and_care_tabs_wrapper_open() {
    echo '<div class="paws-tabs-card">';
}

add_action( 'woocommerce_after_single_product_summary', 'paws_and_care_tabs_wrapper_close', 15 );
function paws_and_care_tabs_wrapper_close() {
    echo '</div>';
}

/**
 * Wrap related products in a styled section
 */
add_action( 'woocommerce_after_single_product_summary', 'paws_and_care_related_open', 19 );
function paws_and_care_related_open() {
    echo '<div class="paws-related-section">';
}

add_action( 'woocommerce_after_single_product_summary', 'paws_and_care_related_close', 21 );
function paws_and_care_related_close() {
    echo '</div>';
}

/**
 * Change related products columns and count
 */
add_filter( 'woocommerce_output_related_products_args', 'paws_and_care_related_products_args' );
function paws_and_care_related_products_args( $args ) {
    $args['posts_per_page'] = 4;
    $args['columns']        = 4;
    return $args;
}
