<?php get_header(); ?>

<main class="main-content">
    <div class="container">
        <div class="content-sidebar-layout">
            
            <!-- Main Content Area -->
            <div class="main-content-area">
                <?php while (have_posts()) : the_post(); 
                $template = get_theme_mod('pulsegrid_single_template', 'simple');
                
                if ($template === 'featured') : 
                    // Featured Template
                    $backdrop_url = get_post_meta(get_the_ID(), '_tmdb_backdrop', true);
                    $poster_url = has_post_thumbnail() ? get_the_post_thumbnail_url(get_the_ID(), 'large') : '';
                    $quality = get_post_meta(get_the_ID(), '_tmdb_calidad', true);
                    
                    // If quality is custom, get the custom value
                    if ($quality === 'custom') {
                        $custom_quality = get_post_meta(get_the_ID(), '_tmdb_calidad_custom', true);
                        if (!empty($custom_quality)) {
                            $quality = $custom_quality;
                        }
                    }
                    
                    $rating = get_post_meta(get_the_ID(), '_tmdb_rating', true);
                    $idioma = get_post_meta(get_the_ID(), '_tmdb_idioma', true);
                    $trailer_url = get_post_meta(get_the_ID(), '_tmdb_trailer', true);
                    $synopsis = get_post_meta(get_the_ID(), '_tmdb_synopsis', true);
                    $tipo = get_post_meta(get_the_ID(), '_tmdb_tipo', true);
                    
                    // Use backdrop if available, otherwise use poster
                    if (empty($backdrop_url) && !empty($poster_url)) {
                        $backdrop_url = $poster_url;
                    }
                ?>
                    <!-- Featured Template -->
                    <article class="single-post-featured">
                        <!-- Hero Section with Backdrop -->
                        <div class="featured-hero" style="background-image: url(<?php echo esc_url($backdrop_url); ?>)">
                            <div class="featured-hero-overlay">
                                <div class="featured-hero-content">
                                    <div class="featured-left-section">
                                        <div class="featured-poster-container">
                                            <?php if ($poster_url) : ?>
                                                <img src="<?php echo esc_url($poster_url); ?>" alt="<?php the_title_attribute(); ?>" class="featured-poster">
                                            <?php endif; ?>
                                            
                                            <?php if ($trailer_url) : ?>
                                                <button type="button" class="featured-trailer-btn featured-trailer-btn-mobile" data-trailer-url="<?php echo esc_url($trailer_url); ?>">
                                                    <svg class="i i-play" viewBox="0 0 24 24">
                                                        <path d="m7 3 14 9-14 9z"></path>
                                                    </svg>
                                                    Mirar Trailer
                                                </button>
                                            <?php endif; ?>
                                        </div>
                                        
                                        <div class="featured-info">
                                            <h1 class="featured-title"><?php the_title(); ?></h1>
                                            
                                            <div class="featured-metadata">
                                                <?php if ($rating) : ?>
                                                    <span class="featured-rating-badge">⭐ <?php echo esc_html($rating); ?></span>
                                                <?php endif; ?>
                                                
                                                <?php if ($quality) : ?>
                                                    <span class="featured-quality-badge"><?php echo esc_html($quality); ?></span>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="featured-right-section">
                                        <?php if ($trailer_url) : ?>
                                            <button type="button" class="featured-trailer-btn featured-trailer-btn-desktop" data-trailer-url="<?php echo esc_url($trailer_url); ?>">
                                                <svg class="i i-play" viewBox="0 0 24 24">
                                                    <path d="m7 3 14 9-14 9z"></path>
                                                </svg>
                                                Mirar Trailer
                                            </button>
                                        <?php endif; ?>
                                        
                                        <?php 
                                        $idiomas_array = is_array($idioma) ? $idioma : array();
                                        if (!empty($idiomas_array)) : 
                                        ?>
                                            <div class="featured-languages-box">
                                                <div class="languages-label">Idioma:</div>
                                                <div class="languages-list">
                                                    <?php 
                                                    $lang_map = array(
                                                        'Español' => array('abbr' => 'CAS', 'flag' => 'español.svg'),
                                                        'Latino' => array('abbr' => 'LAT', 'flag' => 'latino.svg'),
                                                        'Japonés' => array('abbr' => 'JAP', 'flag' => 'japones.webp')
                                                    );
                                                    
                                                    foreach ($idiomas_array as $lang) {
                                                        if (isset($lang_map[$lang])) {
                                                            $flag_url = get_template_directory_uri() . '/flag/' . $lang_map[$lang]['flag'];
                                                            echo '<div class="language-badge">';
                                                            echo '<img src="' . esc_url($flag_url) . '" alt="' . esc_attr($lang) . '" class="lang-flag">';
                                                            echo '<span class="lang-code">' . esc_html($lang_map[$lang]['abbr']) . '</span>';
                                                            echo '</div>';
                                                        }
                                                    }
                                                    ?>
                                                </div>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <!-- Synopsis Section -->
                        <?php if ($synopsis) : ?>
                            <div class="featured-synopsis-section">
                                <div class="synopsis-text">
                                    <?php echo esc_html($synopsis); ?>
                                </div>
                            </div>
                        <?php endif; ?>
                        
                        <!-- Main Content -->
                        <div class="featured-content">
                            <?php the_content(); ?>
                        </div>
                        
                        <!-- Post Content Ad -->
                        <?php 
                        $post_content_ad = get_option('pulsegrid_post_content_ad', '');
                        if (!empty($post_content_ad)) : 
                        ?>
                        <div class="post-content-ad-container">
                            <?php echo $post_content_ad; ?>
                        </div>
                        <?php endif; ?>
                        
                        <!-- Trailer Modal -->
                        <?php if ($trailer_url) : ?>
                            <div id="trailer-modal" class="trailer-modal">
                                <div class="trailer-modal-overlay"></div>
                                <div class="trailer-modal-content">
                                    <button class="trailer-modal-close">&times;</button>
                                    <div class="trailer-video-container">
                                        <iframe id="trailer-iframe" src="" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
                                    </div>
                                </div>
                            </div>
                        <?php endif; ?>
                    </article>
                <?php else : ?>
                    <!-- Simple Template (Original) -->
                    <article class="single-post-content">
                        <header class="post-header-centered">
                            <h1 class="post-title-centered"><?php the_title(); ?></h1>
                        </header>
                        
                        <?php if (has_post_thumbnail()) : ?>
                            <div class="post-featured-image-centered">
                                <?php the_post_thumbnail('medium'); ?>
                            </div>
                            <div class="post-separator"></div>
                        <?php endif; ?>
                        
                        <div class="post-content-centered">
                            <?php 
                            // Display synopsis if available
                            $synopsis = get_post_meta(get_the_ID(), '_tmdb_synopsis', true);
                            if (!empty($synopsis)) {
                                echo '<div class="post-synopsis">';
                                echo wpautop($synopsis);
                                echo '</div>';
                            }
                            
                            // Display post content
                            the_content(); 
                            ?>
                        </div>
                        
                        <!-- Post Content Ad -->
                        <?php 
                        $post_content_ad = get_option('pulsegrid_post_content_ad', '');
                        if (!empty($post_content_ad)) : 
                        ?>
                        <div class="post-content-ad-container">
                            <?php echo $post_content_ad; ?>
                        </div>
                        <?php endif; ?>
                    </article>
                <?php endif; // End template check ?>
                    
                    <!-- Post Tags/Categories -->
                    <?php 
                    $show_tags = get_theme_mod('pulsegrid_show_tags', '1');
                    $tags_type = get_theme_mod('pulsegrid_tags_type', 'tags');
                    
                    if ($show_tags == '1') {
                        if ($tags_type === 'categories' && has_category()) {
                            // Show categories
                            ?>
                            <div class="post-tags">
                                <h3><?php echo esc_html(get_theme_mod('pulsegrid_tags_title', 'Categorías')); ?></h3>
                                <div class="tag-list">
                                    <?php
                                    $categories = get_the_category();
                                    if ($categories) {
                                        foreach ($categories as $category) {
                                            echo '<a href="' . get_category_link($category->term_id) . '" class="tag-item">' . $category->name . '</a>';
                                        }
                                    }
                                    ?>
                                </div>
                            </div>
                            <?php
                        } elseif ($tags_type === 'tags' && has_tag()) {
                            // Show tags
                            ?>
                            <div class="post-tags">
                                <h3><?php echo esc_html(get_theme_mod('pulsegrid_tags_title', 'Etiquetas')); ?></h3>
                                <div class="tag-list">
                                    <?php
                                    $tags = get_the_tags();
                                    if ($tags) {
                                        foreach ($tags as $tag) {
                                            echo '<a href="' . get_tag_link($tag->term_id) . '" class="tag-item">' . $tag->name . '</a>';
                                        }
                                    }
                                    ?>
                                </div>
                            </div>
                            <?php
                        }
                    }
                    ?>
                    
                    <!-- Related Posts -->
                    <?php
                    if (get_theme_mod('pulsegrid_show_related', '1') == '1') :
                        $related_posts = pulsegrid_get_related_posts(get_the_ID(), 5);
                        if ($related_posts && $related_posts->have_posts()) :
                    ?>
                        <div class="related-posts">
                            <h3><?php echo esc_html(get_theme_mod('pulsegrid_related_title', 'Podría Interesarte')); ?></h3>
                            <div class="related-posts-grid">
                                <?php while ($related_posts->have_posts()) : $related_posts->the_post(); ?>
                                    <article class="post-item">
                                        <a href="<?php the_permalink(); ?>">
                                            <?php pulsegrid_post_thumbnail_with_badges('medium'); ?>
                                        </a>
                                        <div class="post-content">
                                            <h3 class="post-title">
                                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                            </h3>
                                        </div>
                                    </article>
                                <?php endwhile; ?>
                            </div>
                        </div>
                        <?php 
                        wp_reset_postdata();
                        endif;
                    endif; ?>
                    
                    <!-- Post Comments Ad -->
                    <?php 
                    $post_comments_ad = get_option('pulsegrid_post_comments_ad', '');
                    if (!empty($post_comments_ad)) : 
                    ?>
                    <div class="post-comments-ad-container">
                        <?php echo $post_comments_ad; ?>
                    </div>
                    <?php endif; ?>
                    
                    <!-- Comments Section -->
                    <?php
                    if (get_theme_mod('pulsegrid_show_comments', '1') == '1' && (comments_open() || get_comments_number())) :
                    ?>
                        <div class="comments-section">
                            <?php comments_template(); ?>
                        </div>
                    <?php endif; ?>
                    
                <?php endwhile; ?>
            </div>
            
            <!-- Sidebar -->
            <aside class="sidebar">
                <?php get_sidebar(); ?>
            </aside>
            
        </div>
    </div>
</main>

<?php get_footer(); ?>
