{% extends "base.html" %}

{#
Generic Section Index Template

Used for non-chronological section landing pages.
Falls back when no specialized template exists.

Context variables:
- section: Section object (for auto-generated pages)
- page: Page object (for explicit _index.md files)
- posts: Child pages (_posts)
- subsections: Child sections (_subsections)

Frontmatter options (discover: structure):
discover:
children:
title: "Custom Title" # or false to hide
variant: "cards" # compact | cards | minimal
children: false # disable children entirely
related:
title: "Custom Title"
limit: 5
related: false # disable related entirely
#}

{% from 'partials/navigation-components.html' import breadcrumbs %}
{% from 'partials/components/tiles.html' import content_tiles %}

{% block content %}
{{ breadcrumbs(page if page else section) }}

<div class="section-index">
    <header class="section-header">
        <h1>{{ page.title if page else section.title if section else site.title }}</h1>

        {% if page and params.description %}
        <p class="section-description">{{ params.description }}</p>
        {% elif section and section.params.description %}
        <p class="section-description">{{ section.params.description }}</p>
        {% end %}
    </header>

    {# User-provided content (from _index.md) #}
    {% if content %}
    <div class="section-content prose">
        {{ content | safe }}
    </div>
    {% end %}

    {# Content tiles with smart defaults - use discover: frontmatter to customize #}
    {% set discover = params.discover if page else {} %}
    {% set children_config = discover.children if discover else {} %}
    {% set related_config = discover.related if discover else {} %}

    {% set children_disabled = children_config is sameas false %}
    {% set related_disabled = related_config is sameas false %}

    {% set show_children = not children_disabled %}
    {% set show_related = not related_disabled %}

    {% set children_title = children_config.title ?? 'In This Section' if children_config is mapping else 'In This Section' %}
    {% set children_variant = children_config.variant ?? 'compact' if children_config is mapping else 'compact' %}
    {% set related_title = related_config.title ?? 'Related Pages' if related_config is mapping else 'Related Pages' %}
    {% set related_limit = (related_config.limit ?? 5) | int if related_config is mapping else (related_config | int if related_config is number else 5) %}

    {% set children_title = none if children_title is sameas false else children_title %}
    {% set related_title = none if related_title is sameas false else related_title %}

    {% set has_children = posts or subsections %}
    {% set has_related = show_related and page and page.related_posts %}

    {% if show_children and (has_children or has_related) %}
    {{ content_tiles(
    title=children_title if has_children else none,
    children=posts if show_children else none,
    subsections=subsections if show_children else none,
    related=page.related_posts[:related_limit] if has_related else none,
    related_title=related_title,
    variant=children_variant,
    group_by='type' if has_children and has_related else none,
    page=page
    ) }}
    {% end %}

    {# Empty state if no children to display #}
    {% if not (posts or subsections) %}
    {# Empty state with helpful guidance #}
    <div class="section-empty">
        <div class="empty-state">
            <p class="empty-message">This section doesn't have any content yet.</p>
            {% if section %}
            <div class="empty-help">
                <p><strong>To add content:</strong></p>
                <ul>
                    <li>Create a page: <code>content/{{ section.path.relative_to(site.content_dir) }}/my-page.md</code>
                    </li>
                    <li>Add a custom index:
                        <code>content/{{ section.path.relative_to(site.content_dir) }}/_index.md</code>
                    </li>
                    <li>Create a subsection:
                        <code>content/{{ section.path.relative_to(site.content_dir) }}/subsection/</code>
                    </li>
                </ul>
            </div>
            {% end %}
        </div>
    </div>
    {% end %}
</div>
{% end %}
