# frozen_string_literal: true

# Generated by curation_concerns:models:install
class FileSet < ActiveFedora::Base
  before_validation :clean_external_resource_url

  property :allow_display_after_expiration, predicate: ::RDF::URI.new('http://fulcrum.org/ns#allowDisplayAfterExpiration'), multiple: false do |index|
    index.as :symbol
  end

  property :allow_download, predicate: ::RDF::URI.new('http://fulcrum.org/ns#allowDownload'), multiple: false do |index|
    index.as :symbol
  end

  property :allow_download_after_expiration, predicate: ::RDF::URI.new('http://fulcrum.org/ns#allowDownloadAfterExpiration'), multiple: false do |index|
    index.as :symbol
  end

  property :allow_hi_res, predicate: ::RDF::URI.new('http://fulcrum.org/ns#allowHiRes'), multiple: false do |index|
    index.as :symbol
  end

  property :alt_text, predicate: ::RDF::Vocab::SCHEMA.accessibilityFeature do |index|
    index.as :stored_searchable
  end

  property :article_creator, predicate: ::RDF::URI.new('http://fulcrum.org/ns#articleCreator') do |index|
    index.as :stored_searchable, :facetable
  end

  property :article_display_date, predicate: ::RDF::URI.new('http://fulcrum.org/ns#articleDisplayDate'), multiple: false do |index|
    index.as :symbol
  end

  property :article_issue, predicate: ::RDF::URI.new('http://fulcrum.org/ns#articleIssue'), multiple: false do |index|
    index.as :symbol
  end

  property :article_permalink, predicate: ::RDF::URI.new('http://fulcrum.org/ns#articlePermalink'), multiple: false do |index|
    index.as :symbol
  end

  property :article_title, predicate: ::RDF::URI.new('http://fulcrum.org/ns#articleTitle'), multiple: false do |index|
    index.as :stored_searchable, :facetable
  end

  property :article_volume, predicate: ::RDF::URI.new('http://fulcrum.org/ns#articleVolume'), multiple: false do |index|
    index.as :symbol
  end

  property :caption, predicate: ::RDF::Vocab::SCHEMA.caption do |index|
    index.as :stored_searchable
  end

  property :closed_captions, predicate: ::RDF::URI.new('http://fulcrum.org/ns#closed_captions') do |index|
    index.as :stored_searchable
  end

  property :content_type, predicate: ::RDF::Vocab::SCHEMA.contentType do |index|
    index.as :stored_searchable, :facetable
  end

  property :copyright_status, predicate: ::RDF::URI.new('http://fulcrum.org/ns#copyrightStatus'), multiple: false do |index|
    index.as :symbol
  end

  property :credit_line, predicate: ::RDF::URI.new('http://fulcrum.org/ns#creditLine'), multiple: false do |index|
    index.as :symbol
  end

  property :display_date, predicate: ::RDF::URI.new('http://fulcrum.org/ns#displayDate') do |index|
    index.as :stored_searchable
  end

  property :exclusive_to_platform, predicate: ::RDF::URI.new('http://fulcrum.org/ns#exclusiveToPlatform'), multiple: false do |index|
    index.as :symbol, :facetable
  end

  property :external_resource_url, predicate: ::RDF::Vocab::Identifiers.uri, multiple: false do |index|
    index.as :symbol
  end
  validates :external_resource_url, format: { allow_blank: true, with: URI.regexp(%w[http https]), message: 'must be a url.' }

  property :permissions_expiration_date, predicate: ::RDF::URI.new('http://fulcrum.org/ns#permissionsExpirationDate'), multiple: false do |index|
    index.as :symbol
  end
  validates :permissions_expiration_date, format: {
    with: /\A\d\d\d\d-\d\d-\d\d\z/,
    message: "Your permissions expiration date must be in YYYY-MM-DD format",
    allow_blank: true
  }

  property :rights_granted, predicate: ::RDF::URI.new('http://fulcrum.org/ns#rightsGranted'), multiple: false do |index|
    index.as :symbol
  end

  property :section_title, predicate: ::RDF::Vocab::DC.relation do |index|
    index.as :stored_searchable, :facetable
  end

  property :sort_date, predicate: ::RDF::Vocab::DC.date, multiple: false do |index|
    index.as :stored_searchable, :facetable
  end
  validates :sort_date, format: { with: /\A\d\d\d\d-\d\d-\d\d\z/, message: "Your sort date must be in YYYY-MM-DD format", allow_blank: true }

  property :transcript, predicate: ::RDF::URI.new('http://fulcrum.org/ns#transcript'), multiple: false do |index|
    index.as :stored_searchable
  end

  property :translation, predicate: ::RDF::URI.new('http://fulcrum.org/ns#translation') do |index|
    index.as :stored_searchable
  end

  property :visual_descriptions, predicate: ::RDF::URI.new('http://fulcrum.org/ns#visual_descriptions') do |index|
    index.as :stored_searchable
  end

  include HeliotropeUniversalMetadata
  include ::Hyrax::FileSetBehavior
  # This must come after the FileSetBehavior because it finalizes the metadata
  # schema (by adding accepts_nested_attributes)
  include ::Hyrax::BasicMetadata

  include HeliotropeMimeTypes

  self.indexer = ::FileSetIndexer

  after_create :after_create_jobs
  after_destroy :after_destroy_jobs

  # Cast to a SolrDocument by querying from Solr
  # Hyrax Heliotrope override: cast to an actual presenter, not just a solr_doc
  def to_presenter
    ::Hyrax::FileSetPresenter.new(SolrDocument.new(ActiveFedora::SolrService.query("id:#{id}", rows: 1).first.to_h), nil)
  end

  # see https://github.com/samvera/hyrax/issues/5900 and https://mlit.atlassian.net/browse/HELIO-4358
  # I don't think the aliased properties should ever have been added. Let the storage layer do its job.
  alias date_uploaded create_date
  alias date_modified modified_date

  private

    def after_create_jobs
      HandleCreateJob.perform_later(HandleNet::FULCRUM_HANDLE_PREFIX + id,
                                    Rails.application.routes.url_helpers.hyrax_file_set_url(id))
    end

    def after_destroy_jobs
      HandleDeleteJob.perform_later(HandleNet::FULCRUM_HANDLE_PREFIX + id)
    end

    def clean_external_resource_url
      # HELIO-3502, https://stackoverflow.com/a/36197554, squish should also work
      self.external_resource_url = self.external_resource_url.gsub(/[[:space:]]/, '') if self.external_resource_url.present?
    end
end
