<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * The template for displaying single blog posts
 *
 * @package Pawfect
 */

get_header();

// Page Header
if (!get_theme_mod('pawfect_page_header_disabled', false)) {
    get_template_part('template-parts/page-header');
}
?>

<main id="primary-content" class="pawfect-bg-background" tabindex="-1">
    <article>
        <?php while (have_posts()):
    the_post(); ?>



            <!-- FEATURED IMAGE -->
            <?php if (has_post_thumbnail()): ?>
                <div class="pawfect-w-full pawfect-h-64 md:pawfect-h-96 lg:pawfect-h-[500px]">
                    <?php the_post_thumbnail('full', array('class' => 'pawfect-w-full pawfect-h-full pawfect-object-cover')); ?>
                </div>
            <?php
    endif; ?>

            <!-- CONTENT & SIDEBAR -->
            <div class="pawfect-container pawfect-mx-auto pawfect-px-6 pawfect-py-12 md:pawfect-py-16">
                <?php
    $has_sidebar = pawfect_has_sidebar();
    $sidebar_position = pawfect_get_sidebar_position();
?>
                <div class="pawfect-grid pawfect-grid-cols-1 <?php echo esc_attr($has_sidebar ? 'lg:pawfect-grid-cols-3' : ''); ?> pawfect-gap-8 lg:pawfect-gap-12">

                    <!-- Main Content Column -->
                    <div class="<?php echo esc_attr($has_sidebar ? 'lg:pawfect-col-span-2' : 'pawfect-max-w-4xl pawfect-mx-auto'); ?> <?php echo esc_attr($sidebar_position === 'left' ? 'pawfect-order-2' : ''); ?>">
                        <div class="pawfect-post-content">
                            <?php the_content(); ?>

                            <?php
    wp_link_pages(array(
        'before' => '<div class="pawfect-page-links pawfect-mt-8 pawfect-text-center">',
        'after' => '</div>',
        'link_before' => '<span class="pawfect-page-number pawfect-inline-block pawfect-px-4 pawfect-py-2 pawfect-mx-1 pawfect-bg-accent pawfect-text-white pawfect-rounded">',
        'link_after' => '</span>',
    ));
?>
                        </div>

                        <!-- Post Tags -->
                        <?php if (get_the_tags()): ?>
                            <div class="pawfect-mt-8 pawfect-pt-6 pawfect-border-t pawfect-border-border-color">
                                <h4 class="pawfect-font-serif pawfect-text-lg pawfect-mb-4"><?php esc_html_e('Tags:', 'pawfect'); ?></h4>
                                <div class="pawfect-flex pawfect-flex-wrap pawfect-gap-2">
                                    <?php foreach (get_the_tags() as $tag): ?>
                                        <a href="<?php echo esc_url(get_tag_link($tag->term_id)); ?>" class="pawfect-inline-block pawfect-px-3 pawfect-py-1 pawfect-bg-accent pawfect-text-white pawfect-rounded-full pawfect-text-sm hover:pawfect-opacity-90 pawfect-transition-opacity">
                                            <?php echo esc_html($tag->name); ?>
                                        </a>
                                    <?php
        endforeach; ?>
                                </div>
                            </div>
                        <?php
    endif; ?>

                        <!-- Social Sharing -->
                        <div class="pawfect-py-8 pawfect-mt-8 pawfect-border-t pawfect-border-b pawfect-border-border-color pawfect-flex pawfect-items-center pawfect-justify-between">
                            <h4 class="pawfect-font-serif pawfect-text-lg"><?php esc_html_e('Share This Story', 'pawfect'); ?></h4>
                            <div class="pawfect-flex pawfect-space-x-4">
                                <a href="<?php echo esc_url('https://www.facebook.com/sharer/sharer.php?u=' . rawurlencode(get_permalink())); ?>" target="_blank" rel="noopener noreferrer" class="pawfect-text-text-light hover:pawfect-text-primary pawfect-transition-colors">
                                    <i class="fab fa-facebook-f fa-lg"></i>
                                </a>
                                <a href="<?php echo esc_url('https://twitter.com/intent/tweet?url=' . rawurlencode(get_permalink()) . '&text=' . rawurlencode(get_the_title())); ?>" target="_blank" rel="noopener noreferrer" class="pawfect-text-text-light hover:pawfect-text-accent pawfect-transition-colors">
                                    <i class="fab fa-twitter fa-lg"></i>
                                </a>
                                <a href="<?php echo esc_url('https://pinterest.com/pin/create/button/?url=' . rawurlencode(get_permalink()) . '&description=' . rawurlencode(get_the_title())); ?>" target="_blank" rel="noopener noreferrer" class="pawfect-text-text-light hover:pawfect-text-primary pawfect-transition-colors">
                                    <i class="fab fa-pinterest fa-lg"></i>
                                </a>
                                <a href="<?php echo esc_url('mailto:?subject=' . rawurlencode(get_the_title()) . '&body=' . rawurlencode(get_permalink())); ?>" class="pawfect-text-text-light hover:pawfect-text-primary pawfect-transition-colors">
                                    <i class="fas fa-envelope fa-lg"></i>
                                </a>
                            </div>
                        </div>
                    </div>

                    <!-- Sidebar Column -->
                    <?php if ($has_sidebar): ?>
                        <aside class="pawfect-pt-8 lg:pawfect-pt-0 <?php echo esc_attr($sidebar_position === 'left' ? 'pawfect-order-1' : ''); ?>">
                            <div class="pawfect-sticky pawfect-top-8 pawfect-space-y-6">
                                <!-- Author Bio -->
                                <?php if (get_the_author_meta('description')): ?>
                                    <div class="pawfect-bg-white pawfect-p-6 pawfect-rounded-xl pawfect-shadow-sm pawfect-border pawfect-border-gray-100 pawfect-text-center">
                                        <?php echo wp_kses_post(get_avatar(get_the_author_meta('ID'), 96, '', '', array('class' => 'pawfect-w-24 pawfect-h-24 pawfect-rounded-full pawfect-mx-auto pawfect-border-4 pawfect-border-white pawfect-shadow-md'))); ?>
                                        <h4 class="pawfect-font-serif pawfect-text-xl pawfect-text-gray-900 pawfect-mt-4 pawfect-mb-2"><?php echo esc_html(get_the_author()); ?></h4>
                                        <p class="pawfect-text-sm pawfect-text-gray-600 pawfect-leading-relaxed">
                                            <?php echo wp_kses_post(get_the_author_meta('description')); ?>
                                        </p>
                                    </div>
                                <?php
        endif; ?>

                                <!-- Regular Sidebar -->
                                <?php get_sidebar(); ?>
                            </div>
                        </aside>
                    <?php
    endif; ?>
                </div>

                <!-- Related Posts Section -->
                <?php
    $related_posts = get_posts(array(
        'category__in' => wp_get_post_categories(get_the_ID()),
        'numberposts' => 3,
        'post__not_in' => array(get_the_ID()),
        'orderby' => 'rand',
        'post_status' => 'publish'
    ));

    if ($related_posts): ?>
                    <section class="pawfect-py-16 md:pawfect-py-24 pawfect-border-t pawfect-border-border-color pawfect-mt-16">
                        <h2 class="pawfect-text-center pawfect-font-serif pawfect-text-3xl md:pawfect-text-4xl pawfect-mb-12"><?php esc_html_e('You Might Also Like', 'pawfect'); ?></h2>
                        <div class="pawfect-grid pawfect-grid-cols-1 md:pawfect-grid-cols-2 lg:pawfect-grid-cols-3 pawfect-gap-8">
                            <?php foreach ($related_posts as $post):
            setup_postdata($post); ?>
                                <?php if (get_the_title() && get_permalink()): ?>
                                    <div class="pawfect-bg-white pawfect-rounded-lg pawfect-shadow-md pawfect-overflow-hidden pawfect-flex pawfect-flex-col">
                                        <?php if (has_post_thumbnail()): ?>
                                            <div class="pawfect-post-thumbnail pawfect-relative pawfect-overflow-hidden">
                                                <a href="<?php the_permalink(); ?>" class="pawfect-block">
                                                    <?php the_post_thumbnail('medium_large', array('class' => 'pawfect-w-full pawfect-h-64 pawfect-object-cover pawfect-transition-transform pawfect-duration-300 hover:pawfect-scale-105')); ?>
                                                </a>
                                            </div>
                                        <?php
                endif; ?>
                                        <div class="pawfect-p-6 pawfect-flex-1 pawfect-flex pawfect-flex-col">
                                            <?php
                $categories = get_the_category();
                if (!empty($categories)): ?>
                                                <p class="pawfect-text-xs pawfect-text-text-light pawfect-uppercase pawfect-tracking-wider pawfect-mb-2"><?php echo esc_html($categories[0]->name); ?></p>
                                            <?php
                endif; ?>
                                            <h3 class="pawfect-font-serif pawfect-text-xl pawfect-leading-tight pawfect-flex-1">
                                                <a href="<?php the_permalink(); ?>" class="hover:pawfect-text-accent pawfect-transition-colors"><?php the_title(); ?></a>
                                            </h3>
                                        </div>
                                    </div>
                                <?php
            endif; ?>
                            <?php
        endforeach;
        wp_reset_postdata(); ?>
                        </div>
                    </section>
                <?php
    endif; ?>

                <!-- Comments Section -->
                <?php if (comments_open() || get_comments_number()): ?>
                    <section id="comments" class="pawfect-py-16">
                        <div class="pawfect-max-w-3xl pawfect-mx-auto">
                            <?php comments_template(); ?>
                        </div>
                    </section>
                <?php
    endif; ?>
            </div>

        <?php
endwhile; ?>
    </article>
</main>

<?php get_footer(); ?>
