class AcquiredMediaRelease < ApplicationRecord
  include Confirmable
  include Contractable
  include Exploitable
  include Notable
  include Releasable
  include Searchable
  include Taggable
  include Signable
  include Syncable
  include PersonName
  include CsvExportable
  include Approvable
  include GuardianPhotoable
  include SecondGuardianPhotoable
  include GuardianName
  include SecondGuardianName
  include FilesFilterable
  
  class << self
    def custom_csv_exportable_headers
      %i[name files_count owner_info]
    end
  end

  # This association needs to be removed after changing the API. Removing it right now will cause failure in API specs.
  has_many :file_infos, as: :releasable, dependent: :destroy

  has_many_attached :files

  composed_of :person_address,
              class_name: "Address",
              mapping: [ 
                %w(person_address_street1 street1), 
                %w(person_address_street2 street2), 
                %w(person_address_city city), 
                %w(person_address_state state), 
                %w(person_address_zip zip), 
                %w(person_address_country country) 
              ]

  composed_of :guardian_address,
              class_name: 'Address',
              mapping: [
                  %w[guardian_address_street1 street1],
                  %w[guardian_address_street2 street2],
                  %w[guardian_address_city city],
                  %w[guardian_address_state state],
                  %w[guardian_address_zip zip],
                  %w[guardian_address_country country]
              ]

  composed_of :guardian_2_address,
              class_name: 'Address',
              mapping: [
                  %w[guardian_2_address_street1 street1],
                  %w[guardian_2_address_street2 street2],
                  %w[guardian_2_address_city city],
                  %w[guardian_2_address_state state],
                  %w[guardian_2_address_zip zip],
                  %w[guardian_2_address_country country]
              ]

  # We don't care for the argument but method WILL receive option name
  # when called from inside with_option block, hence * argument
  def self.face_photo_acceptable_content_types(*)
    ['image/png', 'image/jpeg']
  end

  def self.acceptable_import_file_extensions
    ['.png', '.jpeg', '.jpg', '.pdf']
  end

  validates :name, presence: true
  validates :person_email, email: true, allow_blank: true

  # These validations apply to releases created natively by the system (i.e. not imported from elsewhere)
  with_options on: :native do
    validates :signature, attached: true
  end

  # These validations apply to releases imported to the system from an outside source
  with_options on: :non_native do
    validates :contract, attached: true
  end

  searchable_on %i[
    name
    person_address_street1 person_address_street2 person_address_city person_address_state person_address_zip person_address_country
  ]

  # CATEGORIES = ["Artwork", "Film Footage", "Video Footage", "Still Photograph"].freeze
  CATEGORIES = ["Film Footage", "Video Footage", "Still Photograph"].freeze

  def second_guardian_present?
    guardian_2_first_name.present?
  end

  def contact_person
    @contact_person ||= Contact.new(person_name, person_address, person_email, person_phone)
  end

  def uses_edl?
    true
  end

  def files_count
    files.any? ? files.size : I18n.t('acquired_media_releases.acquired_media_release.no_media')
  end
end
