<?php
/**
 * Render template for Child Author Blog Details block
 */

// If direct access, exit
if (!defined('ABSPATH')) {
    exit;
}

$is_editor = defined('REST_REQUEST') && REST_REQUEST;

// Attributes
$show_breadcrumb = isset($attributes['showBreadcrumb']) ? $attributes['showBreadcrumb'] : true;
$show_image = isset($attributes['showImage']) ? $attributes['showImage'] : true;
$show_author_bottom = isset($attributes['showAuthorBottom']) ? $attributes['showAuthorBottom'] : true;
$show_nav = isset($attributes['showNav']) ? $attributes['showNav'] : true;
$show_comments = isset($attributes['showComments']) ? $attributes['showComments'] : true;
$show_related = isset($attributes['showRelated']) ? $attributes['showRelated'] : true;

$label_back = isset($attributes['labelBack']) ? $attributes['labelBack'] : 'Back to all posts';
$label_by = isset($attributes['labelBy']) ? $attributes['labelBy'] : 'By';
$label_comments = isset($attributes['labelComments']) ? $attributes['labelComments'] : 'Join the Conversation!';
$label_leave_reply = isset($attributes['labelLeaveReply']) ? $attributes['labelLeaveReply'] : 'Leave a Comment!';
$label_related = isset($attributes['labelRelated']) ? $attributes['labelRelated'] : 'You Might Also Like...';

$title_color = isset($attributes['titleColor']) ? $attributes['titleColor'] : '#264653';
$accent_color = isset($attributes['accentColor']) ? $attributes['accentColor'] : '#EF476F';
$bg_color = isset($attributes['bgColor']) ? $attributes['bgColor'] : '#FFFDF5';

// Data Preparation
if ($is_editor) {
    // Placeholder Data
    $data = [
        'title' => 'How to Make Your Own [Character] Puppet!',
        'date' => 'November 15, 2025',
        'author' => 'Danielle Steel',
        'avatar' => RSWPTHEMES_AWT_PLUGIN_URL . 'includes/assets/images/placeholder-avatar.jpg',
        'category' => 'Activities & Crafts',
        'content' => '<p>Hello, story explorers! Today we\'re going to make a super easy puppet.</p>',
        'featured_image' => RSWPTHEMES_AWT_PLUGIN_URL . 'includes/assets/images/placeholder-landscape.jpg',
        'author_bio' => 'Danielle Steel is the award-winning creator of the [Book Series] series.',
        'comments_count' => 3,
        'post_id' => 0,
    ];
} else {
    // Real Data
    $post_id = get_the_ID();
    $author_id = get_the_author_meta('ID');
    $cats = get_the_category();
    $cat_name = !empty($cats) ? $cats[0]->name : 'Uncategorized';

    $data = [
        'title' => get_the_title(),
        'date' => get_the_date(),
        'author' => get_the_author(),
        'avatar' => get_avatar_url($author_id),
        'category' => $cat_name,
        'content' => apply_filters('the_content', get_the_content()),
        'featured_image' => get_the_post_thumbnail_url($post_id, 'full'),
        'author_bio' => get_the_author_meta('description'),
        'comments_count' => get_comments_number(),
        'post_id' => $post_id,
    ];
}
?>

<div class="font-sans text-navy antialiased blog-details-block"
    style="background-color: <?php echo esc_attr($bg_color); ?>; color: <?php echo esc_attr($title_color); ?>;">

    <section class="bg-white pt-12 pb-16">
        <div class="container mx-auto px-6 max-w-4xl">
            <?php if ($show_breadcrumb): ?>
                <a href="<?php echo $is_editor ? '#' : get_post_type_archive_link('post'); ?>"
                    class="font-bold hover:opacity-80 mb-6 inline-block" style="color: #00C6FF;">
                    &larr; <?php echo esc_html($label_back); ?>
                </a>
            <?php endif; ?>

            <span class="block font-bold uppercase text-sm mb-2" style="color: <?php echo esc_attr($accent_color); ?>;">
                <?php echo esc_html($data['category']); ?>
            </span>

            <h1 class="text-4xl md:text-6xl font-black mb-6 leading-tight"
                style="color: <?php echo esc_attr($title_color); ?>;">
                <?php echo esc_html($data['title']); ?>
            </h1>

            <div class="flex items-center space-x-4 opacity-70">
                <img src="<?php echo esc_url($data['avatar']); ?>" alt="<?php echo esc_attr($data['author']); ?>"
                    class="w-12 h-12 rounded-full border-2" style="border-color: #FFD166;">
                <div>
                    <span class="font-bold"><?php echo esc_html($label_by . ' ' . $data['author']); ?></span>
                    <span class="block text-sm"><?php echo esc_html($data['date']); ?></span>
                </div>
            </div>
        </div>
    </section>

    <?php if ($show_image && $data['featured_image']): ?>
        <div class="container mx-auto px-6 max-w-5xl -mt-8 mb-12">
            <img src="<?php echo esc_url($data['featured_image']); ?>" alt="<?php echo esc_attr($data['title']); ?>"
                class="w-full h-auto object-cover rounded-xl shadow-lg">
        </div>
    <?php endif; ?>

    <section class="pb-24">
        <div class="container mx-auto px-6 max-w-3xl">

            <article class="awt-dynamic-content text-lg leading-relaxed space-y-6 prose prose-lg max-w-none">
                <?php echo $data['content']; ?>
            </article>

            <?php if ($show_author_bottom): ?>
                <div class="border-t border-navy/10 mt-16 pt-10">
                    <div class="p-6 rounded-lg flex flex-col sm:flex-row gap-4 items-center text-center sm:text-left"
                        style="background-color: <?php echo esc_attr($bg_color); ?>;">
                        <img src="<?php echo esc_url($data['avatar']); ?>" alt="<?php echo esc_attr($data['author']); ?>"
                            class="w-20 h-20 rounded-full border-4 flex-shrink-0" style="border-color: #FFD166;">
                        <div>
                            <h4 class="text-xl font-black" style="color: <?php echo esc_attr($title_color); ?>;">About the
                                Author</h4>
                            <p class="opacity-80 text-sm">
                                <?php echo esc_html($data['author_bio']); ?>
                            </p>
                        </div>
                    </div>
                </div>
            <?php endif; ?>

            <?php if ($show_nav): ?>
                <div class="flex justify-between items-center mt-12 font-bold" style="color: #00C6FF;">
                    <?php if (!$is_editor): ?>
                        <?php
                        $prev_post = get_previous_post();
                        if ($prev_post):
                            ?>
                            <a href="<?php echo get_permalink($prev_post->ID); ?>" class="hover:opacity-80">
                                &larr; <?php echo get_the_title($prev_post->ID); ?>
                            </a>
                        <?php else: ?>
                            <span></span>
                        <?php endif; ?>

                        <?php
                        $next_post = get_next_post();
                        if ($next_post):
                            ?>
                            <a href="<?php echo get_permalink($next_post->ID); ?>" class="hover:opacity-80">
                                <?php echo get_the_title($next_post->ID); ?> &rarr;
                            </a>
                        <?php endif; ?>
                    <?php else: ?>
                        <a href="#" class="hover:opacity-80">&larr; Previous Post</a>
                        <a href="#" class="hover:opacity-80">Next Post &rarr;</a>
                    <?php endif; ?>
                </div>
            <?php endif; ?>

            <?php if ($show_comments): ?>
                <div class="awt-comments-wrapper awt-dynamic-content border-t border-navy/10 mt-16 pt-10">
                    <h3 class="text-3xl font-black mb-8" style="color: <?php echo esc_attr($title_color); ?>;">
                        <?php echo esc_html($label_comments . ' (' . $data['comments_count'] . ' Comments)'); ?>
                    </h3>

                    <?php
                    if (!$is_editor) {
                        // Pass label to Theme via Global
                        $GLOBALS['awt_label_leave_reply'] = $label_leave_reply;

                        // Delegate to Theme
                        if (comments_open() || get_comments_number()) {
                            comments_template();
                        } else {
                            // Optional: Show message if comments are closed but show_comments is true
                            echo '<p class="text-navy/60 italic">' . __('Comments are closed.', 'author-website-templates') . '</p>';
                        }
                    } else {
                        // Editor Placeholder
                        ?>
                        <div class="space-y-6">
                            <div class="flex space-x-4">
                                <div class="w-12 h-12 rounded-full flex-shrink-0 bg-yellow-200"></div>
                                <div class="bg-white p-5 rounded-xl shadow-sm w-full">
                                    <div class="flex justify-between items-center mb-1">
                                        <span class="font-black text-lg">Sarah (Parent)</span>
                                        <span class="text-sm opacity-60">Nov 16, 2025</span>
                                    </div>
                                    <p class="opacity-80">We just did this! Thanks!</p>
                                </div>
                            </div>
                        </div>
                        <div class="mt-12">
                            <h3 class="text-3xl font-black mb-8"><?php echo esc_html($label_leave_reply); ?></h3>
                            <form class="space-y-6">
                                <textarea rows="5" class="w-full p-4 rounded-lg bg-gray-50"></textarea>
                                <button type="button"
                                    class="btn-fun btn-yellow px-8 py-3 rounded-full font-extrabold uppercase bg-yellow-400 text-navy shadow-lg">Post
                                    Comment</button>
                            </form>
                        </div>
                        <?php
                    }
                    ?>
                </div>
            <?php endif; ?>
        </div>
    </section>

    <?php if ($show_related): ?>
        <section id="related-posts" class="py-24 relative" style="background-color: rgba(0, 198, 255, 0.1);">
            <div class="absolute top-0 left-0 w-full">
                <svg viewBox="0 0 1440 100" fill="none" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none">
                    <path d="M0 0H1440V100C1192.5 50.503 919.5 25.503 720 25.503C520.5 25.503 247.5 50.503 0 100V0Z"
                        fill="<?php echo esc_attr($bg_color); ?>" />
                </svg>
            </div>
            <div class="awt-related-posts awt-container mx-auto px-6 relative z-10 mt-12">
                <h2 class="text-4xl md:text-5xl font-black text-center mb-12"
                    style="color: <?php echo esc_attr($title_color); ?>;"><?php echo esc_html($label_related); ?></h2>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-8">
                    <?php
                    if ($is_editor) {
                        for ($i = 0; $i < 3; $i++)
                            echo '<div class="bg-white p-6 rounded-xl h-64"></div>';
                    } else {
                        $related = rswpthemes_awt_get_related_posts($post_id);
                        if ($related->have_posts()) {
                            while ($related->have_posts()) {
                                $related->the_post();
                                echo '<a href="' . get_permalink() . '" class="block bg-white p-6 rounded-xl shadow-lg transition-all hover:scale-105">';
                                if (has_post_thumbnail())
                                    the_post_thumbnail('medium_large', ['class' => 'rounded-lg mb-4 w-full aspect-video object-cover']);
                                echo '<h3 class="text-xl font-black mb-2">' . get_the_title() . '</h3></a>';
                            }
                            wp_reset_postdata();
                        }
                    }
                    ?>
                </div>
            </div>
        </section>
    <?php endif; ?>
</div>