<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Provisioner v2 - Changelog</title>
    <link rel="stylesheet" href="/crm/assets/fonts/lato/lato.css" />
    <link rel="stylesheet" href="/crm/assets/fonts/ui-sans/ui-sans.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />
    <link rel="stylesheet" href="public/css/changelog.css?v={{ "now"|date("U") }}" />
    {# Use CSS custom property for scrollbar width #}
    <style>
      :root {
        --scrollbar-width: 17px; /* Default Windows scrollbar width */
      }
      
      .content--inner {
        /* Always account for potential scrollbar */
        padding-right: calc(32px - var(--scrollbar-width)) !important;
        /* Reserve scrollbar space to prevent content shift */
        scrollbar-gutter: stable;
        /* When scrollbar appears, content stays in same position */
        box-sizing: border-box;
      }
      
      /* Adjust for different platforms */
      @supports (-webkit-appearance: none) {
        :root {
          --scrollbar-width: 15px; /* macOS scrollbar width */
        }
      }
    </style>
    {# Alias the parent window's jQuery #}
    <script>const $ = parent.$;</script>
  </head>
  <body>
    <!-- Header -->
    <div class="page-header__top">
      <h1 class="title">Changelog</h1>
      
      <select class="nav-select" id="navSelect">
        {% set dropdown_changelog = all_changelog|default(changelog) %}
        {% if search_query %}
          {# In search mode, show placeholder and don't select any version #}
          <option value="" disabled selected>Select a version...</option>
        {% endif %}
        {% if dropdown_changelog.unreleased.changesets is not empty %}
        <option value="unreleased"{% if not search_query and (current_version == 'unreleased' or current_version is empty) %} selected{% endif %}>Unreleased</option>
        {% endif %}
        {% for version, release in dropdown_changelog.releases %}
        <option value="{{ version }}"{% if not search_query and current_version == version %} selected{% endif %}>{{ version }}</option>
        {% endfor %}
      </select>
      
      <form class="search-form">
        <input
          type="text"
          class="search-input"
          id="searchInput"
          name="q"
          value="{{ search_query|default('') }}"
          placeholder="Search commits... (press Enter)"
        />
      </form>
      
      <!-- Statistics -->
      {% set total_releases = changelog.releases|length %}
      {% set unreleased_commits = 0 %}
      {% for changeset in changelog.unreleased.changesets %}
        {% set unreleased_commits = unreleased_commits + changeset.commits|length %}
      {% endfor %}
      {% set latest_release = changelog.releases|first %}
      
      {% if search_query %}
      {# Search results stats #}
      {% set total_search_results = 0 %}
      {% for changeset in changelog.unreleased.changesets %}
        {% set total_search_results = total_search_results + changeset.commits|length %}
      {% endfor %}
      {% for ver, release in changelog.releases %}
        {% for changeset in release.changesets %}
          {% set total_search_results = total_search_results + changeset.commits|length %}
        {% endfor %}
      {% endfor %}
      {% if total_search_results > 0 %}
      <div class="header-stats">
        <div class="stat-item">
          <span class="stat-number">{{ total_search_results }}</span>
          <span class="stat-label">Result{{ total_search_results != 1 ? 's' : '' }}</span>
        </div>
      </div>
      {% endif %}
      {% else %}
      {# Normal version stats #}
      {% if total_releases > 0 or unreleased_commits > 0 %}
      <div class="header-stats">
        <div class="stat-item">
          <span class="stat-number">{{ total_releases }}</span>
          <span class="stat-label">Release{{ total_releases != 1 ? 's' : '' }}</span>
        </div>
        {% if unreleased_commits > 0 %}
        <div class="stat-item">
          <span class="stat-number">{{ unreleased_commits }}</span>
          <span class="stat-label">Unreleased</span>
        </div>
        {% endif %}
      </div>
      {% endif %}
      {% endif %}
    </div>
    
    <!-- UISP-style content wrapper -->
    <div class="content--inner">
      <main>

      <!-- Empty State -->
      {% if changelog.releases is empty and changelog.unreleased.changesets is empty %}
      <div class="empty-state">
        <div class="empty-icon">🚧</div>
        <h2>No changelog entries yet</h2>
        <p>Commits will appear here automatically as development progresses.</p>
        <p class="empty-hint">The first commit will initialize this changelog with release history.</p>
      </div>
      {% else %}

      <!-- Search Results View -->
      {% if search_query %}
        <section id="search-results">
          <div class="section-header">
            <div class="section-title">
              <h2>Search Results for "{{ search_query }}"</h2>
            </div>
          </div>
          <div class="section-content">
            {% set total_results = 0 %}
            
            <!-- Collect all matching commits -->
            <div class="search-results-list">
              <!-- Unreleased commits -->
              {% for changeset in changelog.unreleased.changesets %}
                {% for commit in changeset.commits %}
                  {% set total_results = total_results + 1 %}
                  <div class="search-result-item">
                    <div class="result-header">
                      <a href="{{ config.repo_base_url }}/commit/{{ commit.full_hash|default(commit.hash) }}" class="commit-hash" target="_blank">
                        {{ commit.hash|slice(0, 7) }}
                      </a>
                      <span class="result-version">Unreleased</span>
                      <span class="result-date" data-timestamp="{{ commit.timestamp }}">{{ commit.timestamp }}</span>
                    </div>
                    <div class="result-message">
                      <pre>{{ commit.message }}</pre>
                    </div>
                    <div class="result-author">by {{ commit.author }}</div>
                  </div>
                {% endfor %}
              {% endfor %}
              
              <!-- Released commits -->
              {% for ver, release in changelog.releases %}
                {% for changeset in release.changesets %}
                  {% for commit in changeset.commits %}
                    {% set total_results = total_results + 1 %}
                    <div class="search-result-item">
                      <div class="result-header">
                        <a href="{{ config.repo_base_url }}/commit/{{ commit.full_hash|default(commit.hash) }}" class="commit-hash" target="_blank">
                          {{ commit.hash|slice(0, 7) }}
                        </a>
                        <span class="result-version">{{ ver }}</span>
                        <span class="result-date" data-timestamp="{{ commit.timestamp }}">{{ commit.timestamp }}</span>
                      </div>
                      <div class="result-message">
                      <pre>{{ commit.message }}</pre>
                    </div>
                      <div class="result-author">by {{ commit.author }}</div>
                    </div>
                  {% endfor %}
                {% endfor %}
              {% endfor %}
            </div>
            
            {% if total_results == 0 %}
              <div class="no-results">
                <p>No commits found matching "{{ search_query }}"</p>
              </div>
            {% endif %}
          </div>
        </section>
        
      <!-- Normal Version View -->
      {% else %}
        {% if current_version == 'unreleased' or current_version is empty %}
          {% if changelog.unreleased.changesets is not empty %}
          <section id="unreleased">
            <div class="section-header">
              <div class="section-title">
                <h2>Unreleased</h2>
              </div>
              <span class="release-date">Work in Progress</span>
            </div>
            <div class="section-content">
              <!-- Unified Grid for All Categories -->
              <div class="changelog-grid-wrapper">
                <div class="changelog-grid">
                  <!-- Grid Header -->
                  <div class="grid-header grid-category">Type</div>
                  <div class="grid-header grid-commit">Commit</div>
                  <div class="grid-header grid-message">Message</div>
                  <div class="grid-header grid-date">Timestamp</div>
                  <div class="grid-header grid-author">Author</div>
                  <div class="grid-header grid-files">Files</div>
                  
                  <!-- Grid Rows -->
                  {% set all_commits = [] %}
                  {% for changeset in changelog.unreleased.changesets %}
                    {% for commit in changeset.commits %}
                      {% set all_commits = all_commits|merge([{
                        'category': changeset.category,
                        'title': changeset.title,
                        'hash': commit.hash,
                        'full_hash': commit.full_hash|default(commit.hash),
                        'subject': commit.subject|default(commit.message|split('\n')|first),
                        'message': commit.message,
                        'date': commit.date,
                        'timestamp': commit.timestamp,
                        'author': commit.author,
                        'github_username': commit.github_username,
                        'files': commit.files,
                        'files_count': commit.files_count|default(commit.files|length),
                        'sort_date': commit.timestamp,
                        'timestamp': commit.timestamp
                      }]) %}
                    {% endfor %}
                  {% endfor %}
                  
                  {% set sorted_commits = all_commits|sort((a, b) => b.sort_date <=> a.sort_date) %}
                  
                  {# Pre-process commits to identify duplicate groups #}
                  {% set processed_commits = [] %}
                  {% set commit_groups = {} %}
                  {% for commit in sorted_commits %}
                    {% set subject = commit.subject %}
                    {% if commit_groups[subject] is not defined %}
                      {% set commit_groups = commit_groups|merge({(subject): []}) %}
                    {% endif %}
                    {% set commit_groups = commit_groups|merge({(subject): commit_groups[subject]|merge([commit])}) %}
                  {% endfor %}
                  
                  {# Build processed commits with duplicate information #}
                  {% for subject, group in commit_groups %}
                    {% for i, commit in group %}
                      {% set is_first_duplicate = (group|length > 1 and i == 0) %}
                      {% set is_hidden_duplicate = (group|length > 1 and i > 0) %}
                      {% set duplicate_count = group|length - 1 %}
                      {% set processed_commits = processed_commits|merge([commit|merge({
                        'is_first_duplicate': is_first_duplicate,
                        'is_hidden_duplicate': is_hidden_duplicate,
                        'duplicate_count': duplicate_count
                      })]) %}
                    {% endfor %}
                  {% endfor %}
                  
                  {% for commit in processed_commits %}
                  {% set group_id = commit.subject|replace({' ': '-', ':': '', '!': '', '?': '', '.': '', '(': '', ')': '', '[': '', ']': '', '/': '-', '\\': '-'})|lower %}
                  <!-- Category -->
                  <div class="grid-cell grid-category{% if commit.is_hidden_duplicate %} duplicate-hidden{% endif %}" data-category="{{ commit.category }}"{% if commit.is_hidden_duplicate %} data-duplicate-group="{{ group_id }}"{% endif %}>
                    <i class="fas category-icon-{{ commit.category }} category-icon-table" title="{{ commit.category|title }}"></i>
                  </div>
                  
                  <!-- Commit -->
                  <div class="grid-cell grid-commit{% if commit.is_hidden_duplicate %} duplicate-hidden{% endif %}"{% if commit.is_hidden_duplicate %} data-duplicate-group="{{ group_id }}"{% endif %}>
                    <a href="{{ config.repo_base_url }}/commit/{{ commit.full_hash }}" class="commit-hash" target="_blank" rel="noopener">
                      {{ commit.hash|slice(0, 7) }}
                    </a>
                  </div>
                  
                  <!-- Message -->
                  <div class="grid-cell grid-message{% if commit.is_hidden_duplicate %} duplicate-hidden{% endif %}"{% if commit.is_hidden_duplicate %} data-duplicate-group="{{ group_id }}"{% endif %}>
                    <div class="message-wrapper">
                      <div class="message-content">
                        <span class="message-subject">{{ commit.subject }}</span>
                      </div>
                      <div class="message-controls">
                        {% if commit.message and commit.message|split('\n')|length > 1 %}
                        <span class="message-expand-icon" title="Click to see full message">...</span>
                        {% endif %}
                        {% if commit.is_first_duplicate %}
                        <div class="duplicate-controls" data-duplicate-count="{{ commit.duplicate_count }}" data-duplicate-group="{{ group_id }}">
                          <span class="duplicate-count-pill">+{{ commit.duplicate_count }}</span>
                          <i class="duplicate-toggle-arrow fas fa-chevron-right collapsed"></i>
                        </div>
                        {% endif %}
                      </div>
                      {% if commit.message and commit.message|split('\n')|length > 1 %}
                      <div class="message-full">
                        <pre>{{ commit.message }}</pre>
                      </div>
                      {% endif %}
                    </div>
                  </div>
                  
                  <!-- Date -->
                  <div class="grid-cell grid-date{% if commit.is_hidden_duplicate %} duplicate-hidden{% endif %}"{% if commit.is_hidden_duplicate %} data-duplicate-group="{{ group_id }}"{% endif %}>
                    <div class="commit-date">
                      <span class="date-absolute" data-timestamp="{{ commit.timestamp }}">{{ commit.timestamp }}</span>
                    </div>
                  </div>
                  
                  <!-- Author -->
                  <div class="grid-cell grid-author{% if commit.is_hidden_duplicate %} duplicate-hidden{% endif %}"{% if commit.is_hidden_duplicate %} data-duplicate-group="{{ group_id }}"{% endif %}>
                    <div class="author-info">
                      {% if commit.github_username %}
                      <img src="https://github.com/{{ commit.github_username }}.png?size=24" alt="{{ commit.author }}" class="author-avatar" loading="lazy">
                      <a href="https://github.com/{{ commit.github_username }}" class="author-link" target="_blank" rel="noopener">{{ commit.author }}</a>
                      {% else %}
                      <span class="author-name">{{ commit.author }}</span>
                      {% endif %}
                    </div>
                  </div>
                  
                  <!-- Files -->
                  <div class="grid-cell grid-files{% if commit.is_hidden_duplicate %} duplicate-hidden{% endif %}"{% if commit.is_hidden_duplicate %} data-duplicate-group="{{ group_id }}"{% endif %}>
                    <div class="files-wrapper">
                      <span class="file-count">{{ commit.files_count }} file{{ commit.files_count != 1 ? 's' : '' }}</span>
                      {% if commit.files and commit.files|length > 0 %}
                      <div class="files-list">
                        <div class="files-list-header">Modified Files:</div>
                        <ul>
                          {% for file in commit.files %}
                          <li>{{ file }}</li>
                          {% endfor %}
                        </ul>
                      </div>
                      {% endif %}
                    </div>
                  </div>
                  {% endfor %}
                </div>
              </div>
            </div>
          </section>
          {% endif %}
        {% else %}
          {% for ver, release in changelog.releases %}
            {% if ver == current_version %}
            <section id="{{ ver }}">
              <div class="section-header">
                <div class="section-title">
                  <h2>{{ ver }}</h2>
                </div>
                <span class="release-date">{{ release.date }}</span>
              </div>
              <div class="section-content">
                <!-- Release Summary -->
                {% set added_count = release.changesets|filter(cs => cs.category == 'added')|length %}
                {% set fixed_count = release.changesets|filter(cs => cs.category == 'fixed')|length %}
                {% set changed_count = release.changesets|filter(cs => cs.category == 'changed')|length %}
                {% set security_count = release.changesets|filter(cs => cs.category == 'security')|length %}
                {% set performance_count = release.changesets|filter(cs => cs.category == 'performance')|length %}
                {% set removed_count = release.changesets|filter(cs => cs.category == 'removed')|length %}
                
                {% if release.changesets|length > 1 %}
                <div class="release-summary">
                  <strong>{{ ver }} Summary:</strong>
                  {% set summary_parts = [] %}
                  {% if added_count > 0 %}{% set summary_parts = summary_parts|merge([added_count ~ ' new feature' ~ (added_count != 1 ? 's' : '')]) %}{% endif %}
                  {% if fixed_count > 0 %}{% set summary_parts = summary_parts|merge([fixed_count ~ ' bug fix' ~ (fixed_count != 1 ? 'es' : '')]) %}{% endif %}
                  {% if changed_count > 0 %}{% set summary_parts = summary_parts|merge([changed_count ~ ' improvement' ~ (changed_count != 1 ? 's' : '')]) %}{% endif %}
                  {% if security_count > 0 %}{% set summary_parts = summary_parts|merge([security_count ~ ' security update' ~ (security_count != 1 ? 's' : '')]) %}{% endif %}
                  {% if performance_count > 0 %}{% set summary_parts = summary_parts|merge([performance_count ~ ' performance enhancement' ~ (performance_count != 1 ? 's' : '')]) %}{% endif %}
                  {% if removed_count > 0 %}{% set summary_parts = summary_parts|merge([removed_count ~ ' removal' ~ (removed_count != 1 ? 's' : '')]) %}{% endif %}
                  {% if summary_parts|length > 1 %}
                    {% set last_item = summary_parts|last %}
                    {% set other_items = summary_parts|slice(0, -1) %}
                    {{ other_items|join(', ') }}, and {{ last_item }}.
                  {% else %}
                    {{ summary_parts|join('') }}.
                  {% endif %}
                </div>
                {% endif %}
                
                <!-- Unified Grid for All Categories -->
                <div class="changelog-grid-wrapper">
                  <div class="changelog-grid">
                    <!-- Grid Header -->
                    <div class="grid-header grid-category">Type</div>
                    <div class="grid-header grid-commit">Commit</div>
                    <div class="grid-header grid-message">Message</div>
                    <div class="grid-header grid-date">Timestamp</div>
                    <div class="grid-header grid-author">Author</div>
                    <div class="grid-header grid-files">Files</div>
                    
                    <!-- Grid Rows -->
                    {% set all_commits = [] %}
                    {% for changeset in release.changesets %}
                      {% for commit in changeset.commits %}
                        {% set all_commits = all_commits|merge([{
                          'category': changeset.category,
                          'title': changeset.title,
                          'hash': commit.hash,
                          'full_hash': commit.full_hash|default(commit.hash),
                          'subject': commit.subject|default(commit.message|split('\n')|first),
                          'message': commit.message,
                          'date': commit.date,
                          'author': commit.author,
                          'github_username': commit.github_username,
                          'files': commit.files,
                          'files_count': commit.files_count|default(commit.files|length),
                          'sort_date': commit.timestamp,
                        'timestamp': commit.timestamp
                        }]) %}
                      {% endfor %}
                    {% endfor %}
                    
                    {% set sorted_commits = all_commits|sort((a, b) => b.sort_date <=> a.sort_date) %}
                    
                    {# Pre-process commits to identify duplicate groups #}
                    {% set processed_commits = [] %}
                    {% set commit_groups = {} %}
                    {% for commit in sorted_commits %}
                      {% set subject = commit.subject %}
                      {% if commit_groups[subject] is not defined %}
                        {% set commit_groups = commit_groups|merge({(subject): []}) %}
                      {% endif %}
                      {% set commit_groups = commit_groups|merge({(subject): commit_groups[subject]|merge([commit])}) %}
                    {% endfor %}
                    
                    {# Build processed commits with duplicate information #}
                    {% for subject, group in commit_groups %}
                      {% for i, commit in group %}
                        {% set is_first_duplicate = (group|length > 1 and i == 0) %}
                        {% set is_hidden_duplicate = (group|length > 1 and i > 0) %}
                        {% set duplicate_count = group|length - 1 %}
                        {% set processed_commits = processed_commits|merge([commit|merge({
                          'is_first_duplicate': is_first_duplicate,
                          'is_hidden_duplicate': is_hidden_duplicate,
                          'duplicate_count': duplicate_count
                        })]) %}
                      {% endfor %}
                    {% endfor %}
                    
                    {% for commit in processed_commits %}
                    {% set group_id = commit.subject|replace({' ': '-', ':': '', '!': '', '?': '', '.': '', '(': '', ')': '', '[': '', ']': '', '/': '-', '\\': '-'})|lower %}
                    <!-- Category -->
                    <div class="grid-cell grid-category{% if commit.is_hidden_duplicate %} duplicate-hidden{% endif %}" data-category="{{ commit.category }}"{% if commit.is_hidden_duplicate %} data-duplicate-group="{{ group_id }}"{% endif %}>
                      <i class="fas category-icon-{{ commit.category }} category-icon-table" title="{{ commit.category|title }}"></i>
                    </div>
                    
                    <!-- Commit -->
                    <div class="grid-cell grid-commit{% if commit.is_hidden_duplicate %} duplicate-hidden{% endif %}"{% if commit.is_hidden_duplicate %} data-duplicate-group="{{ group_id }}"{% endif %}>
                      <a href="{{ config.repo_base_url }}/commit/{{ commit.full_hash }}" class="commit-hash" target="_blank" rel="noopener">
                        {{ commit.hash|slice(0, 7) }}
                      </a>
                    </div>
                    
                    <!-- Message -->
                    <div class="grid-cell grid-message{% if commit.is_hidden_duplicate %} duplicate-hidden{% endif %}"{% if commit.is_hidden_duplicate %} data-duplicate-group="{{ group_id }}"{% endif %}>
                      <div class="message-wrapper">
                        <div class="message-content">
                          <span class="message-subject">{{ commit.subject }}</span>
                        </div>
                        <div class="message-controls">
                          {% if commit.message and commit.message|split('\n')|length > 1 %}
                          <span class="message-expand-icon" title="Click to see full message">...</span>
                          {% endif %}
                          {% if commit.is_first_duplicate %}
                          <div class="duplicate-controls" data-duplicate-count="{{ commit.duplicate_count }}" data-duplicate-group="{{ group_id }}">
                            <span class="duplicate-count-pill">+{{ commit.duplicate_count }}</span>
                            <i class="duplicate-toggle-arrow fas fa-chevron-right collapsed"></i>
                          </div>
                          {% endif %}
                        </div>
                        {% if commit.message and commit.message|split('\n')|length > 1 %}
                        <div class="message-full">
                          <pre>{{ commit.message }}</pre>
                        </div>
                        {% endif %}
                      </div>
                    </div>
                    
                    <!-- Date -->
                    <div class="grid-cell grid-date{% if commit.is_hidden_duplicate %} duplicate-hidden{% endif %}"{% if commit.is_hidden_duplicate %} data-duplicate-group="{{ group_id }}"{% endif %}>
                      <div class="commit-date">
                        <span class="date-absolute" data-timestamp="{{ commit.timestamp }}">{{ commit.timestamp }}</span>
                      </div>
                    </div>
                    
                    <!-- Author -->
                    <div class="grid-cell grid-author{% if commit.is_hidden_duplicate %} duplicate-hidden{% endif %}"{% if commit.is_hidden_duplicate %} data-duplicate-group="{{ group_id }}"{% endif %}>
                      <div class="author-info">
                        {% if commit.github_username %}
                        <img src="https://github.com/{{ commit.github_username }}.png?size=24" alt="{{ commit.author }}" class="author-avatar" loading="lazy">
                        <a href="https://github.com/{{ commit.github_username }}" class="author-link" target="_blank" rel="noopener">{{ commit.author }}</a>
                        {% else %}
                        <span class="author-name">{{ commit.author }}</span>
                        {% endif %}
                      </div>
                    </div>
                    
                    <!-- Files -->
                    <div class="grid-cell grid-files{% if commit.is_hidden_duplicate %} duplicate-hidden{% endif %}"{% if commit.is_hidden_duplicate %} data-duplicate-group="{{ group_id }}"{% endif %}>
                      <div class="files-wrapper">
                        <span class="file-count">{{ commit.files_count }} file{{ commit.files_count != 1 ? 's' : '' }}</span>
                        {% if commit.files and commit.files|length > 0 %}
                        <div class="files-list">
                          <div class="files-list-header">Modified Files:</div>
                          <ul>
                            {% for file in commit.files %}
                            <li>{{ file }}</li>
                            {% endfor %}
                          </ul>
                        </div>
                        {% endif %}
                      </div>
                    </div>
                    {% endfor %}
                  </div>
                </div>
              </div>
            </section>
            {% endif %}
          {% endfor %}
        {% endif %}
      {% endif %}
      
      {% endif %}
      </main>
    </div>

    <script src="public/js/changelog.js?v={{ "now"|date("U") }}"></script>
  </body>
</html>