<?php

/**
 * Author Archive Template
 *
 * Displays basic author information and lists their Guides and Posts.
 */

if (!defined('ABSPATH')) {
    exit;
}

// Deny access to specific author archive (ID = 7) with a 404 response
$__author_obj = get_queried_object();
$__author_id  = $__author_obj && isset($__author_obj->ID) ? (int) $__author_obj->ID : 0;
if ($__author_id === 7) {
    status_header(404);
    nocache_headers();
    include get_query_template('404');
    exit;
}

get_header();

$author_obj   = get_queried_object();
$author_id    = $author_obj && isset($author_obj->ID) ? (int) $author_obj->ID : 0;
$author_name  = $author_obj && isset($author_obj->display_name) ? $author_obj->display_name : '';
$author_bio   = get_the_author_meta('description', $author_id);
$author_portrait = is_array(get_field('portrait')) ? get_field('portrait')['ID'] : null;

$post_count   = (int) count_user_posts($author_id, 'post', true);
$guides_q_for_count = new WP_Query([
    'post_type'      => 'guide',
    'post_status'    => 'publish',
    'author'         => $author_id,
    'posts_per_page' => 1,
    'fields'         => 'ids',
]);
$guide_count  = (int) $guides_q_for_count->found_posts;
wp_reset_postdata();
?>

<div class="main-content v2 bg-white">

    <?php
    if (function_exists('yoast_breadcrumb')) {
        yoast_breadcrumb('<div class="breadcrumbs"><div class="container">', '</div></div>');
    } ?>

    <div class="container py-10 md:py-14 md:pb-28 max-w-6xl">
        <div class="flex flex-col md:flex-row gap-10 md:gap-20 items-center">
            <div class="md:min-w-[300px]">
                <div class="w-full aspect-square rounded-full overflow-hidden  mx-auto md:mx-0">
                    <?php if (!empty($author_portrait)) :
                        echo wp_get_attachment_image($author_portrait, 'medium', false, array('class' => 'w-full h-full object-cover '));
                    else : ?>
                        <div class="w-full h-full flex items-center justify-center bg-gold text-white">
                            <span class="font-heading text-2xl uppercase"><?php echo esc_html(mb_substr($author_name, 0, 1)); ?></span>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
            <div class="md:grow">
                <h1 class="font-heading text-[1.875rem] lg:text-[2.5rem] font-normal uppercase leading-snug">
                    <?php echo esc_html($author_name); ?>
                </h1>
                <?php if (!empty($author_bio)) : ?>
                    <div class="mt-4 text-base text-gray-700 max-w-3xl">
                        <?php echo esc_html($author_bio); ?>

                    </div>
                <?php endif; ?>
                <?php

                $lang = icl_get_current_language();
                if (!empty(get_field('contenu_etendu_' . $lang))) : ?>
                    <p class="py-5"><?php echo get_field('contenu_etendu_' . $lang); ?></p>
                <?php else : ?>
                    <p class="py-5"><?php echo get_field('contenu_etendu'); ?></p>
                <?php endif; ?>

                <!--
                <div class="mt-4 flex flex-wrap gap-4 text-sm text-gray-600">
                    <span><?php echo sprintf(_n('%d article', '%d articles', $post_count, 'laurentperrier'), $post_count); ?></span>
                    <span>•</span>
                    <span><?php echo sprintf(_n('%d guide', '%d guides', $guide_count, 'laurentperrier'), $guide_count); ?></span>
                </div> -->
            </div>
        </div>
    </div>

    <div class="container py-10 md:py-16 max-w-7xl">
        <?php

        // List latest Guides by this author (limit 12)
        $guides_q = new WP_Query([
            'post_type'      => 'guide',
            'post_status'    => 'publish',
            'author'         => $author_id,
            'posts_per_page' => 12,
            'orderby'        => 'date',
            'order'          => 'DESC',
            'no_found_rows'  => true,
        ]);
        ?>

        <?php if ($guides_q->have_posts()) : ?>
            <!-- <h2 class="font-heading text-xl uppercase mb-6"><?php _e('Guides', 'laurentperrier'); ?></h2> -->
            <div role="list" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-8 md:gap-10 mb-12">
                <?php while ($guides_q->have_posts()) : $guides_q->the_post(); ?>
                    <?php get_template_part('template-parts/guide/card'); ?>
                <?php endwhile; ?>
            </div>
            <?php wp_reset_postdata(); ?>
        <?php endif; ?>

        <!-- <h2 class="font-heading text-xl uppercase mb-6"><?php _e('Articles', 'laurentperrier'); ?></h2> -->
        <?php
        // Last 6 standard posts for this author
        $recent_posts_q = new WP_Query([
            'post_type'      => 'post',
            'post_status'    => 'publish',
            'author'         => $author_id,
            'posts_per_page' => 6,
            'orderby'        => 'date',
            'order'          => 'DESC',
            'no_found_rows'  => true,
        ]);
        ?>
        <?php if ($recent_posts_q->have_posts()) : ?>
            <div class="grid grid-cols-1 md:grid-cols-3 gap-8">
                <?php while ($recent_posts_q->have_posts()) : $recent_posts_q->the_post(); ?>
                    <article class="group overflow-hidden bg-white hover:shadow-md transition-shadow">
                        <a href="<?php the_permalink(); ?>" class="block">
                            <div class="aspect-16/9  relative overflow-hidden">
                                <?php if (has_post_thumbnail()) : ?>
                                    <?php the_post_thumbnail('medium_large', ['class' => 'absolute inset-0 w-full h-full object-cover', 'alt' => esc_attr(get_the_title())]); ?>
                                <?php endif; ?>
                            </div>
                            <div class="p-5">
                                <h3 class="font-heading text-base uppercase leading-snug group-hover:underline"><?php the_title(); ?></h3>
                                <div class="mt-2 text-xs text-gray-500"><?php echo esc_html(get_the_date()); ?></div>
                            </div>
                        </a>
                    </article>
                <?php endwhile; ?>
            </div>
            <?php wp_reset_postdata(); ?>

        <?php endif; ?>
    </div>
</div>

<?php get_footer(); ?>
