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

get_header(); ?>

<main id="main" class="site-main">
    <?php while (have_posts()) : the_post(); ?>
        <?php
        $level = get_post_meta(get_the_ID(), '_course_level', true);
        $duration = get_post_meta(get_the_ID(), '_course_duration', true);
        $price = get_post_meta(get_the_ID(), '_course_price', true);
        $features = explode("\n", get_post_meta(get_the_ID(), '_course_features', true));
        ?>

        <!-- Course Hero Section -->
        <section class="course-hero">
            <div class="course-hero-background">
                <?php if (has_post_thumbnail()) : ?>
                    <?php the_post_thumbnail('hero-image', array('class' => 'course-hero-image')); ?>
                <?php else : ?>
                    <img src="<?php echo get_template_directory_uri(); ?>/assets/img/cc1.jpg" alt="<?php the_title(); ?>" class="course-hero-image">
                <?php endif; ?>
                <div class="course-hero-overlay"></div>
            </div>
            <div class="container">
                <div class="course-hero-content">
                    <h1 class="course-hero-title"><?php the_title(); ?></h1>
                    <p class="course-hero-subtitle"><?php echo get_the_excerpt(); ?></p>
                    <div class="course-hero-meta">
                        <?php if ($level) : ?>
                            <span class="course-meta-item">
                                <strong><?php echo nihongo_translate('level'); ?>:</strong> <?php echo esc_html($level); ?>
                            </span>
                        <?php endif; ?>
                        <?php if ($duration) : ?>
                            <span class="course-meta-item">
                                <strong><?php echo nihongo_translate('duration'); ?>:</strong> <?php echo esc_html($duration); ?>
                            </span>
                        <?php endif; ?>
                        <?php if ($price) : ?>
                            <span class="course-meta-item">
                                <strong><?php echo nihongo_translate('price'); ?>:</strong> <?php echo esc_html($price); ?>
                            </span>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </section>

        <!-- Course Content Section -->
        <section class="section course-content">
            <div class="container">
                <div class="course-layout">
                    <div class="course-main-content">
                        <div class="course-description">
                            <h2><?php echo nihongo_translate('course_description'); ?></h2>
                            <?php the_content(); ?>
                        </div>

                        <?php if (!empty($features)) : ?>
                            <div class="course-features">
                                <h2><?php echo nihongo_translate('course_features'); ?></h2>
                                <ul class="features-list">
                                    <?php foreach ($features as $feature) : ?>
                                        <?php if (trim($feature)) : ?>
                                            <li><?php echo esc_html(trim($feature)); ?></li>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </ul>
                            </div>
                        <?php endif; ?>

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

                    <div class="course-sidebar">
                        <div class="course-info-card">
                            <h3><?php echo nihongo_translate('course_information'); ?></h3>
                            
                            <?php if ($level) : ?>
                                <div class="info-item">
                                    <strong><?php echo nihongo_translate('level'); ?>:</strong>
                                    <span><?php echo esc_html($level); ?></span>
                                </div>
                            <?php endif; ?>
                            
                            <?php if ($duration) : ?>
                                <div class="info-item">
                                    <strong><?php echo nihongo_translate('duration'); ?>:</strong>
                                    <span><?php echo esc_html($duration); ?></span>
                                </div>
                            <?php endif; ?>
                            
                            <?php if ($price) : ?>
                                <div class="info-item">
                                    <strong><?php echo nihongo_translate('price'); ?>:</strong>
                                    <span><?php echo esc_html($price); ?></span>
                                </div>
                            <?php endif; ?>

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

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

                        <div class="course-contact-card">
                            <h3><?php echo nihongo_translate('have_questions'); ?></h3>
                            <p><?php echo nihongo_translate('contact_us_for_more_info'); ?></p>
                            <a href="<?php echo get_permalink(get_page_by_path('contact')); ?>" class="btn-secondary">
                                <?php echo nihongo_translate('contact_us'); ?>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Related Courses Section -->
        <section class="section related-courses">
            <div class="container">
                <h2 class="section-title"><?php echo nihongo_translate('related_courses'); ?></h2>
                
                <?php
                $related_courses = get_posts(array(
                    'post_type' => 'course',
                    'posts_per_page' => 3,
                    'post__not_in' => array(get_the_ID()),
                    'orderby' => 'rand'
                ));
                ?>
                
                <?php if ($related_courses) : ?>
                    <div class="courses-grid">
                        <?php foreach ($related_courses as $related_course) : ?>
                            <div class="course-card">
                                <div class="course-image">
                                    <?php if (has_post_thumbnail($related_course->ID)) : ?>
                                        <a href="<?php echo get_permalink($related_course->ID); ?>">
                                            <?php echo get_the_post_thumbnail($related_course->ID, 'course-thumbnail'); ?>
                                        </a>
                                    <?php else : ?>
                                        <a href="<?php echo get_permalink($related_course->ID); ?>">
                                            <img src="<?php echo get_template_directory_uri(); ?>/assets/img/cc1.jpg" alt="<?php echo esc_attr($related_course->post_title); ?>">
                                        </a>
                                    <?php endif; ?>
                                </div>
                                <div class="course-content">
                                    <h3 class="course-title">
                                        <a href="<?php echo get_permalink($related_course->ID); ?>">
                                            <?php echo esc_html($related_course->post_title); ?>
                                        </a>
                                    </h3>
                                    <p class="course-description"><?php echo esc_html($related_course->post_excerpt); ?></p>
                                    <div class="course-meta">
                                        <span class="course-level"><?php echo esc_html(get_post_meta($related_course->ID, '_course_level', true)); ?></span>
                                        <span class="course-duration"><?php echo esc_html(get_post_meta($related_course->ID, '_course_duration', true)); ?></span>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </section>

    <?php endwhile; ?>
</main>

<?php get_footer(); ?>
