<?php
/**
 * Template part for displaying the Feature Story Section.
 * 
 * @package Blog_Over
 */

$enable_feature_story = get_theme_mod('blog_over_feature_story_enable', false);

if (!$enable_feature_story) {
    return;
}

// Get Settings
$category = get_theme_mod('blog_over_feature_story_category', '');
$elements = get_theme_mod('blog_over_feature_story_elements', array('category', 'title', 'meta', 'excerpt', 'button'));
$layout = get_theme_mod('blog_over_feature_story_layout', 'overlay');

// Query the single post
$args = array(
    'post_type' => 'post',
    'posts_per_page' => 1,
    'ignore_sticky_posts' => true,
);

if (!empty($category)) {
    if (is_numeric($category)) {
        $args['cat'] = intval($category);
    } else {
        $args['category_name'] = sanitize_title($category);
    }
}

// Pro Feature: Allow filtering by exact Post ID if pro is active
if (function_exists('blog_over_is_pro_active') && blog_over_is_pro_active()) {
    $exact_post_id = get_theme_mod('blog_over_feature_story_post_id', '');
    if (!empty($exact_post_id)) {
        $args['p'] = intval($exact_post_id);
    }
}

$feature_query = new WP_Query($args);

if ($feature_query->have_posts()):
    $feature_query->the_post();

    // Get background image / video based on media type
    $media_type = 'post_image'; // default for free
    $bg_url = '';
    $bg_style = '';
    $media_class = 'has-post-thumbnail';
    $placeholder_html = '';
    $video_bg_html = '';

    // Pro Feature: Media Type selection
    if (function_exists('blog_over_is_pro_active') && blog_over_is_pro_active()) {
        $media_type = get_theme_mod('blog_over_feature_story_media_type', 'post_image');
    }

    if ($media_type === 'custom_image') {
        // Pro: Custom Image upload
        $custom_img = get_theme_mod('blog_over_feature_story_custom_image', '');
        if (!empty($custom_img)) {
            $bg_url = $custom_img;
            $bg_style = 'background-image: url(' . esc_url($bg_url) . ');';
        } else {
            $media_class = 'no-post-thumbnail';
            $placeholder_html = '<div class="blog-over-placeholder-icon"><i class="far fa-image"></i></div>';
        }
    } elseif ($media_type === 'video') {
        // Pro: Video URL (YouTube, Vimeo, or self-hosted .mp4)
        $video_url = get_theme_mod('blog_over_feature_story_video', '');
        if (!empty($video_url)) {
            $embed_url = blog_over_parse_video_embed_url($video_url);
            if ($embed_url) {
                // YouTube or Vimeo — render as responsive iframe
                $video_bg_html = '<div class="blog-over-feature-video-embed">'
                    . '<iframe src="' . esc_url($embed_url) . '" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>'
                    . '</div>';
                $media_class .= ' has-video-embed';
            } else {
                // Self-hosted .mp4 — render as background video
                $video_bg_html = '<video autoplay loop muted playsinline class="blog-over-feature-video"><source src="' . esc_url($video_url) . '" type="video/mp4"></video>';
                $media_class .= ' has-video-background';
            }
            // Removed fallback poster image as per user request to only show video
            $bg_style = '';
        } else {
            $media_class = 'no-post-thumbnail';
            $placeholder_html = '<div class="blog-over-placeholder-icon"><i class="fas fa-video"></i></div>';
        }
    } else {
        // Default: Post Featured Image
        $bg_url = get_the_post_thumbnail_url(get_the_ID(), 'full');
        if ($bg_url) {
            $bg_style = 'background-image: url(' . esc_url($bg_url) . ');';
        } else {
            $media_class = 'no-post-thumbnail';
            $placeholder_html = '<div class="blog-over-placeholder-icon"><i class="far fa-image"></i></div>';
        }
    }

    $section_title = get_theme_mod('blog_over_feature_story_section_title', '');

    // Content Box Alignment (Pro feature or default right)
    $box_align = get_theme_mod('blog_over_feature_story_box_align', 'right');
    $align_class = 'box-align-' . esc_attr($box_align);

    // Render Layouts
    ?>
    <section class="blog-over-feature-story-wrapper layout-<?php echo esc_attr($layout); ?> <?php echo esc_attr($align_class); ?>">
        
        <?php if (!empty($section_title)) : ?>
            <div class="blog-over-container">
                <div class="blog-over-section-heading-wrap">
                    <h2 class="blog-over-section-heading blog-over-feature-story-section-title"><?php echo esc_html($section_title); ?></h2>
                </div>
            </div>
        <?php endif; ?>

        <?php if ($layout === 'split') : ?>
            <div class="blog-over-split-inner">
                <div class="blog-over-feature-media <?php echo esc_attr($media_class); ?>" style="<?php echo esc_attr($bg_style); ?>" role="img" aria-label="<?php echo esc_attr(get_the_title()); ?>">
                    <?php echo $placeholder_html; // phpcs:ignore ?>
                    <?php echo $video_bg_html; // phpcs:ignore ?>
                    <div class="blog-over-feature-overlay"></div>
                </div>
                <?php if (!empty($elements) && is_array($elements)) : ?>
                    <div class="blog-over-feature-content-wrap">
                        <div class="blog-over-feature-content">
                            <?php foreach ($elements as $el) : ?>
                                <?php blog_over_render_feature_story_element($el); ?>
                            <?php endforeach; ?>
                        </div>
                    </div>
                <?php endif; ?>
            </div>

        <?php elseif ($layout === 'overlap') : ?>
            <div class="blog-over-container">
                <div class="blog-over-overlap-inner">
                    <div class="blog-over-feature-media <?php echo esc_attr($media_class); ?>" style="<?php echo esc_attr($bg_style); ?>" role="img" aria-label="<?php echo esc_attr(get_the_title()); ?>">
                        <?php echo $placeholder_html; // phpcs:ignore ?>
                        <?php echo $video_bg_html; // phpcs:ignore ?>
                        <div class="blog-over-feature-overlay"></div>
                    </div>
                    <?php if (!empty($elements) && is_array($elements)) : ?>
                        <div class="blog-over-feature-content-wrap">
                            <div class="blog-over-feature-content">
                                <?php foreach ($elements as $el) : ?>
                                    <?php blog_over_render_feature_story_element($el); ?>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>

        <?php else : // overlay (default) ?>
            <div class="blog-over-overlay-inner <?php echo esc_attr($media_class); ?>" style="<?php echo esc_attr($bg_style); ?>" role="img" aria-label="<?php echo esc_attr(get_the_title()); ?>">
                <?php echo $placeholder_html; // phpcs:ignore ?>
                <?php echo $video_bg_html; // phpcs:ignore ?>
                <div class="blog-over-feature-overlay"></div>
                <?php if (!empty($elements) && is_array($elements)) : ?>
                    <div class="blog-over-container">
                        <div class="blog-over-feature-content-wrap">
                            <div class="blog-over-feature-content">
                                <?php foreach ($elements as $el) : ?>
                                    <?php blog_over_render_feature_story_element($el); ?>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </section>
    <?php
    wp_reset_postdata();
endif;
