{% extends "blog/shell.html" %}

{#
================================================================================
Blog Home Template (Kida-Native)
================================================================================

Optimized home page template for blog sites. Extends blog/shell.html.

FEATURES:
- Simplified hero section
- Prominent recent posts grid
- Auto-discovered blog section
- Clean, reading-focused layout

KIDA FEATURES USED:
- {% let %} for template-scoped variables
- Optional chaining (?.) for null-safe access
- Null coalescing (??) for smart defaults
- Pipeline operator (|>) for filter chains
- {% def %} for reusable post card component
- {% cache %} for recent posts computation

USAGE:
Set `template: blog/home.html` in home page frontmatter
Or set `type: blog` on home page to auto-select
================================================================================
#}

{% from 'partials/components/post-card.html' import post_card %}

{# =============================================================================
   MAIN TEMPLATE
   ============================================================================= #}

{% block blog_content %}
{# Template-scoped configuration #}
{% let page_title = page?.title ?? config?.title ?? 'Blog' %}
{% let page_desc = params?.description ?? '' %}
{% let show_recent = params?.show_recent_posts ?? true %}
{% let blog_section_name = params?.blog_section ?? 'posts' %}

<div class="blog-home">
  {# Hero Section #}
  <header class="blog-home-hero">
    <div class="blog-home-hero-content">
      <h1 class="blog-home-title">{{ page_title }}</h1>
      {% if page_desc %}
      <p class="blog-home-subtitle">{{ page_desc }}</p>
      {% end %}
    </div>
  </header>

  {# Main Content (if any) #}
  {% if content and content.strip() %}
  <div class="blog-home-content prose">
    {{ content | safe }}
  </div>
  {% end %}

  {# Recent Posts Section - cached for performance #}
  {% if show_recent %}
  {# Find blog section and recent posts (before cache for key computation) #}
  {% let site_sections = site?.sections ?? [] %}
  {% let blog_section_candidates = site_sections |> selectattr('name', 'eq', blog_section_name) |> list %}
  {% let blog_section = blog_section_candidates[0] if blog_section_candidates else none %}

  {# Get recent posts from blog section or fallback to site-wide search #}
  {# regular_pages excludes index by default (standard expected behavior) #}
  {% let recent = [] %}
  {% if blog_section and blog_section?.pages %}
  {% let section_pages = blog_section.regular_pages ?? blog_section.pages ?? [] %}
  {% let recent = section_pages
      |> selectattr('date')
      |> sort(attribute='date', reverse=true)
      |> take(6) %}
  {% elif site?.pages %}
  {# Fallback: find blog posts by URL path or metadata type #}
  {% let blog_posts = [] %}
  {% for p in site.pages %}
  {% let p_type = p?.metadata?.type ?? '' %}
  {% let p_path = p?._path ?? '' %}
  {% if p_type == 'blog' or '/blog/' in p_path or '/posts/' in p_path %}
  {% set _ = blog_posts.append(p) %}
  {% end %}
  {% end %}
  {% let recent = blog_posts
      |> selectattr('date')
      |> sort(attribute='date', reverse=true)
      |> take(6) %}
  {% end %}

  {# Cache key includes content hashes so hot reload invalidates when posts change #}
  {% let recent_hashes = recent | map(attribute='content_hash') | join('-') if recent else '' %}
  {% cache 'blog-home-recent-' ~ (site.nav_version ?? '') ~ '-' ~ recent_hashes %}

  {% if recent %}
  <section class="blog-home-recent">
    <div class="blog-home-recent-header">
      <h2 class="blog-home-recent-title">Recent Posts</h2>
      {% if blog_section %}
      <a href="{{ blog_section?.href ?? blog_section?._path }}" class="blog-home-view-all">View all posts →</a>
      {% end %}
    </div>
    <div class="blog-home-posts-grid">
      {% for post in recent %}
      {{ post_card(post, variant='default') }}
      {% end %}
    </div>
  </section>
  {% end %}
  {% end %}
  {% end %}
</div>
{% end %}
