class MaterialRelease < ApplicationRecord
  include Confirmable
  include Contractable
  include Exploitable
  include Notable
  include Photoable # This association needs to be removed after changing the API. Removing it right now will cause failure in API specs.
  include Releasable
  include Searchable
  include Signable
  include Syncable
  include Taggable
  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 owner_info files_count]
    end
  end

  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]
              ]
  
  has_many_attached :files

  # 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

  with_options on: :native do
    validates :person_first_name, :person_last_name, presence: true
    validates :signature, 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
  ]

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

  def second_guardian_present?
    guardian_2_first_name.present?
  end

  def uses_edl?
    true
  end

  def files_count
    files.any? ? files.size : I18n.t('material_releases.material_release.no_media')
  end
end
