<%
  attachment = GovukPublishingComponents::Presenters::Attachment.new(attachment)
  target ||= "_self"
  hide_opendocument_metadata ||= false
  attributes = []
  data_attributes ||= {}

  if attachment.content_type_name
    content = if attachment.content_type_abbr
                raw tag.abbr(attachment.content_type.abbr,
                             title: attachment.content_type_name,
                             class: "gem-c-attachment__abbr")
              else
                attachment.content_type_name
              end
    attributes << tag.span(content, class: "gem-c-attachment__attribute")
  end

  if attachment.file_size
    attributes << tag.span(
      number_to_human_size(attachment.file_size),
      class: "gem-c-attachment__attribute",
    )
  end

  if attachment.number_of_pages
    attributes << tag.span(
      pluralize(attachment.number_of_pages, "page"),
      class: "gem-c-attachment__attribute",
    )
  end
%>
<%= tag.section class: "gem-c-attachment" do %>
  <%= tag.div class: "gem-c-attachment__thumbnail" do %>
    <%= link_to attachment.url,
                class: "govuk-link",
                target: target,
                tabindex: "-1",
                "aria-hidden": true,
                data: data_attributes do %>
      <% if attachment.document? %>
        <%= render "govuk_publishing_components/components/attachment/thumbnail_document.svg" %>
      <% elsif attachment.spreadsheet? %>
        <%= render "govuk_publishing_components/components/attachment/thumbnail_spreadsheet.svg" %>
      <% else %>
        <%= render "govuk_publishing_components/components/attachment/thumbnail_generic.svg" %>
      <% end %>
    <% end %>
  <% end %>

  <%= tag.div class: "gem-c-attachment__details" do %>
    <%= tag.h2 class: "gem-c-attachment__title" do %>
      <%= link_to attachment.title, attachment.url,
            class: "govuk-link gem-c-attachment__link",
            target: target,
            data: data_attributes %>
    <% end %>

    <% if attributes.any? %>
      <%= tag.p raw(attributes.join(', ')), class: "gem-c-attachment__metadata" %>
    <% end %>

    <% unless hide_opendocument_metadata %>
      <% if attachment.opendocument? %>
        <%= tag.p class: "gem-c-attachment__metadata" do %>
          <%= t("components.attachment.opendocument_html", target: target) %>
        <% end %>
      <% end %>
    <% end %>

    <% if attachment.alternative_format_contact_email %>
      <%= tag.p t("components.attachment.request_format_text"), class: "gem-c-attachment__metadata" %>
      <%= render "govuk_publishing_components/components/details", {
        title: t("components.attachment.request_format_cta")
      } do %>
        <%= t("components.attachment.request_format_details_html", alternative_format_contact_email: attachment.alternative_format_contact_email) %>
      <% end %>
    <% end %>
  <% end %>
<% end %>