<?php

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

get_header();

// Get blog settings
$blog_layout = get_theme_mod('pawfect_blog_layout', 'right-sidebar');
$blog_style = get_theme_mod('pawfect_blog_style', 'grid');
$posts_per_page = get_theme_mod('pawfect_blog_posts_per_page', 6);
$show_featured_image = get_theme_mod('pawfect_blog_show_featured_image', true);
$show_meta = get_theme_mod('pawfect_blog_show_meta', true);
$show_excerpt = get_theme_mod('pawfect_blog_show_excerpt', true);
$excerpt_length = get_theme_mod('pawfect_blog_excerpt_length', 25);
$show_read_more = get_theme_mod('pawfect_blog_show_read_more', true);
$read_more_text = get_theme_mod('pawfect_blog_read_more_text', __('Read More', 'pawfect'));

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

<div class="pawfect-container pawfect-mx-auto pawfect-px-4 pawfect-py-12">
    <?php if ($blog_layout !== 'no-sidebar'): ?>
        <div class="pawfect-grid <?php echo esc_attr($blog_layout === 'left-sidebar' ? 'lg:pawfect-grid-cols-[300px_1fr]' : 'lg:pawfect-grid-cols-[1fr_300px]'); ?> pawfect-gap-8">
            
            <?php if ($blog_layout === 'left-sidebar'): ?>
                <!-- Left Sidebar -->
                <aside class="pawfect-blog-sidebar">
                    <?php get_sidebar(); ?>
                </aside>
            <?php
    endif; ?>

            <!-- Main Content -->
            <main id="primary-content" class="pawfect-blog-main" tabindex="-1">
    <?php
else: ?>
        <!-- No Sidebar Layout -->
        <main id="primary-content" class="pawfect-blog-main pawfect-max-w-4xl pawfect-mx-auto" tabindex="-1">
    <?php
endif; ?>

                <?php
// Custom query for blog posts
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$blog_query = new WP_Query(array(
    'post_type' => 'post',
    'posts_per_page' => $posts_per_page,
    'paged' => $paged,
    'post_status' => 'publish'
));

if ($blog_query->have_posts()): ?>
                    
                    <div class="pawfect-blog-posts <?php
    if ($blog_style === 'grid') {
        echo esc_attr($blog_layout === 'no-sidebar' ? 'pawfect-grid pawfect-grid-cols-1 md:pawfect-grid-cols-2 lg:pawfect-grid-cols-3 pawfect-gap-8' : 'pawfect-grid pawfect-grid-cols-1 md:pawfect-grid-cols-2 pawfect-gap-8');
    }
    elseif ($blog_style === 'masonry') {
        echo esc_attr('pawfect-masonry pawfect-columns-1 md:pawfect-columns-2 pawfect-gap-8');
    }
    else {
        echo esc_attr('pawfect-space-y-8');
    }
?>">
                        
                        <?php while ($blog_query->have_posts()):
        $blog_query->the_post(); ?>

                            <?php
        // Use standardized content template with blog-specific arguments
        get_template_part('template-parts/post/content', null, array(
            'card_style' => get_theme_mod('pawfect_blog_card_style', 'classic'),
            'layout_style' => $blog_style,
            'show_featured_image' => $show_featured_image,
            'show_meta' => $show_meta,
            'show_excerpt' => $show_excerpt,
            'excerpt_length' => $excerpt_length,
            'show_read_more' => $show_read_more,
            'read_more_text' => $read_more_text,
            'use_drag_drop' => true // Enable drag & drop ordering
        )); ?>

                        <?php
    endwhile; ?>
                        
                    </div>

                    <!-- Pagination -->
                    <?php
    // Custom Pagination (same as shop)
    $pages = paginate_links(array(
        'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
        'format' => '?paged=%#%',
        'current' => max(1, $paged),
        'total' => $blog_query->max_num_pages,
        'type' => 'array',
        'prev_text' => '←',
        'next_text' => '→',
    ));
    if ($pages): ?>
                        <nav class="pawfect-flex pawfect-justify-center pawfect-items-center pawfect-space-x-4 pawfect-mt-12">
                            <?php foreach ($pages as $page):
            // Detect current page
            if (strpos($page, 'current') !== false) {
                // Extract page number
                preg_match('/>([0-9]+)</', $page, $matches);
                $num = isset($matches[1]) ? $matches[1] : '';
                echo '<span class="pawfect-px-4 pawfect-py-2 pawfect-rounded-md pawfect-bg-accent pawfect-text-white">' . esc_html($num) . '</span>';
            }
            else {
                // Output link
                echo wp_kses_post(str_replace('<a', '<a class="pawfect-px-4 pawfect-py-2 pawfect-rounded-md pawfect-text-text-light hover:pawfect-bg-white"', $page));
            }
        endforeach; ?>
                        </nav>
                    <?php
    endif; ?>

                <?php
else: ?>
                    
                    <div class="pawfect-no-posts pawfect-text-center pawfect-py-12">
                        <h2 class="pawfect-text-2xl pawfect-font-bold pawfect-text-gray-900 pawfect-mb-4"><?php esc_html_e('No Posts Found', 'pawfect'); ?></h2>
                        <p class="pawfect-text-gray-600"><?php esc_html_e('Sorry, no blog posts were found. Please check back later.', 'pawfect'); ?></p>
                    </div>

                <?php
endif; ?>

                <?php wp_reset_postdata(); ?>
            </main>

            <?php if ($blog_layout === 'right-sidebar'): ?>
                <!-- Right Sidebar -->
                <aside class="pawfect-blog-sidebar">
                    <?php get_sidebar(); ?>
                </aside>
            <?php
endif; ?>

    <?php if ($blog_layout !== 'no-sidebar'): ?>
        </div>
    <?php
endif; ?>
</div>

<?php get_footer(); ?>
