<?php
/**
 * Template for displaying individual testimonial posts
 *
 * @package Nihongo
 */

get_header(); ?>

<main id="main" class="site-main">
    <?php while (have_posts()) : the_post(); ?>
        <?php
        $name = get_post_meta(get_the_ID(), '_testimonial_name', true);
        $country = get_post_meta(get_the_ID(), '_testimonial_country', true);
        $course = get_post_meta(get_the_ID(), '_testimonial_course', true);
        $rating = get_post_meta(get_the_ID(), '_testimonial_rating', true);
        ?>

        <!-- Testimonial Hero Section -->
        <section class="testimonial-hero">
            <div class="testimonial-hero-background">
                <?php if (has_post_thumbnail()) : ?>
                    <?php the_post_thumbnail('hero-image', array('class' => 'testimonial-hero-image')); ?>
                <?php else : ?>
                    <img src="<?php echo get_template_directory_uri(); ?>/assets/img/r1.jpg" alt="<?php the_title(); ?>" class="testimonial-hero-image">
                <?php endif; ?>
                <div class="testimonial-hero-overlay"></div>
            </div>
            <div class="container">
                <div class="testimonial-hero-content">
                    <h1 class="testimonial-hero-title"><?php the_title(); ?></h1>
                    <div class="testimonial-hero-meta">
                        <?php if ($name) : ?>
                            <span class="testimonial-meta-item">
                                <strong><?php echo nihongo_translate('name'); ?>:</strong> <?php echo esc_html($name); ?>
                            </span>
                        <?php endif; ?>
                        <?php if ($country) : ?>
                            <span class="testimonial-meta-item">
                                <strong><?php echo nihongo_translate('country'); ?>:</strong> <?php echo esc_html($country); ?>
                            </span>
                        <?php endif; ?>
                        <?php if ($course) : ?>
                            <span class="testimonial-meta-item">
                                <strong><?php echo nihongo_translate('course'); ?>:</strong> <?php echo esc_html($course); ?>
                            </span>
                        <?php endif; ?>
                        <?php if ($rating) : ?>
                            <div class="testimonial-rating">
                                <?php for ($i = 0; $i < $rating; $i++): ?>
                                    <span class="star">★</span>
                                <?php endfor; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </section>

        <!-- Testimonial Content Section -->
        <section class="section testimonial-content">
            <div class="container">
                <div class="testimonial-layout">
                    <div class="testimonial-main-content">
                        <div class="testimonial-text">
                            <?php the_content(); ?>
                        </div>

                        <div class="testimonial-cta">
                            <a href="<?php echo get_permalink(get_page_by_path('contact')); ?>" class="hero-cta">
                                <?php echo nihongo_translate('start_your_journey'); ?>
                            </a>
                        </div>
                    </div>

                    <div class="testimonial-sidebar">
                        <div class="testimonial-info-card">
                            <h3><?php echo nihongo_translate('student_information'); ?></h3>
                            
                            <?php if ($name) : ?>
                                <div class="info-item">
                                    <strong><?php echo nihongo_translate('name'); ?>:</strong>
                                    <span><?php echo esc_html($name); ?></span>
                                </div>
                            <?php endif; ?>
                            
                            <?php if ($country) : ?>
                                <div class="info-item">
                                    <strong><?php echo nihongo_translate('country'); ?>:</strong>
                                    <span><?php echo esc_html($country); ?></span>
                                </div>
                            <?php endif; ?>
                            
                            <?php if ($course) : ?>
                                <div class="info-item">
                                    <strong><?php echo nihongo_translate('course'); ?>:</strong>
                                    <span><?php echo esc_html($course); ?></span>
                                </div>
                            <?php endif; ?>

                            <div class="info-item">
                                <strong><?php echo nihongo_translate('date'); ?>:</strong>
                                <span><?php echo get_the_date(); ?></span>
                            </div>
                        </div>

                        <div class="testimonial-contact-card">
                            <h3><?php echo nihongo_translate('inspired_by_this_story'); ?></h3>
                            <p><?php echo nihongo_translate('start_your_own_journey'); ?></p>
                            <a href="<?php echo get_permalink(get_page_by_path('contact')); ?>" class="btn-secondary">
                                <?php echo nihongo_translate('get_started'); ?>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Related Testimonials Section -->
        <section class="section related-testimonials">
            <div class="container">
                <h2 class="section-title"><?php echo nihongo_translate('more_student_stories'); ?></h2>
                
                <?php
                $related_testimonials = get_posts(array(
                    'post_type' => 'testimonial',
                    'posts_per_page' => 3,
                    'post__not_in' => array(get_the_ID()),
                    'orderby' => 'rand'
                ));
                ?>
                
                <?php if ($related_testimonials) : ?>
                    <div class="testimonials-grid">
                        <?php foreach ($related_testimonials as $related_testimonial) : ?>
                            <div class="testimonial-card">
                                <div class="testimonial-content">
                                    <?php if (has_post_thumbnail($related_testimonial->ID)) : ?>
                                        <div class="testimonial-image">
                                            <a href="<?php echo get_permalink($related_testimonial->ID); ?>">
                                                <?php echo get_the_post_thumbnail($related_testimonial->ID, 'testimonial-thumbnail'); ?>
                                            </a>
                                        </div>
                                    <?php endif; ?>
                                    <div class="testimonial-header">
                                        <h3 class="testimonial-title">
                                            <a href="<?php echo get_permalink($related_testimonial->ID); ?>">
                                                <?php echo esc_html($related_testimonial->post_title); ?>
                                            </a>
                                        </h3>
                                        <div class="testimonial-rating">
                                            <?php 
                                            $related_rating = get_post_meta($related_testimonial->ID, '_testimonial_rating', true);
                                            for ($i = 0; $i < $related_rating; $i++): ?>
                                                <span class="star">★</span>
                                            <?php endfor; ?>
                                        </div>
                                    </div>
                                    <p class="testimonial-comment"><?php echo esc_html($related_testimonial->post_excerpt); ?></p>
                                    <div class="testimonial-author">
                                        <div class="testimonial-info">
                                            <h4><?php echo esc_html(get_post_meta($related_testimonial->ID, '_testimonial_name', true)); ?></h4>
                                            <p><?php echo esc_html(get_post_meta($related_testimonial->ID, '_testimonial_country', true)); ?> / <?php echo esc_html(get_post_meta($related_testimonial->ID, '_testimonial_course', true)); ?></p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </section>

    <?php endwhile; ?>
</main>

<?php get_footer(); ?>
