class MedicalRelease < ApplicationRecord
  include Contractable
  include Notable
  include Photoable
  include Releasable
  include Searchable
  include Signable
  include Syncable
  include PersonName

  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) 
              ]

  def self.face_photo_acceptable_content_types
    ["image/png", "image/jpeg"]
  end

  # These validations apply to all releases
  validates :person_first_name, :person_last_name, presence: true
  validates :person_email, email: true, allow_blank: true

  acts_as_taggable_on :internal_tags, :tags

  # 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[ 
    person_first_name person_last_name person_email person_phone
    person_address_street1 person_address_street2 person_address_city person_address_state person_address_zip person_address_country 
  ]

  # All releases must respond to the following messages
  def name
    person_name
  end

  def filename_suffix
    "#{person_last_name} #{person_first_name}"
  end

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

  def uses_edl?
    false
  end

  def minor?
    false
  end

  def contract_file_name
    "#{project.name.parameterize}_#{contract_template.release_type}_#{(signed_at || created_at).strftime("%Y.%m.%d")}_#{release_number}_#{filename_suffix.parameterize}"
  end
end
