<?php

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

get_header();


// Get blog settings from customizer
$layout_style = get_theme_mod('pawfect_blog_layout_style', 'grid');
$card_style = get_theme_mod('pawfect_blog_card_style', 'modern');
$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'));

$has_sidebar = pawfect_has_sidebar();
$sidebar_position = pawfect_get_sidebar_position();
$grid_classes = $has_sidebar ? 'pawfect-grid lg:pawfect-grid-cols-[1fr_300px] pawfect-gap-8' : 'pawfect-max-w-4xl pawfect-mx-auto';

// 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">
    <div class="<?php echo esc_attr($grid_classes); ?>">

        <!-- Main Content -->
        <main id="primary-content" tabindex="-1" class="<?php echo esc_attr(pawfect_get_main_content_classes()); ?> <?php echo esc_attr($sidebar_position === 'left' ? 'pawfect-order-2' : ''); ?>">

            <?php if (have_posts()): ?>

                <!-- Blog Posts Grid/List -->
                <div class="pawfect-posts-container <?php echo $layout_style === 'grid' ? 'pawfect-posts-grid' : 'pawfect-posts-list'; ?>">
                    <?php if ($layout_style === 'grid'): ?>
                        <div class="pawfect-grid pawfect-grid-cols-1 md:pawfect-grid-cols-2 <?php echo !$has_sidebar ? 'lg:pawfect-grid-cols-3' : 'lg:pawfect-grid-cols-2'; ?> pawfect-gap-8">
                    <?php
    endif; ?>

                    <?php while (have_posts()):
        the_post(); ?>

                        <?php
        // Pass layout settings to content template
        get_template_part('template-parts/post/content', '', array(
            'layout_style' => $layout_style,
            'card_style' => $card_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; ?>

                    <?php if ($layout_style === 'grid'): ?>
                        </div>
                    <?php
    endif; ?>
                </div>

                <!-- Pagination -->
                <?php
    // Custom Pagination (same as shop)
    global $wp_query;
    $big = 999999999; // need an unlikely integer
    $pages = paginate_links(array(
        'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
        'format' => '?paged=%#%',
        'current' => max(1, get_query_var('paged')),
        'total' => $wp_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: ?>

                <!-- No Posts Found -->
                <div class="pawfect-no-posts pawfect-text-center pawfect-py-16">
                    <div class="pawfect-max-w-md pawfect-mx-auto">
                        <svg class="pawfect-w-16 pawfect-h-16 pawfect-mx-auto pawfect-text-gray-400 pawfect-mb-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
                        </svg>
                        <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 pawfect-mb-6">
                            <?php esc_html_e('Sorry, no posts were found. Please check back later or try searching for something else.', 'pawfect'); ?>
                        </p>
                        <a href="<?php echo esc_url(home_url('/')); ?>" class="pawfect-inline-flex pawfect-items-center pawfect-gap-2 pawfect-bg-pawfect-primary pawfect-text-white pawfect-px-6 pawfect-py-3 pawfect-rounded-lg hover:pawfect-bg-pawfect-primary-dark pawfect-transition-colors pawfect-duration-300">
                            <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"></path>
                            </svg>
                            <?php esc_html_e('Back to Home', 'pawfect'); ?>
                        </a>
                    </div>
                </div>

            <?php
endif; ?>

        </main>

        <!-- Sidebar -->
        <?php if ($has_sidebar): ?>
            <aside class="pawfect-blog-sidebar <?php echo esc_attr($sidebar_position === 'left' ? 'pawfect-order-1' : ''); ?>">
                <?php get_sidebar(); ?>
            </aside>
        <?php
endif; ?>

    </div>
</div>



<?php get_footer(); ?>
