{#
  Copyright (C) 2020-2024 CERN.
  Copyright (C) 2020-2021 Northwestern University.
  Copyright (C) 2021-2023 TU Wien.
  Copyright (C) 2021 Graz University of Technology.
  Copyright (C) 2021 New York University.
  Copyright (C) 2024-2025 KTH Royal Institute of Technology.

  Invenio RDM Records is free software; you can redistribute it and/or modify
  it under the terms of the MIT License; see LICENSE file for more details.
#}

{% if use_theme_basic_template|default(true) %}
  {%- extends config.BASE_TEMPLATE %}
  {%- block css %}
    {{ super() }}
    {% if community and community.theme and community.theme.enabled  %}
      <link rel="stylesheet" type="text/css"
            href="/communities/{{ community.slug }}/community-theme-{{ community.revision_id }}.css">
    {% endif %}
  {%- endblock css %}
{% endif %}

{%- from "invenio_app_rdm/records/macros/files.html" import file_list_box, preview_file_box, media_file_list_box with context %}

{%- set title = record_ui["metadata"]["title"] %}
{%- set metadata = record_ui["metadata"] %}

<!-- Shared namespace for files to allow overridability on child blocks while maintaining reference to parent values -->
{% set files_ns = namespace(preview_file=None, files=[]) %}

{%- set can_curate_record = permissions is defined and (permissions.can_edit or permissions.can_review) %}

<!-- preview_submission_request is set to true when coming from a community submission request -->
{%- set is_preview_submission_request = preview_submission_request or false %}
{%- set show_record_management_menu = can_curate_record and (not is_preview or is_preview_submission_request) %}

{%- if record_ui["parent"]["access"]["settings"] %}
  {%- set allow_user_requests = not current_user.is_anonymous and record_ui["parent"]["access"]["settings"]["allow_user_requests"] %}
  {%- set allow_guest_requests = current_user.is_anonymous and record_ui["parent"]["access"]["settings"]["allow_guest_requests"] %}
{%- endif %}

{%- block head_meta %}
  {% if use_theme_basic_template|default(true) %}
    {{ super() }}
  {% endif %}
  {%- include "invenio_app_rdm/records/details/meta.html" %}
{%- endblock head_meta %}




{%- block page_body %}
<span id="detail-share-btn-require-link-expiration"
        data-share-btn-require-link-expiration='{{ config.RDM_RECORDS_REQUIRE_SECRET_LINKS_EXPIRATION | tojson }}'>

  <section id="banners" class="banners" aria-label="{{ _('Information banner') }}">
    <!-- COMMUNITY HEADER: hide it when displaying the submission request -->
    {% if not is_preview_submission_request %}
      {% if community %}
        {% set is_community_restricted = community.access.visibility == 'restricted' %}
        {% if community.theme and community.theme.enabled %}
          {% include "invenio_communities/details/header.html" %}
        {% else %}
          <div
            class="ui fluid container page-subheader-outer with-submenu compact ml-0-mobile mr-0-mobile">
            <div class="ui container page-subheader">
              <div class="page-subheader-element">
                <img
                  class="ui rounded image community-header-logo"
                  src="{{ community.links.logo | resolve_community_logo(community.id) }}"
                  alt=""
                />
              </div>
              <div class="page-subheader-element">
                <div class="ui header">
                  <a
                  href="{{ url_for('invenio_app_rdm_communities.communities_detail', pid_value=community.slug) }}" class="ui small header">
                    {{ community.metadata.title }}
                  </a>
                  <!-- Show the icon for subcommunities -->
                  {% if community.parent %}
                    <p class="ml-2 mb-0 display-inline-block"><i class="green check circle outline icon"></i></p>
                  {% endif %}
                  {% if community.parent %}
                    <div class="sub header">{{ _('Part of') }}
                      <a
                  href="{{ url_for('invenio_app_rdm_communities.communities_home', pid_value=community.parent.slug) }}">
                   {{community.parent.metadata.title}}
                  </a>
                </div>
                  {% endif %}
                </div>
                {% if is_community_restricted %}
                  <span
                    class="ui label horizontal small access-status restricted rel-ml-1"
                    title="{{ _('Community visibility') }}"
                    data-tooltip="{{ _('The community is restricted to users with access.') }}"
                    data-inverted="">
                  <i class="icon ban" aria-hidden="true"></i> {{ _("Restricted") }}
                </span>
                {% endif %}
              </div>
            </div>
          </div>
        {% endif %}
      {% endif %}
    {% endif %}
    <!-- /COMMUNITY HEADER -->

    <!-- PREVIEW HEADER -->
    {% if is_preview %}
      <div class="ui info bottom attached manage message">
        <div class="ui container">
          <div class="header">
            <i class="eye icon"></i>
            <strong>{{ _("Preview") }}</strong>
          </div>
          <p>
            {% if not is_draft %}
              {{ _("You are previewing a published record.") }}
            {% elif record_ui["is_published"] %}
              {{ _("You are previewing changes that have not yet been published.") }}
            {% elif not record_ui["is_published"] and record_ui["versions"]["index"] == 1 %}
              {{ _("You are previewing a new record that has not yet been published.") }}
            {% elif not record_ui["is_published"] and record_ui["versions"]["index"] > 1 %}
              {{ _("You are previewing a new record version that has not yet been published.") }}
            {% endif %}
          </p>
        </div>
      </div>
    {% endif %}
    <!-- /PREVIEW HEADER -->

    {% if record_ui["is_published"] and record_ui["links"]["latest_html"] and not record_ui["versions"]["is_latest"] %}
      <div class="ui warning flashed bottom attached manage message">
        <div class="ui container">
          <div class="ui relaxed grid">
            <div class="column">
              <div class="row">
                <p>
                  {% trans link_start=('<a href="' + record_ui["links"]["latest_html"] + '"><b>')|safe, link_end='</b></a>'|safe %}
                    There is a {{ link_start }}newer version{{ link_end }} of the record
                    available.
                  {% endtrans %}
                </p>
              </div>
            </div>
          </div>
        </div>
      </div>
    {% endif %}
  </section>


  <div class="ui container">
    <div class="ui relaxed grid mt-5">
      <div class="two column row top-padded">

        {# Main content #}
        <article
          class="sixteen wide tablet eleven wide computer column main-record-content">
          {% if record_ui["access"]["record"] == 'restricted' %}
            <section id="record-access-status"
                     class="ui {{ record_ui["ui"]["access_status"]["message_class"] }} message rel-mt-1"
                     aria-label="{{ _('Record access information') }}"
            >
              <i class="{{ record_ui["ui"]["access_status"]["icon"] }} icon" aria-hidden="true"></i>
              <strong>{{ record_ui["ui"]["access_status"]["title_l10n"] }}</strong>
              {{ record_ui["ui"]["access_status"]["description_l10n"] | safe }}

              {% if record_ui["access"]["embargo"]["reason"] %}
                <p>{{ _("Reason") }}: {{ record_ui["access"]["embargo"]["reason"] }}</p>
              {% endif %}
            </section>
          {% endif %}

          {%- block record_body -%}

            {%- block record_header -%}

              {%- block record_header_button -%}

                {% if is_preview and not is_preview_submission_request and can_curate_record and is_draft %}
                  <nav class="back-navigation rel-pb-2 pl-0"
                       aria-label="{{ _('Back-navigation') }}">
                    {%- set back_page = url_for('invenio_app_rdm_records.deposit_edit', pid_value=record_ui.id) -%}
                    {%- set back_btn_label = _('Back to edit') -%}
                    <a class="ui button labeled icon small compact"
                       href="{{ back_page }}">
                      <i class="ui icon angle left"></i> {{ back_btn_label }}</a>
                  </nav>
                {% endif %}

              {% endblock record_header_button %}

              {% if show_record_management_menu %}
                <section id="mobile-record-management"
                         class="ui grid tablet only mobile only">
                  <div class="sixteen wide column right aligned">
                    <button id="manage-record-btn"
                            class="ui small basic icon button m-0"
                            aria-haspopup="dialog"
                            aria-expanded="false"
                    >
                      <i class="cog icon"></i> {{ _('Manage record') }}
                    </button>
                  </div>
                  <div id="recordManagementMobile"
                       role="dialog"
                       class="ui flowing popup transition hidden"
                       {% if config.RDM_DETAIL_SIDE_BAR_MANAGE_ATTRIBUTES_EXTENSION_TEMPLATE %}
                        {% include config.RDM_DETAIL_SIDE_BAR_MANAGE_ATTRIBUTES_EXTENSION_TEMPLATE %}
                       {% endif %}
                  >
                  </div>
                </section>
              {% endif %}

              <section id="record-info"
                       aria-label="{{ _('Publication date and version number') }}">
                <div class="ui grid middle aligned">
                  <div class="two column row">
                    <div class="left floated left aligned column">
                      <span class="ui" title="{{ _('Publication date') }}">
                        {{ _('Published') }} {{ record_ui["ui"]["publication_date_l10n_long"] }}
                      </span>
                      <span
                        class="label text-muted"> | {{ _('Version {version_number}').format(version_number=record_ui["ui"]["version"]) }}</span>
                    </div>
                    <div class="right floated right aligned column">

                      {% if record_ui["ui"]["resource_type"] %}
                        <span role="note"
                              class="ui label horizontal small neutral mb-5"
                              aria-label="{{ _('Resource type') }}"
                        >
                          {{ record_ui["ui"]["resource_type"]["title_l10n"] }}
                        </span>
                      {% endif %}

                      <span role="note"
                            class="ui label horizontal small access-status {{ record_ui["ui"]["access_status"]["id"] }} mb-5"
                            data-tooltip="{{ record_ui["ui"]["access_status"]["description_l10n"] }}"
                            data-inverted=""
                            aria-label="{{ _('Access status') }}"
                      >
                        {% if record_ui["ui"]["access_status"]["icon"] %}
                          <i class="icon {{ record_ui["ui"]["access_status"]["icon"] }}"
                             aria-hidden="true"></i>
                        {% endif %}
                        <span
                          aria-label="{{ record_ui["ui"]["access_status"]["description_l10n"] }}">
                          {{ record_ui["ui"]["access_status"]["title_l10n"] }}
                        </span>
                      </span>
                    </div>
                  </div>
                </div>
              </section>
              <div class="ui divider hidden"></div>

            {%- endblock record_header -%}

            {# Title #}
            {%- block record_title -%}
              <section id="record-title-section"
                       aria-label="{{ _('Record title, authors and contributors') }}">
                <h1 id="record-title"
                    class="wrap-overflowing-text">{{ metadata.title }}</h1>

                {% if record_ui["ui"]["creators"] or record_ui["ui"]["contributors"] %}
                  <section id="creatibutors"
                           aria-label="{{ _('Authors and contributors') }}">
                    {%- include "invenio_app_rdm/records/details/creatibutors.html" %}
                  </section>
                {% endif %}
              </section>
            {%- endblock record_title -%}

            {# Description #}
            {%- block record_content -%}
              {%- include "invenio_app_rdm/records/details/description.html" %}
            {%- endblock record_content -%}

            {# Files #}
            {%- block record_files -%}
              {# record has files BUT passed files are empty. This happens when we display are request. #}
              {%- if record_ui["files"]["enabled"] -%}
                <section id="record-files" class="rel-mt-2 rel-mb-3"
                         aria-label="{{ _('Files') }}">
                  {%- if permissions.can_read_files -%}
                    {# record has files AND user can see files #}
                    {# Shared namespace for files (outside blocks) #}
                    {%- set files_ns.files = files | order_entries | selectattr("status", "==", "completed") | list %}
                    {%- if files_ns.files|length > 0 -%}
                      <h2 id="files-heading">{{ _('Files') }}</h2>
                      {%- if files_ns.files|has_previewable_files -%}
                        {%-set files_ns.preview_file = files_ns.files|select_preview_file(default_preview=record_ui["files"]["default_preview"]) %}
                        {%- block record_file_preview -%}
                          {{ preview_file_box(files_ns.preview_file, record_ui["id"], is_preview, record, include_deleted) }}
                        {%- endblock record_file_preview -%}
                      {%- endif -%}
                      {%- block record_file_list -%}
                        {{ file_list_box(files_ns.files, record_ui["id"], is_preview, include_deleted, record, permissions) }}
                      {%- endblock record_file_list -%}
                    {% endif %}
                  {% else %}
                    {# record has files BUT user does not have permission to see files #}
                    <div
                      class="ui accordion panel mb-10 {{ record_ui["ui"]["access_status"]["id"] }}"
                      href="#files-preview-accordion-panel">
                      <h3
                        class="active title panel-heading {{ record_ui["ui"]["access_status"]["id"] }} m-0">
                        <div role="button" id="files-preview-accordion-trigger"
                             tabindex="0" class="trigger"
                             aria-controls="files-preview-accordion-panel">
                          {{ _("Files") }}
                          <i class="angle right icon" aria-hidden="true"></i>
                        </div>
                      </h3>
                      <div role="region" id="files-preview-accordion-panel"
                           aria-labelledby="files-preview-accordion-trigger"
                           class="active content preview-container pt-0">
                        <div
                          class="ui {{ record_ui["ui"]["access_status"]["message_class"] }} message file-box-message rel-pl-1 rel-pr-1">
                          <i class="ui {{ record_ui["ui"]["access_status"]["icon"] }} icon"
                             aria-hidden="true"></i>
                          <h4
                            class="inline">{{ record_ui["ui"]["access_status"]["title_l10n"] }}</h4>
                          <p>{{ record_ui["ui"]["access_status"]["description_l10n"] | safe }}</p>

                          {% if record_ui["access"]["embargo"]["reason"] %}
                            <p>{{ _("Reason") }}: {{ record_ui["access"]["embargo"]["reason"] }}</p>
                          {% endif %}

                          {% block record_files_access_request %}
                            {%- if allow_user_requests or allow_guest_requests %}
                              <div class="ui divider"></div>
                              <h4>{{ _("Request access") }}</h4>
                              <p>
                                {{ _("If you would like to request access to these files, please fill out the form below.") }}
                              </p>
                              {%- if record_ui["parent"]["access"]["settings"] %}
                                {%- set accept_conditions_text = record_ui["parent"]["access"]["settings"]["accept_conditions_text"] %}
                              {%- endif %}

                              {%- if accept_conditions_text %}
                                <p class="ui small header rel-mt-2 rel-mb-1">
                                  {{ _("You need to satisfy these conditions in order for this request to be accepted:") }}
                                </p>
                                <div class="rel-mt-2 rich-input-content">
                                  {{ accept_conditions_text | safe }}
                                </div>
                              {%- endif %}

                              {%- include "invenio_app_rdm/records/details/access-form.html" %}
                            {%- endif %}
                          {% endblock record_files_access_request %}
                        </div>
                      </div>
                    </div>
                  {%- endif %}
                </section>
              {%- endif %}
            {%- endblock record_files -%}

            {# Media files #}
            {%- block record_media_files -%}
              {# record has media files AND user can see files #}
              {# can_media_read_files is false when record is fully restricted and users can't see the landing page at all #}
              {%- if media_files and media_files.enabled and media_files.entries and permissions.can_media_read_files -%}
                {%- set any_visible = media_files.entries | selectattr('access.hidden', 'equalto', false) | list | length > 0 %}
                {%- if any_visible %}
                  <section id="record-media-files" aria-label="{{ _('System files') }}">
                    {%- set media_files = media_files | order_entries | selectattr("status", "==", "completed") | list %}
                    {%- if media_files|length > 0 -%}
                      {{ media_file_list_box(media_files, record_ui["id"], is_preview, include_deleted, record, permissions) }}
                    {%- endif %}
                  </section>
                {%- endif %}
              {%- endif %}
            {%- endblock record_media_files -%}

            {# Additional details #}
            {%- block record_details -%}
              <section id="additional-details" class="rel-mt-2"
                       aria-label="{{ _('Additional record details') }}">
                {%- include "invenio_app_rdm/records/details/details.html" %}
              </section>
            {%- endblock record_details -%}

            {# Record footer #}
            {%- block record_footer -%}
            {%- endblock record_footer -%}

          {%- endblock record_body %}
        </article>

        {# Sidebar #}
        <aside class="sixteen wide tablet five wide computer column sidebar"
               aria-label="{{ _('Record details') }}">
          {% block record_sidebar %}
            {%- include "invenio_app_rdm/records/details/side_bar.html" %}
          {% endblock record_sidebar %}
        </aside>
      </div>
    </div>

    <div class="ui container">
      <div class="ui relaxed grid">
        <div class="two column row">
          <div class="sixteen wide tablet eleven wide computer column">
            {# Jump up button #}
            {%- block jump -%}
              <div class="ui grid">
                <div class="centered row rel-mt-1">
                  <button id="jump-btn" class="jump-to-top ui button labeled icon"
                          aria-label="{{ _('Jump to top of page') }}">
                    <i class="arrow alternate circle up outline icon"></i>
                    {{ _("Jump up") }}
                  </button>
                </div>
              </div>
            {%- endblock jump -%}
          </div>
        </div>
      </div>
    </div>
  </div>

{%- endblock page_body %}

{%- block javascript %}
  {% if use_theme_basic_template|default(true) %}
    {{ super() }}
  {% else %}
    {% include config.THEME_JAVASCRIPT_TEMPLATE %}
  {% endif %}
  {%- block record_jsonld %}
    {% set jsonld_serialization = record_ui | transform_record('SchemaorgJSONLDSerializer', throws=False) %}
    {%- if jsonld_serialization %}
      <script type='application/ld+json'>{{ jsonld_serialization | tojson }}</script>
    {%- endif %}
  {%- endblock record_jsonld %}

  {{ webpack['invenio-app-rdm-landing-page-theme.js'] }}
  {{ webpack['invenio-app-rdm-landing-page.js'] }}
  {{ webpack['previewer_theme.js'] }}
{%- endblock javascript %}
