{#
  Renders one entry’s Page Builder matrix in sort order.
  Required: pagebuilderEntry — the Craft entry whose `pagebuilder` field to output (e.g. a Case Study when embedding).

  Always passes `entry: pagebuilderEntry` into components so treatment-style blocks resolve fields on the correct entry.

  embedNestingLevel: used only for `caseStudiesSingle` — prevents infinite nesting (max 1 level of embed).
#}
{% set pagebuilderEntry = pagebuilderEntry ?? null %}
{% set embedNestingLevel = embedNestingLevel|default(0) %}

{% if pagebuilderEntry and pagebuilderEntry.pagebuilder is defined and pagebuilderEntry.pagebuilder|length %}
    {% set blocks = pagebuilderEntry.pagebuilder.orderBy('sortOrder asc').all() %}
    {% for block in blocks %}
        {% set prevBlock = loop.index0 > 0 ? blocks[loop.index0 - 1] : null %}
        {% set nextBlock = not loop.last ? blocks[loop.index0 + 1] : null %}
        {% set prevHandle = prevBlock ? prevBlock.type.handle : null %}
        {% set nextHandle = nextBlock ? nextBlock.type.handle : null %}
        {% set prevIsWhatIs = prevHandle == 'whatIsTreatment' %}
        {% set nextIsVideo = nextHandle in ['videoSection', 'video'] %}
        {% set _bl = block.getFieldLayout() %}
        {% set _isVideoByFields = _bl
            and _bl.getFieldByHandle('heroYoutubeUrl') is not null
            and (
                _bl.getFieldByHandle('sectionHeading') is not null
                or _bl.getFieldByHandle('sectionDescription') is not null
                or _bl.getFieldByHandle('treatmentHeroBackground') is not null
            ) %}
        {% if _isVideoByFields %}
            {% include 'components/video-section.twig' with {
                block: block,
                entry: pagebuilderEntry,
                compactTop: prevIsWhatIs
            } %}
        {% else %}
        {% switch block.type.handle %}

            {% case 'heroSection' %}
                {% include 'components/hero.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'callToAction' %}
                {% include 'components/call-to-action.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'callToActionSection' %}
                {% include 'components/call-to-action-section.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'transformations' %}
            {% case 'featuredTransformations' %}
                {% include 'components/transformations.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'featuredCaseStudies' %}
                {% include 'components/featured-case-studies.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'caseStudiesSingle' %}
                {% if embedNestingLevel < 1 %}
                    {% include 'components/case-study-single-embed.twig' with { block: block, embedNestingLevel: embedNestingLevel + 1 } %}
                {% endif %}

            {% case 'treatmentsGrid' %}
                {% include 'components/treatments-grid.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'blogGrid' %}
                {% include 'components/blog-grid.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'howItWorks' %}
                {% include 'components/how-it-works.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'testimonialsGrid' %}
                {% include 'components/testimonials.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'featuredTestimonial' %}
                {% include 'components/featured-testimonial.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'videoSection' %}
            {% case 'video' %}
                {% include 'components/video-section.twig' with {
                    block: block,
                    entry: pagebuilderEntry,
                    compactTop: prevIsWhatIs
                } %}

            {% case 'flexibleSectionType' %}
                {% include 'components/flexible-section.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'aboutDoctor' %}
                {% include 'components/about-doctor.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'accreditations' %}
                {% include 'components/accreditations.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'instagramCta' %}
                {% include 'components/instagram-cta.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'locationContact' %}
                {% include 'components/location-contact.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'treatmentOverview' %}
                {% include 'components/treatment-overview.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'treatmentHero' %}
                {% include 'components/treatment-hero.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'whyChooseTreatment' %}
                {% include 'components/why-choose-treatment.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'faqSection' %}
            {% case 'faqList' %}
                {% include 'components/faq-section.twig' with { block: block, entry: pagebuilderEntry } %}

            {% case 'whatIsTreatment' %}
                {% include 'components/what-is-treatment.twig' with {
                    block: block,
                    entry: pagebuilderEntry,
                    compactBottom: nextIsVideo
                } %}

            {% case 'whoShouldConsider' %}
            {% case 'whoShouldConsiderSection' %}
                {% include 'components/who-should-consider.twig' with { block: block, entry: pagebuilderEntry } %}

            {% default %}
                {# Fallback: if this block has a heroYoutubeUrl field, treat it as a video section #}
                {% set _bl = block.getFieldLayout() %}
                {% if _bl and _bl.getFieldByHandle('heroYoutubeUrl') is not null %}
                    {% include 'components/video-section.twig' with {
                        block: block,
                        entry: pagebuilderEntry,
                        compactTop: prevIsWhatIs
                    } %}
                {% endif %}
        {% endswitch %}
        {% endif %}
    {% endfor %}
{% endif %}
