class LocationRelease < ApplicationRecord
  include Confirmable
  include Contractable
  include Exploitable
  include Notable
  include Photoable
  include Releasable
  include Searchable
  include Signable
  include Syncable
  include Taggable
  include PersonName

  composed_of :address,
              mapping: [
                %w(address_street1 street1),
                %w(address_street2 street2),
                %w(address_city city),
                %w(address_state state),
                %w(address_zip zip),
                %w(address_country country)
              ]
  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)
              ]

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

  with_options on: :native do
    validates :person_first_name, :person_last_name, presence: true
    validates :signature, attached: true
  end

  searchable_on %i[
    name
    address_street1 address_street2 address_city address_state address_zip address_country
    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 minor?
    false
  end

  def uses_edl?
    true
  end

  private

  def end_date_after_start_date
    return true if filming_ended_on.blank? || filming_started_on.blank?

    if filming_ended_on < filming_started_on
      errors.add(:filming_ended_on, "must be after the filming started on date")
    end
  end
end
