<?php
/**
 * Template part for displaying the WooCommerce products section on the homepage
 *
 * @package Paws_And_Care
 * @since 1.0.0
 */

// Only works if WooCommerce is active
if ( ! class_exists( 'WooCommerce' ) ) {
    return;
}

// Check if section is enabled
$woo_enable = get_theme_mod( 'paws_and_care_woo_enable', true );
if ( ! $woo_enable ) {
    return;
}

// Get customizer values
$woo_title    = get_theme_mod( 'paws_and_care_woo_title', __( 'Our Products', 'paws-and-care' ) );
$woo_subtitle = get_theme_mod( 'paws_and_care_woo_subtitle', __( 'Check out our latest products for your furry friends.', 'paws-and-care' ) );
$woo_count    = get_theme_mod( 'paws_and_care_woo_count', 4 );
$woo_columns  = get_theme_mod( 'paws_and_care_woo_columns', 4 );
$woo_order    = get_theme_mod( 'paws_and_care_woo_order', 'latest' );
$show_button  = get_theme_mod( 'paws_and_care_woo_show_button', true );
$button_text  = get_theme_mod( 'paws_and_care_woo_button_text', __( 'View All Products', 'paws-and-care' ) );

// Query Arguments
$args = array(
    'post_type'      => 'product',
    'posts_per_page' => $woo_count,
    'post_status'    => 'publish',
);

switch ( $woo_order ) {
    case 'featured':
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'product_visibility',
                'field'    => 'name',
                'terms'    => 'featured',
                'operator' => 'IN',
            ),
        );
        break;
    case 'best_selling':
        $args['meta_key'] = 'total_sales';
        $args['orderby']  = 'meta_value_num';
        break;
    case 'top_rated':
        $args['meta_key'] = '_wc_average_rating';
        $args['orderby']  = 'meta_value_num';
        break;
    case 'on_sale':
        $product_ids_on_sale = wc_get_product_ids_on_sale();
        $args['post__in'] = array_merge( array( 0 ), $product_ids_on_sale );
        break;
    case 'latest':
    default:
        $args['orderby'] = 'date';
        $args['order']   = 'DESC';
        break;
}

$products_query = new WP_Query( $args );

// If no products found, don't show the section
if ( ! $products_query->have_posts() ) {
    return;
}

// Bootstrap column class
$woo_columns = (int) $woo_columns;
$col_class = 'col-6 col-lg-' . ( 12 / $woo_columns );
?>

<section id="woocommerce-section" class="py-5 woocommerce-section">
    <?php if ( function_exists( 'paws_and_care_section_shortcut' ) ) {
        paws_and_care_section_shortcut( 'paws_and_care_woo', __( 'Edit Products Section', 'paws-and-care' ) );
    } ?>
    <div class="container">
        <!-- Section Header -->
        <div class="row justify-content-center mb-5">
            <div class="col-lg-7 text-center">
                <h2 class="section-title mt-3"><?php echo esc_html( $woo_title ); ?></h2>
                <?php if ( ! empty( $woo_subtitle ) ) : ?>
                    <p class="lead"><?php echo esc_html( $woo_subtitle ); ?></p>
                <?php endif; ?>
            </div>
        </div>

        <!-- Products Grid -->
        <div class="row g-4 woocommerce paws-products-grid">
            <?php
            $product_index = 0;
            while ( $products_query->have_posts() ) :
                $products_query->the_post();
                global $product;

                // Product data
                $average_rating     = $product->get_average_rating();
                $review_count       = $product->get_review_count();
                ?>
                <div class="<?php echo esc_attr( $col_class ); ?>" style="--card-delay: <?php echo esc_attr( $product_index * 0.1 ); ?>s;">
                    <div class="paws-product-card">
                        <!-- Product Image -->
                        <div class="paws-product-image">
                            <a href="<?php the_permalink(); ?>" class="paws-product-image-link">
                                <?php if ( has_post_thumbnail() ) : ?>
                                    <?php the_post_thumbnail( 'woocommerce_thumbnail', array( 'class' => 'product-thumb' ) ); ?>
                                <?php else : ?>
                                    <img src="<?php echo esc_url( wc_placeholder_img_src( 'woocommerce_thumbnail' ) ); ?>" alt="<?php the_title_attribute(); ?>" class="product-thumb">
                                <?php endif; ?>
                            </a>

                            <!-- Badges -->
                            <div class="paws-product-badges">
                                <?php if ( $product->is_on_sale() ) : ?>
                                    <span class="paws-badge paws-badge-sale">
                                        <?php
                                        if ( $product->is_type( 'simple' ) ) {
                                            $regular  = (float) $product->get_regular_price();
                                            $sale     = (float) $product->get_sale_price();
                                            if ( $regular > 0 ) {
                                                $percent = round( 100 - ( $sale / $regular * 100 ) );
                                                /* translators: %d: sale percentage */
                                                printf( esc_html__( '-%d%%', 'paws-and-care' ), $percent );
                                            } else {
                                                esc_html_e( 'Sale', 'paws-and-care' );
                                            }
                                        } else {
                                            esc_html_e( 'Sale', 'paws-and-care' );
                                        }
                                        ?>
                                    </span>
                                <?php endif; ?>
                                <?php if ( $product->is_featured() ) : ?>
                                    <span class="paws-badge paws-badge-featured">
                                        <i class="fa-solid fa-star"></i>
                                    </span>
                                <?php endif; ?>
                            </div>

                            <!-- Quick Actions Overlay -->
                            <div class="paws-product-actions">
                                <a href="<?php the_permalink(); ?>" class="paws-action-btn" title="<?php esc_attr_e( 'View Details', 'paws-and-care' ); ?>">
                                    <i class="fa-solid fa-eye"></i>
                                </a>
                                <?php if ( $product->is_purchasable() && $product->is_in_stock() && $product->is_type( 'simple' ) ) : ?>
                                    <a href="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="paws-action-btn ajax_add_to_cart add_to_cart_button" data-product_id="<?php echo esc_attr( $product->get_id() ); ?>" data-quantity="1" title="<?php esc_attr_e( 'Add to Cart', 'paws-and-care' ); ?>">
                                        <i class="fa-solid fa-cart-plus"></i>
                                    </a>
                                <?php endif; ?>
                            </div>
                        </div>

                        <!-- Product Info -->
                        <div class="paws-product-info">
                            <?php
                            $cats = get_the_terms( $product->get_id(), 'product_cat' );
                            if ( $cats && ! is_wp_error( $cats ) ) :
                                $first_cat = $cats[0];
                            ?>
                                <a href="<?php echo esc_url( get_term_link( $first_cat ) ); ?>" class="paws-product-cat">
                                    <?php echo esc_html( $first_cat->name ); ?>
                                </a>
                            <?php endif; ?>

                            <h3 class="paws-product-title">
                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </h3>

                            <?php if ( $average_rating > 0 ) : ?>
                                <div class="paws-product-rating">
                                    <div class="paws-stars">
                                        <?php
                                        for ( $i = 1; $i <= 5; $i++ ) {
                                            if ( $i <= floor( $average_rating ) ) {
                                                echo '<i class="fa-solid fa-star"></i>';
                                            } elseif ( $i - 0.5 <= $average_rating ) {
                                                echo '<i class="fa-solid fa-star-half-stroke"></i>';
                                            } else {
                                                echo '<i class="fa-regular fa-star"></i>';
                                            }
                                        }
                                        ?>
                                    </div>
                                    <span class="paws-review-count">
                                        <?php
                                        /* translators: %d: review count */
                                        printf( esc_html( _n( '(%d)', '(%d)', $review_count, 'paws-and-care' ) ), $review_count );
                                        ?>
                                    </span>
                                </div>
                            <?php endif; ?>

                            <div class="paws-product-footer">
                                <div class="paws-product-price">
                                    <?php echo wp_kses_post( $product->get_price_html() ); ?>
                                </div>
                                <?php if ( $product->is_purchasable() && $product->is_in_stock() ) : ?>
                                    <a href="<?php echo esc_url( $product->is_type( 'simple' ) ? $product->add_to_cart_url() : get_permalink() ); ?>"
                                       class="paws-add-to-cart-btn <?php echo $product->is_type( 'simple' ) ? 'ajax_add_to_cart add_to_cart_button' : ''; ?>"
                                       <?php if ( $product->is_type( 'simple' ) ) : ?>
                                           data-product_id="<?php echo esc_attr( $product->get_id() ); ?>"
                                           data-quantity="1"
                                       <?php endif; ?>
                                       title="<?php echo esc_attr( $product->add_to_cart_text() ); ?>">
                                        <i class="fa-solid fa-bag-shopping"></i>
                                    </a>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php
                $product_index++;
            endwhile;
            wp_reset_postdata();
            ?>
        </div>

        <?php if ( $show_button ) : ?>
            <div class="row mt-5">
                <div class="col-12 text-center">
                    <a href="<?php echo esc_url( wc_get_page_permalink( 'shop' ) ); ?>" class="btn btn-primary btn-lg px-5">
                        <?php echo esc_html( $button_text ); ?> <i class="fa-solid fa-arrow-right ms-2"></i>
                    </a>
                </div>
            </div>
        <?php endif; ?>
    </div>
</section>
