<?php
/**
 * Render callback for the Latest Stories block.
 *
 * @package AuthorWebsiteTemplates
 */

$posts_to_show = isset($attributes['postsToShow']) ? $attributes['postsToShow'] : 3;
$order = isset($attributes['order']) ? $attributes['order'] : 'desc';
$order_by = isset($attributes['orderBy']) ? $attributes['orderBy'] : 'date';
$section_title = isset($attributes['sectionTitle']) ? $attributes['sectionTitle'] : 'Latest Adventures';
$read_more_text = isset($attributes['readMoreText']) ? $attributes['readMoreText'] : 'Read Adventure';
$show_date = isset($attributes['showDate']) ? $attributes['showDate'] : true;
$show_category = isset($attributes['showCategory']) ? $attributes['showCategory'] : true;
$show_excerpt = isset($attributes['showExcerpt']) ? $attributes['showExcerpt'] : true;
$show_read_more = isset($attributes['showReadMore']) ? $attributes['showReadMore'] : true;
$show_view_all = isset($attributes['showViewAll']) ? $attributes['showViewAll'] : true;
$view_all_text = isset($attributes['viewAllText']) ? $attributes['viewAllText'] : 'View All Posts →';
$view_all_url = isset($attributes['viewAllUrl']) ? $attributes['viewAllUrl'] : '#';

$stories_style = isset($attributes['storiesStyle']) ? $attributes['storiesStyle'] : 'fun';

$is_fun = $stories_style === 'fun';
$is_elegant = $stories_style === 'elegant';

$section_bg_color = isset($attributes['sectionBgColor']) ? $attributes['sectionBgColor'] : '#FFFDF5';
if ($is_elegant && $section_bg_color === '#FFFDF5') {
    $section_bg_color = '#ffffff';
}

$card_bg_color = isset($attributes['cardBgColor']) ? $attributes['cardBgColor'] : '#ffffff';
$title_color = isset($attributes['titleColor']) ? $attributes['titleColor'] : '#264653';
$meta_color = isset($attributes['metaColor']) ? $attributes['metaColor'] : '#EF476F';

// Override default colors for Elegant if user hasn't set custom ones (assuming defaults match fun style defaults)
if ($is_elegant) {
    if ($title_color === '#264653')
        $title_color = ''; // Use class color
    if ($meta_color === '#EF476F')
        $meta_color = ''; // Use class color
}

$read_more_color = isset($attributes['readMoreColor']) && !empty($attributes['readMoreColor']) ? $attributes['readMoreColor'] : $meta_color;
$view_all_color = isset($attributes['viewAllColor']) && !empty($attributes['viewAllColor']) ? $attributes['viewAllColor'] : '';

// CSS Classes based on style
$header_class = $is_elegant
    ? 'flex justify-between items-end mb-12 border-b border-gray-100 pb-6'
    : 'flex justify-between items-end mb-12';

$title_class = $is_elegant
    ? 'text-3xl md:text-4xl font-serif font-bold text-brand-dark text-center md:text-left'
    : 'text-4xl font-black text-center md:text-left';

$view_all_class = $is_elegant
    ? 'hidden md:inline-block font-medium text-brand-dark hover:text-gray-600 mb-2'
    : 'hidden md:inline-block font-bold transition-colors mb-2 text-blue-sky hover:text-navy';

$inner_card_class = $is_elegant
    ? ''
    : 'overflow-hidden rounded-xl shadow-lg mb-4';

$image_wrapper_class = $is_elegant
    ? 'h-56 rounded-lg bg-gray-100 mb-4 overflow-hidden'
    : ''; // Fun uses inner_card for overflow hidden

$image_class = $is_elegant
    ? 'w-full h-full object-cover transition-transform duration-500 group-hover:scale-105'
    : 'w-full h-48 object-cover transition-transform duration-500 group-hover:scale-110';

$meta_class = $is_elegant
    ? 'text-brand-gray font-bold text-xs uppercase tracking-widest block mb-1'
    : 'font-bold text-sm uppercase block mb-2';

$post_title_class = $is_elegant
    ? 'text-xl font-serif font-bold text-brand-dark mt-2 group-hover:text-gray-600 transition-colors'
    : 'text-xl font-black mt-2 group-hover:opacity-75 transition-opacity';

$args = array(
    'post_type' => 'post',
    'posts_per_page' => $posts_to_show,
    'post_status' => 'publish',
    'orderby' => $order_by,
    'order' => $order,
);

$latest_posts = new WP_Query($args);

$wrapper_attributes = get_block_wrapper_attributes(array(
    'class' => 'awt-child-author-latest-stories py-24',
    'style' => 'background-color: ' . esc_attr($section_bg_color) . ';',
));
?>

<section <?php echo $wrapper_attributes; ?>>
    <div class="awt-container mx-auto px-6">
        <div class="<?php echo esc_attr($header_class); ?>">
            <h2 class="<?php echo esc_attr($title_class); ?>"
                style="<?php echo $title_color ? 'color: ' . esc_attr($title_color) . ';' : ''; ?>">
                <?php echo esc_html($section_title); ?>
            </h2>
            <?php if ($show_view_all): ?>
                <a href="<?php echo esc_url($view_all_url); ?>" class="<?php echo esc_attr($view_all_class); ?>"
                    style="<?php echo !empty($view_all_color) ? 'color: ' . esc_attr($view_all_color) . ';' : ''; ?>">
                    <?php echo esc_html($view_all_text); ?>
                </a>
            <?php endif; ?>
        </div>

        <?php if ($latest_posts->have_posts()): ?>
            <div class="grid grid-cols-1 md:grid-cols-3 gap-8">
                <?php while ($latest_posts->have_posts()):
                    $latest_posts->the_post(); ?>
                    <article class="group cursor-pointer">
                        <div class="<?php echo esc_attr($inner_card_class); ?>"
                            style="<?php echo $is_fun ? 'background-color: ' . esc_attr($card_bg_color) . ';' : ''; ?>">

                            <?php if ($is_elegant): ?>
                                <div class="<?php echo esc_attr($image_wrapper_class); ?>"><?php endif; ?>

                                <a href="<?php the_permalink(); ?>" class="block">
                                    <?php if (has_post_thumbnail()): ?>
                                        <img src="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'medium_large'); ?>"
                                            alt="<?php the_title_attribute(); ?>" class="<?php echo esc_attr($image_class); ?>">
                                    <?php else: ?>
                                        <div class="w-full h-48 bg-gray-200 flex items-center justify-center text-gray-400">
                                            <?php esc_html_e('No Image', 'author-website-templates'); ?>
                                        </div>
                                    <?php endif; ?>
                                </a>

                                <?php if ($is_elegant): ?>
                                </div><?php endif; ?>
                        </div>

                        <?php if ($show_date || $show_category): ?>
                            <span class="<?php echo esc_attr($meta_class); ?>"
                                style="<?php echo $meta_color ? 'color: ' . esc_attr($meta_color) . ';' : ''; ?>">
                                <?php
                                if ($show_date) {
                                    echo get_the_date();
                                }

                                if ($show_date && $show_category && has_category()) {
                                    echo ' &bull; ';
                                }

                                if ($show_category) {
                                    $categories = get_the_category();
                                    if (!empty($categories)) {
                                        echo esc_html($categories[0]->name);
                                    }
                                }
                                ?>
                            </span>
                        <?php endif; ?>

                        <h3 class="<?php echo esc_attr($post_title_class); ?>">
                            <a href="<?php the_permalink(); ?>"
                                style="<?php echo $title_color ? 'color: ' . esc_attr($title_color) . ';' : ''; ?>">
                                <?php the_title(); ?>
                            </a>
                        </h3>

                        <?php if ($show_excerpt): ?>
                            <div class="text-gray-600 mt-2 text-base leading-relaxed">
                                <?php echo wp_trim_words(get_the_excerpt(), 20); ?>
                            </div>
                        <?php endif; ?>

                        <?php if ($show_read_more): ?>
                            <a href="<?php the_permalink(); ?>" class="mt-4 font-bold inline-block hover:underline"
                                style="color: <?php echo esc_attr($read_more_color); ?>;">
                                <?php echo esc_html($read_more_text); ?> &rarr;
                            </a>
                        <?php endif; ?>
                    </article>
                <?php endwhile; ?>
            </div>
            <?php wp_reset_postdata(); ?>
        <?php else: ?>
            <div class="text-center p-8 border-2 border-dashed border-gray-300 rounded-lg bg-white/50">
                <p class="text-gray-500 text-lg">
                    <?php esc_html_e('No adventures found yet!', 'author-website-templates'); ?>
                </p>
            </div>
        <?php endif; ?>
    </div>
</section>