<?php
/**
 * Template part for displaying blog sidebar
 *
 * @package Paws_And_Care
 * @since 1.0.0
 */
?>

<!-- Search Widget -->
<div class="card border-0 shadow-sm mb-4 bg-light">
    <div class="card-body p-4">
        <h5 class="mb-4"><?php esc_html_e( 'Search Articles', 'paws-and-care' ); ?></h5>
        <?php get_search_form(); ?>
    </div>
</div>

<!-- Categories Widget -->
<div class="card border-0 shadow-sm mb-4 bg-light">
    <div class="card-body p-4">
        <h5 class="mb-4"><?php esc_html_e( 'Categories', 'paws-and-care' ); ?></h5>
        <div class="list-group list-group-flush">
            <?php
            $categories = get_categories( array(
                'orderby'    => 'count',
                'order'      => 'DESC',
                'hide_empty' => true,
                'number'     => 5,
            ) );

            foreach ( $categories as $category ) :
                ?>
                <a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>" class="list-group-item list-group-item-action border-0 px-0 d-flex justify-content-between align-items-center">
                    <?php echo esc_html( $category->name ); ?>
                    <span class="badge bg-primary rounded-pill"><?php echo esc_html( $category->count ); ?></span>
                </a>
            <?php endforeach; ?>
        </div>
    </div>
</div>

<!-- Recent Posts Widget -->
<div class="card border-0 shadow-sm mb-4 bg-light">
    <div class="card-body p-4">
        <h5 class="mb-4"><?php esc_html_e( 'Recent Posts', 'paws-and-care' ); ?></h5>
        <?php
        $recent_posts = new WP_Query( array(
            'posts_per_page'      => 3,
            'post_status'         => 'publish',
            'ignore_sticky_posts' => true,
        ) );

        if ( $recent_posts->have_posts() ) :
            while ( $recent_posts->have_posts() ) :
                $recent_posts->the_post();
                ?>
                <div class="d-flex mb-3">
                    <?php if ( has_post_thumbnail() ) : ?>
                        <a href="<?php the_permalink(); ?>">
                            <?php the_post_thumbnail( 'thumbnail', array(
                                'class' => 'rounded me-3',
                                'style' => 'width: 60px; height: 60px; object-fit: cover;',
                            ) ); ?>
                        </a>
                    <?php endif; ?>
                    <div>
                        <h6 class="mb-1">
                            <a href="<?php the_permalink(); ?>" class="text-decoration-none"><?php the_title(); ?></a>
                        </h6>
                        <small class="text-muted"><?php echo esc_html( get_the_date() ); ?></small>
                    </div>
                </div>
                <?php
            endwhile;
            wp_reset_postdata();
        endif;
        ?>
    </div>
</div>

<!-- Tags Widget -->
<div class="card border-0 shadow-sm mb-4 bg-light">
    <div class="card-body p-4">
        <h5 class="mb-4"><?php esc_html_e( 'Popular Tags', 'paws-and-care' ); ?></h5>
        <div class="d-flex flex-wrap gap-2">
            <?php
            $tags = get_tags( array(
                'orderby' => 'count',
                'order'   => 'DESC',
                'number'  => 10,
            ) );

            foreach ( $tags as $tag ) :
                ?>
                <a href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" class="badge bg-primary text-decoration-none">
                    <?php echo esc_html( $tag->name ); ?>
                </a>
            <?php endforeach; ?>
        </div>
    </div>
</div>

<!-- Newsletter Widget -->
<div class="card border-0 shadow-sm bg-light">
    <div class="card-body p-4">
        <h5 class="mb-4"><?php esc_html_e( 'Newsletter', 'paws-and-care' ); ?></h5>
        <p class="text-muted mb-3"><?php esc_html_e( 'Subscribe to get the latest pet care tips and updates.', 'paws-and-care' ); ?></p>
        <form>
            <div class="mb-3">
                <input type="email" class="form-control" placeholder="<?php esc_attr_e( 'Your email address', 'paws-and-care' ); ?>">
            </div>
            <button type="submit" class="btn btn-primary w-100"><?php esc_html_e( 'Subscribe', 'paws-and-care' ); ?></button>
        </form>
    </div>
</div>

