<?php
/**
 * @package pawwell
 */

defined( 'ABSPATH' ) || exit;


/**
 * Check if the breadcrumb is enabled or not
 */
if ( ! function_exists( 'pawwell_breadcrumb_is_enabled' ) ) :
function pawwell_breadcrumb_is_enabled() {
	return (bool) get_theme_mod( 'pawwell_enable_breadcrumb', true );
}
endif;


/**
* Footer
*/

if ( ! function_exists( 'pawwell_footer_copyrights' ) ) :
function pawwell_footer_copyrights() {
	?>
		<div class="footer-bottom">
            <p>
                <?php

                    $copyright_text = get_theme_mod( 'pawwell_footer_copyright_text' );
                    	if ( ! empty( $copyright_text ) ) :
                    		echo esc_html( $copyright_text );
                    else :
                        echo date_i18n(
                            /* translators: Copyright date format, see https://secure.php.net/date */
                            _x( 'Y', 'copyright date format', 'pawwell' )
                        );
                        ?>
                            <a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php echo esc_html( get_bloginfo( 'name' ) ); ?></a>
                        <?php
                    endif;
                ?>
            </p>
        </div>
	<?php
}
endif;
add_action( 'pawwell_action_footer', 'pawwell_footer_copyrights' );


/**
* Custom excerpt length.
*/
if ( ! function_exists( 'pawwell_my_excerpt_length' ) ) :
function pawwell_my_excerpt_length($length) {
	if ( is_admin() ) :
		return $length;
	endif;
  	return absint(get_theme_mod( 'pawwell_excerpt_length',70));
}
endif;
add_filter('excerpt_length', 'pawwell_my_excerpt_length');


/**
  * Get Breadcrumbs Content
  */
if( !function_exists( 'pawwell_get_breadcrumbs_content' ) ):
    function pawwell_get_breadcrumbs_content() {
        if ( ! is_front_page() && pawwell_breadcrumb_is_enabled() ) :

            // Build breadcrumb items
            $breadcrumb_items = array();

            if ( is_single() ) {
                // Single post breadcrumb
                $breadcrumb_items[] = array( 'label' => __( 'Home', 'pawwell' ), 'url' => home_url( '/' ) );

                $blog_page_id = get_option('page_for_posts');
                if ( $blog_page_id ) {
                    $breadcrumb_items[] = array( 'label' => get_the_title( $blog_page_id ), 'url' => get_permalink( $blog_page_id ) );
                } else {
                    $breadcrumb_items[] = array( 'label' => __( 'Journal', 'pawwell' ), 'url' => home_url( '/' ) );
                }

                $categories = get_the_category();
                if ( !empty($categories) ) {
                    $category = $categories[0];
                    $breadcrumb_items[] = array( 'label' => $category->name, 'url' => get_category_link( $category->term_id ) );
                }

                $breadcrumb_items[] = array( 'label' => get_the_title(), 'url' => '' );

            } elseif ( is_category() ) {
                $breadcrumb_items[] = array( 'label' => __( 'Home', 'pawwell' ), 'url' => home_url( '/' ) );

                $blog_page_id = get_option('page_for_posts');
                if ( $blog_page_id ) {
                    $breadcrumb_items[] = array( 'label' => get_the_title( $blog_page_id ), 'url' => get_permalink( $blog_page_id ) );
                }

                $breadcrumb_items[] = array( 'label' => single_cat_title( '', false ), 'url' => '' );

            } elseif ( is_home() ) {
                $breadcrumb_items[] = array( 'label' => __( 'Home', 'pawwell' ), 'url' => home_url( '/' ) );

                $blog_page_id = get_option('page_for_posts');
                if ( $blog_page_id ) {
                    $breadcrumb_items[] = array( 'label' => get_the_title( $blog_page_id ), 'url' => '' );
                } else {
                    $breadcrumb_items[] = array( 'label' => __( 'Blog', 'pawwell' ), 'url' => '' );
                }

            } elseif ( is_page() ) {
                $breadcrumb_items[] = array( 'label' => __( 'Home', 'pawwell' ), 'url' => home_url( '/' ) );

                // Check for parent pages
                $parent_id = wp_get_post_parent_id( get_the_ID() );
                if ( $parent_id ) {
                    $breadcrumb_items[] = array( 'label' => get_the_title( $parent_id ), 'url' => get_permalink( $parent_id ) );
                }

                $breadcrumb_items[] = array( 'label' => get_the_title(), 'url' => '' );

            } elseif ( is_shop() ) {
                $breadcrumb_items[] = array( 'label' => __( 'Home', 'pawwell' ), 'url' => home_url( '/' ) );
                $breadcrumb_items[] = array( 'label' => __( 'Shop', 'pawwell' ), 'url' => '' );

            } elseif ( is_cart() ) {
                $breadcrumb_items[] = array( 'label' => __( 'Home', 'pawwell' ), 'url' => home_url( '/' ) );
                $breadcrumb_items[] = array( 'label' => __( 'Cart', 'pawwell' ), 'url' => '' );

            } elseif ( is_checkout() ) {
                $breadcrumb_items[] = array( 'label' => __( 'Home', 'pawwell' ), 'url' => home_url( '/' ) );
                $breadcrumb_items[] = array( 'label' => __( 'Cart', 'pawwell' ), 'url' => wc_get_cart_url() );
                $breadcrumb_items[] = array( 'label' => __( 'Checkout', 'pawwell' ), 'url' => '' );

            } elseif ( is_product_category() ) {
                $breadcrumb_items[] = array( 'label' => __( 'Home', 'pawwell' ), 'url' => home_url( '/' ) );
                $breadcrumb_items[] = array( 'label' => __( 'Shop', 'pawwell' ), 'url' => get_permalink( wc_get_page_id( 'shop' ) ) );
                $breadcrumb_items[] = array( 'label' => single_term_title( '', false ), 'url' => '' );

            } elseif ( is_archive() ) {
                $breadcrumb_items[] = array( 'label' => __( 'Home', 'pawwell' ), 'url' => home_url( '/' ) );
                $breadcrumb_items[] = array( 'label' => get_the_archive_title(), 'url' => '' );

            } elseif ( is_search() ) {
                $breadcrumb_items[] = array( 'label' => __( 'Home', 'pawwell' ), 'url' => home_url( '/' ) );
                $breadcrumb_items[] = array( 'label' => __( 'Search Results', 'pawwell' ), 'url' => '' );

            } elseif ( is_404() ) {
                $breadcrumb_items[] = array( 'label' => __( 'Home', 'pawwell' ), 'url' => home_url( '/' ) );
                $breadcrumb_items[] = array( 'label' => __( '404 Not Found', 'pawwell' ), 'url' => '' );
            }

            // Only render breadcrumbs if we have items
            if ( empty( $breadcrumb_items ) ) :
                return;
            endif;

            ?>
            <!-- Breadcrumbs -->
            <div class="shop-breadcrumbs">
                <div class="container">
                    <nav class="shop-breadcrumb-nav" aria-label="<?php esc_attr_e( 'Breadcrumb', 'pawwell' ); ?>">
                        <?php foreach ( $breadcrumb_items as $i => $item ) : ?>
                            <?php if ( ! empty( $item['url'] ) ) : ?>
                                <a href="<?php echo esc_url( $item['url'] ); ?>"><?php echo esc_html( $item['label'] ); ?></a>
                                <span class="shop-breadcrumb-sep">/</span>
                            <?php else : ?>
                                <span class="shop-breadcrumb-current"><?php echo esc_html( $item['label'] ); ?></span>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </nav>
                </div>
            </div>
            <?php
        endif;
    }
endif;

/**
  * Get Page Title
  */

if( !function_exists( 'pawwell_get_title' ) ):
    function pawwell_get_title() {
        if(!is_front_page()) :
            // Skip title on cart page
            if(function_exists('is_cart') && is_cart()) :
                return;
            endif;
            // Skip title on checkout page
            if(function_exists('is_checkout') && is_checkout()) :
                return;
            endif;
            ?>
                <div class="page-title">
                    <?php pawwell_before_title_content(); ?>
                    <div class="container">
                        <h1 class="main-title"><?php the_title(); ?></h1>
                    </div>
                    <?php pawwell_after_title_content(); ?>
                </div>
            <?php
        endif;
    }
endif;


/**
  * Function for displaying menu item description
  *
  */
function pawwell_nav_description( $item_output, $item, $depth, $args ) {
    if( isset($args->theme_location) && !empty($item->description) ) :
        $description_html = '<span class="menu-bubble-description">'.esc_html($item->description).'</span>';
        return $item_output.$description_html;
    endif;
    return $item_output;
}
add_filter( 'walker_nav_menu_start_el', 'pawwell_nav_description', 10, 4 );


/**
  * Function for Minimizing dynamic CSS
  */
function pawwell_minimize_css($css){
    $css = preg_replace('/\/\*((?!\*\/).)*\*\//', '', $css);
    $css = preg_replace('/\s{2,}/', ' ', $css);
    $css = preg_replace('/\s*([:;{}])\s*/', '$1', $css);
    $css = preg_replace('/;}/', '}', $css);
    return $css;
}

/**
  * Adding blog sidebar classes to body
  */
if ( ! function_exists( 'pawwell_add_blog_sidebar_classes_to_body' ) ) :
function pawwell_add_blog_sidebar_classes_to_body($classes = '') {
    $sidebar_layout = get_theme_mod( 'pawwell_blog_single_sidebar_layout', 'no' );
    if ( 'right' === $sidebar_layout && is_single() ) :
        $classes[] = 'single-right-sidebar';

    elseif ( 'left' === $sidebar_layout && is_single() ) :
        $classes[] = 'single-left-sidebar';

    elseif ( 'no' === $sidebar_layout && is_single() ) :
        $classes[] = 'single-no-sidebar';
    endif;
    return $classes;
}
endif;
add_filter('body_class', 'pawwell_add_blog_sidebar_classes_to_body');


/**
 * Estimated Reading Time
 */
if (!function_exists('pawwell_estimated_reading_time')) {
    function pawwell_estimated_reading_time($content) {
        $word_count = str_word_count(strip_tags($content));
        $reading_time = ceil($word_count / 200); // Average reading speed: 200 words per minute
        return $reading_time;
    }
}

/**
 * Custom Comment Callback
 */
if (!function_exists('pawwell_comment_callback')) {
    function pawwell_comment_callback($comment, $args, $depth) {
        $commenter = wp_get_current_commenter();
        $req = get_option('require_name_email');
        ?>
        <div class="comment-item" <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
            <div class="comment-item-header">
                <div class="comment-item-author">
                    <div class="comment-item-avatar">
                        <?php echo get_avatar($comment->comment_author_email, 40); ?>
                    </div>
                    <div class="comment-item-info">
                        <h4 class="comment-item-name"><?php echo wp_kses_post( get_comment_author_link() ); ?></h4>
                        <span class="comment-item-date"><?php printf(esc_html__('%s ago', 'pawwell'), human_time_diff(get_comment_time('U'), time())); ?></span>
                    </div>
                </div>
            </div>
            <div class="comment-item-text">
                <?php comment_text(); ?>
            </div>
            <div class="comment-item-actions">
                <?php
                comment_reply_link(array(
                    'depth'     => $depth,
                    'max_depth' => $args['max_depth'],
                    'before'    => '<button class="comment-action-btn">',
                    'after'     => '</button>',
                    'add_below' => 'comment',
                    'reply_text' => '<svg viewBox="0 0 24 24"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg> ' . esc_html__('Reply', 'pawwell'),
                ));
                ?>
            </div>
        </div>
        <?php
    }
}

/**
 * Fallback menu
 */
if ( ! function_exists( 'pawwell_default_menu_fallback' ) ) :
function pawwell_default_menu_fallback( $args ) {
    $pages = get_pages( array(
        'sort_column'  => 'menu_order, post_title',
        'number'       => 5,
        'hierarchical' => false,
    ) );

    if ( empty( $pages ) ) {
        return;
    }

    foreach ( $pages as $page ) {
        printf(
            '<li class="menu-item"><a href="%s">%s</a></li>',
            esc_url( get_permalink( $page->ID ) ),
            esc_html( $page->post_title )
        );
    }
}
endif;

/**
 * Custom Comment Callback
 */

if ( ! function_exists( 'pawwell_related_blog_posts' ) ) :
function pawwell_related_blog_posts() {
    
    $related_args = array(
        'posts_per_page' => 3,
        'post__not_in'   => array( get_the_ID() ),
        'category__in'   => wp_get_post_categories( get_the_ID(), array( 'fields' => 'ids' ) ),
        'orderby'        => 'rand',
    );
    $related_query = new WP_Query( $related_args );
    if ( $related_query->have_posts() ) :
    ?>
    <!-- Related Posts -->
    <section class="related-section">
        <div class="container">
            <div class="related-head fade-in">
                <div>
                    <span class="related-eyebrow"><?php esc_html_e( 'Keep Reading', 'pawwell' ); ?></span>
                    <h2 class="related-title"><?php esc_html_e( 'Related Articles', 'pawwell' ); ?></h2>
                </div>
                <?php
                $blog_page_id = get_option( 'page_for_posts' );
                $blog_url     = $blog_page_id ? get_permalink( $blog_page_id ) : ( function_exists( 'get_post_type_archive_link' ) ? get_post_type_archive_link( 'post' ) : home_url( '/' ) );
                ?>
                <a href="<?php echo esc_url( $blog_url ); ?>" class="related-view-all">
                    <?php esc_html_e( 'View All Articles', 'pawwell' ); ?>
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5 12h14m-6-6 6 6-6 6"/></svg>
                </a>
            </div>

            <div class="related-grid">
                <?php
                    while ( $related_query->have_posts() ) :
                        $related_query->the_post();
                        $rel_categories = get_the_category();
                        $rel_cat        = ! empty( $rel_categories ) ? $rel_categories[0] : null;
                        $rel_rtime      = function_exists( 'pawwell_estimated_reading_time' ) ? pawwell_estimated_reading_time( get_the_content() ) : 5;
                ?>
                <article class="rel-card fade-in">
                    <a href="<?php the_permalink(); ?>" class="rel-img">
                        <?php if ( $rel_cat ) : ?>
                        <span class="rel-cat"><?php echo esc_html( $rel_cat->name ); ?></span>
                        <?php endif; ?>
                        <?php if ( has_post_thumbnail() ) : ?>
                            <?php the_post_thumbnail( 'large' ); ?>
                        <?php endif; ?>
                    </a>
                    <div class="rel-body">
                        <div class="rel-meta">
                            <span class="rel-meta-item">
                                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="3" y="5" width="18" height="16" rx="2"/><path d="M3 9h18M8 3v4M16 3v4"/></svg>
                                <?php echo esc_html( get_the_date() ); ?>
                            </span>
                            <span class="rel-meta-dot"></span>
                            <span class="rel-meta-item">
                                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg>
                                <?php echo esc_html( $rel_rtime ); ?> <?php esc_html_e( 'min', 'pawwell' ); ?>
                            </span>
                        </div>
                        <h3 class="rel-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                        <p class="rel-excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt(), 18, '&hellip;' ) ); ?></p>
                        <a href="<?php the_permalink(); ?>" class="rel-read">
                            <?php esc_html_e( 'Read More', 'pawwell' ); ?>
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5 12h14m-6-6 6 6-6 6"/></svg>
                        </a>
                    </div>
                </article>
                <?php endwhile; ?>
            </div>
        </div>
    </section>
    <?php
        wp_reset_postdata();
    endif;
}
endif;