class SearchPresenter
  delegate :title, to: :document

  def initialize(document)
    @document = document
  end

  def to_json
    {
      title: document.title,
      content_id: document.content_id,
      content_store_document_type: document.document_type,
      description: document.summary,
      link: document.base_path,
      indexable_content: indexable_content,
      publishing_app: 'specialist-publisher',
      rendering_app: 'government-frontend',
      public_timestamp: format_date(document.public_updated_at),
      first_published_at: format_date(document.first_published_at),
    }.merge(document.format_specific_metadata).reject { |_k, v| v.blank? }
  end

  def format_date(timestamp)
    return nil if timestamp.blank?
    timestamp.to_datetime.rfc3339
  end

  def indexable_content
    Govspeak::Document.new(document.body).to_text + hidden_content
  end

private

  def hidden_content
    has_hidden_content? ? " #{document.hidden_indexable_content}" : ""
  end

  def has_hidden_content?
    defined?(document.hidden_indexable_content) && document.hidden_indexable_content
  end

  attr_reader :document
end

class SearchPresenter
  delegate :title, to: :document

  def initialize(document)
    @document = document
  end

  def to_json
    {
      title: document.title,
      content_id: document.content_id,
      content_store_document_type: document.document_type,
      description: document.summary,
      link: document.base_path,
      indexable_content: indexable_content,
      publishing_app: 'specialist-publisher',
      rendering_app: 'government-frontend',
      public_timestamp: format_date(document.public_updated_at),
      first_published_at: format_date(document.first_published_at),
    }.merge(document.format_specific_metadata).reject { |_k, v| v.blank? }
  end

  def format_date(timestamp)
    raise ArgumentError, "Timestamp is blank" if timestamp.blank?
    timestamp.to_datetime.rfc3339
  end

  def indexable_content
    Govspeak::Document.new(document.body).to_text + hidden_content
  end

private

  def hidden_content
    has_hidden_content? ? " #{document.hidden_indexable_content}" : ""
  end

  def has_hidden_content?
    defined?(document.hidden_indexable_content) && document.hidden_indexable_content
  end

  attr_reader :document
end

Revert "Make date field in DocumentSearch optional"
Revert "Make date field in DocumentSearch optional"

This reverts commit 230461613f1a51d4cb93d415e27001c4dd66c748.

Date fields were made optional because of a problem in a publishing API
republishing task which was not saving the dates correctly. This caused
errors in finder-frontend because Atom feeds for finders rely on the
`public_timestamp` field).

The original publishing API issue has now been fixed so specialist
publisher should no longer send blank date fields to the search API.
