<?php

/**
 * Template Name: Auteurs (Editors & Authors Only)
 * Description: Page listant uniquement les utilisateurs ayant le rôle "Editeur" ou "Auteur".
 *
 * Usage: créez une page dans l'admin et assignez-lui ce modèle.
 */

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

get_header();

// Récupérer uniquement les rôles Editor + Author
$authors = get_users([
    'role__in'   => ['editor', 'author'],
    'orderby'    => 'display_name',
    'order'      => 'ASC',
    'fields'     => ['ID', 'display_name', 'user_nicename', 'user_email'],
]);
?>

<div class="main-content v2 bg-white">
    <?php
    // Fil d'ariane si disponible
    if (function_exists('yoast_breadcrumb')) {
        yoast_breadcrumb('<div class="breadcrumbs"><div class="container">', '</div></div>');
    }
    ?>

    <div class="relative flex items-center justify-center min-h-[240px]">
        <div class="relative text-center">
            <h1 class="font-heading text-[1.875rem] lg:text-[2.5rem] font-normal uppercase leading-snug">
                <?php echo esc_html(get_the_title()); ?>
            </h1>
            <?php if (has_excerpt()) : ?>
                <div class="mt-4 text-base text-gray-600 max-w-3xl mx-auto">
                    <?php the_excerpt(); ?>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <div class="container py-10 md:py-16 max-w-7xl">
        <?php if (!empty($authors)) : ?>
            <div role="list" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-8 md:gap-10">
                <?php foreach ($authors as $user) :
                    $author_url = get_author_posts_url($user->ID, $user->user_nicename);
                    $avatar     = get_avatar_url($user->ID, ['size' => 256]);
                    $bio        = get_the_author_meta('description', $user->ID);
                ?>
                    <article role="listitem" class="group border border-gray-200 rounded-lg overflow-hidden bg-white hover:shadow-md transition-shadow">
                        <div class="aspect-16/9 bg-gray-100 relative overflow-hidden">
                            <?php if ($avatar) : ?>
                                <img src="<?php echo esc_url($avatar); ?>" alt="<?php echo esc_attr($user->display_name); ?>" class="absolute inset-0 w-full h-full object-cover" />
                            <?php else : ?>
                                <div class="absolute inset-0 flex items-center justify-center bg-gold text-white">
                                    <span class="font-heading text-xl uppercase"><?php echo esc_html(mb_substr($user->display_name, 0, 1)); ?></span>
                                </div>
                            <?php endif; ?>
                        </div>
                        <div class="p-6 flex flex-col gap-3">
                            <h2 class="font-heading text-xl font-normal uppercase leading-snug">
                                <a href="<?php echo esc_url($author_url); ?>" class="hover:underline" aria-label="<?php echo esc_attr(sprintf(__('Voir les articles de %s', 'laurentperrier'), $user->display_name)); ?>">
                                    <?php echo esc_html($user->display_name); ?>
                                </a>
                            </h2>
                            <?php if (!empty($bio)) : ?>
                                <p class="text-gray-600 text-sm leading-relaxed">
                                    <?php echo esc_html(wp_trim_words($bio, 28, '…')); ?>
                                </p>
                            <?php endif; ?>
                            <div class="mt-2">
                                <a href="<?php echo esc_url($author_url); ?>" class="inline-flex items-center gap-2 text-sm font-heading uppercase tracking-wider group-hover:underline">
                                    <?php _e('Voir les articles', 'laurentperrier'); ?>
                                    <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
                                        <path d="M9 18l6-6-6-6" />
                                    </svg>
                                </a>
                            </div>
                        </div>
                    </article>
                <?php endforeach; ?>
            </div>
        <?php else : ?>
            <div class="text-center py-16">
                <h2 class="font-heading text-xl uppercase mb-2"><?php _e('Aucun auteur trouvé', 'laurentperrier'); ?></h2>
                <p class="text-gray-600"><?php _e('Aucun utilisateur avec le rôle Auteur ou Éditeur.', 'laurentperrier'); ?></p>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php get_footer(); ?>