<?php

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

get_header();

// Get blog settings from customizer
$blog_layout = get_theme_mod('pawfect_blog_layout', 'right-sidebar');
$card_style = get_theme_mod('pawfect_blog_card_style', 'modern');
$layout_style = get_theme_mod('pawfect_blog_layout_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'): ?>
        <!-- No Sidebar Layout -->
        <main id="primary-content" class="pawfect-blog-main" tabindex="-1">
    <?php
else: ?>
        <!-- Sidebar Layout -->
        <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
endif; ?>
            
            <!-- Archive Header -->
            <div class="pawfect-archive-header pawfect-mb-8 pawfect-text-center">
                <?php if (is_category()): ?>
                    <h1 class="pawfect-text-3xl pawfect-font-bold pawfect-text-gray-900 pawfect-mb-4">
                        <?php printf(esc_html__('Category: %s', 'pawfect'), single_cat_title('', false)); ?>
                    </h1>
                    <?php if (category_description()): ?>
                        <div class="pawfect-text-gray-600 pawfect-max-w-2xl pawfect-mx-auto">
                            <?php echo wp_kses_post(category_description()); ?>
                        </div>
                    <?php
    endif; ?>
                <?php
elseif (is_tag()): ?>
                    <h1 class="pawfect-text-3xl pawfect-font-bold pawfect-text-gray-900 pawfect-mb-4">
                        <?php printf(esc_html__('Tag: %s', 'pawfect'), single_tag_title('', false)); ?>
                    </h1>
                    <?php if (tag_description()): ?>
                        <div class="pawfect-text-gray-600 pawfect-max-w-2xl pawfect-mx-auto">
                            <?php echo wp_kses_post(tag_description()); ?>
                        </div>
                    <?php
    endif; ?>
                <?php
elseif (is_author()): ?>
                    <h1 class="pawfect-text-3xl pawfect-font-bold pawfect-text-gray-900 pawfect-mb-4">
                        <?php printf(esc_html__('Author: %s', 'pawfect'), esc_html(get_the_author())); ?>
                    </h1>
                    <?php if (get_the_author_meta('description')): ?>
                        <div class="pawfect-text-gray-600 pawfect-max-w-2xl pawfect-mx-auto">
                            <?php echo esc_html(get_the_author_meta('description')); ?>
                        </div>
                    <?php
    endif; ?>
                <?php
elseif (is_date()): ?>
                    <h1 class="pawfect-text-3xl pawfect-font-bold pawfect-text-gray-900 pawfect-mb-4">
                        <?php
    if (is_year()) {
        printf(esc_html__('Year: %s', 'pawfect'), esc_html(get_the_date('Y')));
    }
    elseif (is_month()) {
        printf(esc_html__('Month: %s', 'pawfect'), esc_html(get_the_date('F Y')));
    }
    elseif (is_day()) {
        printf(esc_html__('Day: %s', 'pawfect'), esc_html(get_the_date()));
    }
?>
                    </h1>
                <?php
else: ?>
                    <h1 class="pawfect-text-3xl pawfect-font-bold pawfect-text-gray-900 pawfect-mb-4">
                        <?php esc_html_e('Archives', 'pawfect'); ?>
                    </h1>
                <?php
endif; ?>
            </div>

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

                <!-- Posts Grid/List -->
                <div class="pawfect-posts-container <?php echo esc_attr($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 esc_attr($blog_layout === 'no-sidebar' ? 'lg:pawfect-grid-cols-3' : 'lg:pawfect-grid-cols-2'); ?> pawfect-gap-8">
                    <?php
    endif; ?>

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

                        <?php
        // Use the OOP content.php approach with drag & drop support
        get_template_part('template-parts/post/content', '', array(
            'card_style' => $card_style,
            'layout_style' => $layout_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 for archive pages
        )); ?>

                    <?php
    endwhile; ?>

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

                <!-- Pagination -->
                <div class="pawfect-pagination pawfect-mt-12 pawfect-text-center">
                    <?php
    the_posts_pagination(array(
        'mid_size' => 2,
        'prev_text' => '←',
        'next_text' => '→',
        'class' => 'pawfect-pagination-links'
    ));
?>
                </div>

            <?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">
                        <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 in this archive.', 'pawfect'); ?>
                        </p>
                        <a href="<?php echo esc_url(home_url('/')); ?>" class="pawfect-btn pawfect-bg-pawfect-primary pawfect-text-white pawfect-px-6 pawfect-py-3 pawfect-rounded-lg pawfect-font-medium pawfect-transition-all pawfect-duration-200 hover:pawfect-bg-opacity-90">
                            <?php esc_html_e('Back to Home', 'pawfect'); ?>
                        </a>
                    </div>
                </div>

            <?php
endif; ?>

        </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(); ?>
